LLVM IR: Advent of Code (2025) Day 1

Published At: 11/8/2026, 6:43:58 PM ⋅ 98 views

I've recently started learning LLVM IR for the fun of it. It's not the worst thing ever, though you can tell it wasn't made to be written by humans. It very much does feel like a more readable Assembly. I would've tried to do these AoCs in Assembly, but my last time working with Assembly (when I made a webserver) was quite painful so thought we'd move a level up.

I started off by writing a utility function to read individual lines from a file:

target triple = "arm64-apple-macosx26.0.0"

@filename = private constant [15 x i8] c"day1/input.txt\00"
@mode = private constant [2 x i8] c"r\00"
@str = private constant [12 x i8] c"Result: %d\0A\00"

declare ptr @fopen(ptr, ptr)
declare i64 @fread(ptr, i64, i64, ptr)
declare i32 @fseek(ptr, i64, i32)
declare i32 @fclose(ptr)
declare i32 @printf(ptr, ...)
declare ptr @malloc(i64)
declare void @free(ptr)
declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
declare i32 @atoi(ptr)

%State = type { ptr, i32 }
%Result = type { ptr, ptr, i32 }         ; The first pointer is a State*

define noundef ptr @readLine(ptr %0) {
1:
  %2 = icmp eq ptr %0, null
  br i1 %2, label %3, label %8

3:
  %4 = tail call ptr @malloc(i64 noundef 12)     ; Pray this works, because there's no fallback
  %5 = call ptr @fopen(ptr @filename, ptr @mode) ; Same prayer here
  %6 = getelementptr inbounds i8, ptr %4, i64 0
  store ptr %5, ptr %6, align 8
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  store i32 0, ptr %7, align 4
  br label %8

8:
  %9 = phi ptr [ %0, %1 ], [ %4, %3 ]
  %10 = getelementptr inbounds i8, ptr %9, i64 0
  %11 = getelementptr inbounds i8, ptr %9, i64 8
  %12 = load i32, ptr %11, align 4
  %13 = sext i32 %12 to i64
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %15, label %17

15:
  %16 = add i64 %13, 1
  br label %17

17:
  %18 = phi i64 [ %16, %15 ], [ %13, %8 ]
  %19 = load ptr, ptr %10, align 8
  %20 = tail call i32 @fseek(ptr %19, i64 %18, i32 0)
  %21 = alloca [100 x i8]
  %22 = getelementptr [100 x i8], ptr %21, i64 0, i64 0
  %23 = tail call i64 @fread(ptr %22, i64 1, i64 100, ptr %19)
  %24 = icmp eq i64 %23, 0
  br i1 %24, label %44, label %25

25:
  %26 = alloca i32
  store i32 0, ptr %26
  br label %27

27:
  %28 = load i32, ptr %26
  %29 = getelementptr i8, ptr %22, i32 %28
  %30 = load i8, ptr %29
  %31 = icmp eq i8 %30, 10
  br i1 %31, label %34, label %32

32:
  %33 = add i32 %28, 1
  store i32 %33, ptr %26
  br label %27

34:
  %35 = icmp ne i32 %28, 0
  br i1 %35, label %36, label %44

36:
  %37 = sext i32 %28 to i64
  %38 = call ptr @malloc(i64 %37)
  call void @llvm.memcpy.p0.p0.i64(ptr %38, ptr %22, i64 %37, i1 false)
  %39 = call ptr @malloc(i64 20)
  store i32 %28, ptr %11
  store ptr %9, ptr %39
  %40 = getelementptr i8, ptr %39, i64 8
  store ptr %38, ptr %40
  %41 = getelementptr i8, ptr %39, i64 16
  store i32 %28, ptr %41
  %42 = trunc i64 %18 to i32
  %43 = add i32 %28, %42
  store i32 %43, ptr %11
  ret ptr %39

44:
  call void @free(ptr %9)
  ret ptr null
}

Putting aside the somewhat obfuscated SSA names, the operation is pretty simple and not very relevant to the actual solution. It uses a simple state machine to repeatedly read lines from a file. It's definitely inefficient, and definitely leaves room for the caller to screw up: the function is intended to be called in a loop, as follows:

#include <stdio.h>

extern Result* readLine(State* state);

typedef struct {
    FILE* fp;
    int offset;
} State;

typedef struct {
    State* state;
    char* line;
    int length;
} Result;

int main() {
    State* state = NULL;
    for (Result* res = readLine(state); res != NULL; res = readLine(state)) {
        /* Do operations here */
        state = res->state;
        free(res->line);
        free(res);
    }
}

As you can see, there are some pretty big expectations from the caller to free the allocated line as well as the result structure. Whatever, it works. A better solution would involve just passing in pointers to the readLine function so that it doesn't have to repeatedly reallocate memory but that wasn't the first thing I thought of. We're going ahead with this.

For the actual solution, here's part 1:

; Just need this line here for syntax highlighting to work
target triple = "arm64-apple-macosx26.0.0"

@filename = private constant [15 x i8] c"day1/input.txt\00"
@mode = private constant [2 x i8] c"r\00"
@str = private constant [12 x i8] c"Result: %d\0A\00"

declare ptr @fopen(ptr, ptr)
declare i64 @fread(ptr, i64, i64, ptr)
declare i32 @fseek(ptr, i64, i32)
declare i32 @fclose(ptr)
declare i32 @printf(ptr, ...)
declare ptr @malloc(i64)
declare void @free(ptr)
declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
declare i32 @atoi(ptr)

