File size: 4,131 Bytes
7153fd1
216b9fc
 
de4cfa2
216b9fc
de4cfa2
216b9fc
de4cfa2
 
 
 
 
216b9fc
de4cfa2
7153fd1
216b9fc
 
 
38d6937
216b9fc
 
 
 
 
38d6937
216b9fc
 
38d6937
216b9fc
 
 
 
38d6937
216b9fc
f7f68c1
 
 
 
 
 
 
 
 
 
 
61dee7f
 
 
 
 
 
216b9fc
 
 
 
38d6937
216b9fc
 
 
 
 
38d6937
 
216b9fc
 
 
 
 
 
 
 
 
38d6937
 
216b9fc
38d6937
 
216b9fc
38d6937
216b9fc
 
 
 
 
 
 
 
 
 
 
 
 
de4cfa2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
license: apache-2.0
task_categories:
  - text-generation
language:
  - en
tags:
  - assembly
  - c
  - leetcode
  - compiler
  - code
size_categories:
  - 10K<n<100K
---

# LeetCode Assembly Dataset

441 LeetCode problems solved in C, compiled to assembly across **4 architectures**, **2 compilers**, and **4 optimization levels** using GCC and Clang via the [Godbolt Compiler Explorer](https://godbolt.org) API.

## Dataset Summary

| Stat | Value |
|------|-------|
| Total rows | 14,112 |
| Unique problems | 441 |
| Architectures | x86-64, AArch64, MIPS64, RISC-V 64 |
| Compilers | GCC 15.2, Clang 21.1.0 |
| Optimization levels | -O0, -O1, -O2, -O3 |
| Compilation success rate | 100% |
| Difficulty split | Easy: 98, Medium: 259, Hard: 84 |

Each problem has **32 assembly variants** (4 architectures x 2 compilers x 4 optimization levels), making this useful for studying how the same algorithm compiles differently across ISAs, compilers, and optimization settings.

## Key Insights

- **-O3 often produces more code than -O0**, not less. Across all architectures, -O3 output is 37-73% larger on average due to aggressive inlining and loop unrolling.
- **-O1 consistently produces the most compact assembly** across all four architectures.
- **Recursive tree problems see extreme -O3 blowup.** The top 10 largest O3-vs-O0 ratios are all binary tree problems. Worst case: Longest Univalue Path goes from 90 lines (O0) to 3,809 lines (O3) -- a 42x increase.
- **Iterative and math problems shrink dramatically at -O3.** Flip String to Monotone Increasing drops from 89 to 8 lines (91% reduction). Nim Game compiles down to just 4 lines.
- **AArch64 produces the most compact assembly at -O2** (avg 153 lines), followed by x86-64 (170), RISC-V (170), and MIPS64 (236). MIPS is 39% more verbose than AArch64.
- **Hard problems produce 2x more assembly than Easy problems** at -O2 on x86-64 (247 vs 121 avg lines). Algorithmic complexity maps directly to code size.
- **The largest output is LFU Cache** (1,121 lines at -O0 on x86-64), a complex doubly-linked list plus hash table implementation.
- **The smallest output is Nim Game at -O3** (4 lines on x86-64) -- the entire solution optimizes to a single bitwise AND.

## Use Cases

- Training or evaluating models on C-to-assembly translation
- Studying how optimization levels affect generated code
- Compiler behavior research

## Schema

| Column | Type | Description |
|--------|------|-------------|
| `id` | int | Unique row identifier (0-14111) |
| `problem_id` | int | LeetCode problem number |
| `problem_title` | string | Problem name (e.g. "Two Sum") |
| `difficulty` | string | Easy, Medium, or Hard |
| `c_source` | string | Complete C source code |
| `architecture` | string | Target ISA: `x86-64`, `aarch64`, `mips64`, `riscv64` |
| `optimization` | string | Optimization flag: `-O0`, `-O1`, `-O2`, `-O3` |
| `compiler` | string | Compiler version used (e.g. `x86-64 gcc 15.2`, `x86-64 clang 21.1.0`) |
| `assembly` | string | Compiled assembly output |

## Usage

```python
from datasets import load_dataset

ds = load_dataset("ronantakizawa/leetcode-assembly", split="train")

# Get all x86-64 GCC assembly at -O2
x86_gcc_O2 = ds.filter(lambda r: r["architecture"] == "x86-64" and r["compiler"].startswith("x86-64 gcc") and r["optimization"] == "-O2")

# Compare GCC vs Clang for the same problem
two_sum = ds.filter(lambda r: r["problem_id"] == 1 and r["architecture"] == "x86-64" and r["optimization"] == "-O2")
for row in two_sum:
    print(f"--- {row['compiler']} ---")
    print(row["assembly"][:200])
    print()
```

## Sources

C solutions were collected from three open-source GitHub repositories of pure-C LeetCode solutions:

- [vli02/leetcode](https://github.com/vli02/leetcode) (383 solutions)
- [begeekmyfriend/leetcode](https://github.com/begeekmyfriend/leetcode) (215 solutions)
- [lennylxx/leetcode](https://github.com/lennylxx/leetcode) (159 solutions)

After deduplication, 441 unique problems remained. Solutions are self-contained C files using only the standard library (no C++ STL), producing clean and readable assembly output.