#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint8_t;
typedef int uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
void print_world_pgm(const uint64_t *world, uint64_t iterations) {
  char path[256];
  FILE *f;
  uint8_t s;
  uint64_t b, c, x1, x2, y;
  sprintf(path, "langton_%04luB.pgm", iterations);
  fprintf(stderr, "%s\n", path);
  f = fopen(path, "w");
  fprintf(f, "P2\n%d %d\n4\n", 32, 32);
  c = 0;
  for (y = 0; y < 32; ++y) {
    for (x1 = 0; x1 < 32 / 32; ++x1) {
      b = world[c++];
      for (x2 = 0; x2 < 32; ++x2) {
        s = (b >> (2 * x2)) & 3;
        fprintf(f, "%2d", s);
      }
    }
    fprintf(f, "\n");
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<print_world_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe9d(%rip),%rsi
call   1060 <sprintf@plt>
mov    0x2e71(%rip),%rax
movslq (%rax),%rdi
lea    -0x110(%rbp),%rdx
lea    0xe93(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rdi
lea    0xe6a(%rip),%rsi
mov    $0x20,%ecx
mov    %ecx,%edx
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x124(%rbp)
movl   $0x0,-0x130(%rbp)
cmpl   $0x20,-0x130(%rbp)
jge    12b8 <print_world_pgm+0x178>
movl   $0x0,-0x128(%rbp)
cmpl   $0x1,-0x128(%rbp)
jge    128f <print_world_pgm+0x14f>
mov    -0x8(%rbp),%rax
mov    -0x124(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x124(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x120(%rbp)
movl   $0x0,-0x12c(%rbp)
cmpl   $0x20,-0x12c(%rbp)
jge    1276 <print_world_pgm+0x136>
mov    -0x120(%rbp),%eax
mov    -0x12c(%rbp),%ecx
shl    $0x1,%ecx
sar    %cl,%eax
and    $0x3,%eax
mov    %eax,-0x11c(%rbp)
mov    -0x118(%rbp),%rdi
mov    -0x11c(%rbp),%edx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x12c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x12c(%rbp)
jmp    1220 <print_world_pgm+0xe0>
jmp    127b <print_world_pgm+0x13b>
mov    -0x128(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x128(%rbp)
jmp    11e8 <print_world_pgm+0xa8>
mov    -0x118(%rbp),%rdi
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x130(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x130(%rbp)
jmp    11d1 <print_world_pgm+0x91>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_world_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %esi,%edx
mov    %rdi,%r14
lea    0xe93(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e61(%rip),%rax
movslq (%rax),%rdi
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%r13
lea    0xe6d(%rip),%rsi
mov    %rax,%rdi
mov    $0x20,%edx
mov    $0x20,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe5e(%rip),%r15
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    (%r14,%r12,4),%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edx
mov    %ebp,%ecx
sar    %cl,%edx
and    $0x3,%edx
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x2,%ebp
cmp    $0x40,%ebp
jne    11e0 <print_world_pgm+0x90>
add    $0x1,%r12
mov    $0xa,%edi
mov    %r13,%rsi
call   1040 <fputc@plt>
cmp    $0x20,%r12
jne    11d0 <print_world_pgm+0x80>
mov    %r13,%rdi
call   1030 <fclose@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"langton_%04luB.pgm\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [12 x i8] c"P2\0A%d %d\0A4\0A\00"
@.str.4 = [4 x i8] c"%2d\00"
@.str.5 = [2 x i8] c"\0A\00"
define void @print_world_pgm(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%14 = load i32, i32* %4
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* @stderr
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i32*
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 32, i32 32)
store i32 0, i32* %9
store i32 0, i32* %12
br label %25
25: ; preds = %61, %2
%26 = load i32, i32* %12
%27 = icmp slt i32 %26, 32
br i1 %27, label %28, label %64
28: ; preds = %25
store i32 0, i32* %10
br label %29
29: ; preds = %55, %28
%30 = load i32, i32* %10
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %58
32: ; preds = %29
%33 = load i32*, i32** %3
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
%38 = load i32, i32* %37
store i32 %38, i32* %8
store i32 0, i32* %11
br label %39
39: ; preds = %51, %32
%40 = load i32, i32* %11
%41 = icmp slt i32 %40, 32
br i1 %41, label %42, label %54
42: ; preds = %39
%43 = load i32, i32* %8
%44 = load i32, i32* %11
%45 = mul nsw i32 2, %44
%46 = ashr i32 %43, %45
%47 = and i32 %46, 3
store i32 %47, i32* %7
%48 = load i32*, i32** %6
%49 = load i32, i32* %7
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %48, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %42
%52 = load i32, i32* %11
%53 = add nsw i32 %52, 1
store i32 %53, i32* %11
br label %39
54: ; preds = %39
br label %55
55: ; preds = %54
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %29
58: ; preds = %29
%59 = load i32*, i32** %6
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %58
%62 = load i32, i32* %12
%63 = add nsw i32 %62, 1
store i32 %63, i32* %12
br label %25
64: ; preds = %25
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"langton_%04luB.pgm\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [12 x i8] c"P2\0A%d %d\0A4\0A\00"
@.str.4 = [4 x i8] c"%2d\00"
define void @print_world_pgm(i32* nocapture %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = load i32, i32* @stderr
%7 = sext i32 %6 to i64
%8 = inttoptr i64 %7 to i32*
%9 = call i32 (i32*, i8*, ...) @fprintf(i32* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%10 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 32, i32 32)
br label %12
12: ; preds = %2, %24
%13 = phi i64 [ 0, %2 ], [ %25, %24 ]
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
br label %16
16: ; preds = %12, %16
%17 = phi i32 [ 0, %12 ], [ %22, %16 ]
%18 = shl nuw nsw i32 %17, 1
%19 = ashr i32 %15, %18
%20 = and i32 %19, 3
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = add nuw nsw i32 %17, 1
%23 = icmp eq i32 %22, 32
br i1 %23, label %24, label %16
24: ; preds = %16
%25 = add nuw nsw i64 %13, 1
%26 = call i32 @fputc(i32 10, i32* %10)
%27 = icmp eq i64 %25, 32
br i1 %27, label %28, label %12
28: ; preds = %24
%29 = call i32 @fclose(i32* %10)
ret void
}