define noundef i32 @part1() {
1:
  %2 = alloca i32
  store i32 50, ptr %2
  %3 = alloca i32
  store i32 0, ptr %3
  %4 = alloca ptr
  store ptr null, ptr %4
  br label %5

5:
  %6 = load ptr, ptr %4
  %7 = call ptr @readLine(ptr %6)
  %8 = icmp eq ptr %7, null
  br i1 %8, label %35, label %9

9:
  %10 = getelementptr i8, ptr %7, i64 0
  %11 = load ptr, ptr %10
  store ptr %11, ptr %4
  %12 = getelementptr i8, ptr %7, i64 8
  %13 = load ptr, ptr %12
  %14 = load i8, ptr %13
  %15 = getelementptr i8, ptr %13, i61 1
  %16 = call i32 @atoi(ptr %15)
  %17 = icmp eq i8 %14, 76
  br i1 %17, label %18, label %20

18:
  %19 = mul i32 %16, -1
  br label %20

20:
  %21 = phi i32 [ %19, %18 ], [ %16, %9 ]
  %22 = load i32, ptr %2
  %23 = add i32 %22, %21
  %24 = srem i32 %23, 100
  %25 = icmp slt i32 %24, 0
  br i1 %25, label %26, label %28

26:
  %27 = add i32 %24, 100
  br label %28

28:
  %29 = phi i32 [ %27, %26 ], [ %24, %20 ]
  store i32 %29, ptr %2
  %30 = icmp eq i32 %24, 0
  br i1 %30, label %31, label %34

31:
  %32 = load i32, ptr %3
  %33 = add i32 %32, 1
  store i32 %33, ptr %3
  br label %34

34:
  tail call void @free(ptr %13)
  tail call void @free(ptr %7)
  br label %5

35:
  %36 = load ptr, ptr %4
  tail call void @fclose(ptr %36)
  tail call void @free(ptr %7)
  %37 = load i32, ptr %3
  ret i32 %37
}

The problem wasn't very difficult, so neither is the code. Again, sorry for the obfuscated SSAs, I just like how straight numbers look. I write the code in a more readable way first, with descriptive SSA and then switch them out for numbers after I the program works. Here's a block-by-block summary of the program:

  • %1: Allocate variables to hold pointer, counter, and state (for the loop)
  • %5: Performs the loop condition, jumps to %35 (end) if it fails, and %9 (loop body) if it succeeds.
  • %9: Loads the first character of the string into %14, and uses atoi from stdlib.h to convert the rest of the string into an int, and stores it in %16. The integer is negated if the character in %14 is an 'L' (ASCII 76)
  • %18: Negates %16
  • %20: Checks if position + delta (the next position) is less than 0
  • %26: Corrects negative future position by adding 100
  • %28: Stores the next position into the position variable, checks if the position is 0.
  • %31: Increments the counter by 1 if the position is 0
  • %34: Cleanup allocations, restart loop
  • %35: Final cleanup, return result

Part 2 was a bit more difficult so I first wrote it in C and then pretty much translated it to LLVM IR:

define noundef i32 @part2() {
1:
  %2 = alloca i32
  store i32 50, ptr %2
  %3 = alloca i32
  store i32 0, ptr %3
  %4 = alloca ptr
  store ptr null, ptr %4
  br label %5

5:
  %6 = load ptr, ptr %4
  %7 = call ptr @readLine(ptr %6)
  %8 = icmp eq ptr %7, null
  br i1 %8, label %42, label %9

9:
  %10 = getelementptr i8, ptr %7, i64 0
  %11 = load ptr, ptr %7
  store ptr %11, ptr %4
  %12 = getelementptr i8, ptr %7, i64 8
  %13 = load ptr, ptr %12
  %14 = load i8, ptr %13
  %15 = getelementptr i8, ptr %13, i64 1
  %16 = call i32 @atoi(ptr %15)
  %17 = sdiv i32 %16, 100
  %18 = srem i32 %16, 100
  %19 = load i32, ptr %3
  %20 = add i32 %19, %17
  store i32 %20, ptr %3
  %21 = icmp eq i8 %14, 76
  br i1 %21, label %22, label %24

22:
  %23 = mul i32 %18, -1
  br label %24

24:
  %25 = phi i32 [ %23, %22 ], [ %18, %9 ]
  %26 = load i32, ptr %2
  %27 = add i32 %26, %25
  %28 = icmp ne i32 %26, 0
  %29 = icmp eq i1 %21, false
  %30 = icmp sle i32 %27, 0
  %31 = icmp sge i32 %27, 100
  %32 = and i1 %21, %30
  %33 = and i1 %29, %31
  %34 = or i1 %32, %33
  %35 = and i1 %28, %34
  br i1 %35, label %36, label %39

36:
  %37 = load i32, ptr %3
  %38 = add i32 %37, 1
  store i32 %38, ptr %3
  br label %39

39:
  %40 = add i32 %27, 100
  %41 = srem i32 %40, 100
  store i32 %41, ptr %2
  tail call void @free(ptr %13)
  tail call void @free(ptr %7)
  br label %5

42:
  %43 = load i32, ptr %3
  ret i32 %43
}

Most of the code in this is pretty much the same as part 1, since it takes a good amount of code just to implement the loops and stuff. The actually different logic is pretty basic, we divide the delta by 100 to get an initial "number of 0s passed" value, and use modulo to get a next position, and use that to handle a couple edge cases (going left/right over 0). The code isn't really different enough that I'd want to write a whole new block-by-block summary, so just read the code I guess?

P016R15