#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ home();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void display_state(int **state, int tick, int cols, int rows) {
  int i;
  int j;
  char filename[15];
  FILE *file;
  home();
  printf("Simulation tick %d\n", tick);
  sprintf(filename, "tick_%05d.pgm", tick);
  file = fopen(filename, "w");
  fprintf(file, "P2\n");
  fprintf(file, "# %s\n", filename);
  fprintf(file, "%d %d\n", cols, rows);
  fprintf(file, "2/n");
  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      putc("012"[state[i][j]], file);
      putc('\n', file);
    }
  }
  fclose(file);
}


x86 assembly (clang, -O0):
<display_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    $0x0,%al
call   1070 <home@plt>
mov    -0xc(%rbp),%esi
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
lea    -0x2b(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe6b(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x2b(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
lea    -0x2b(%rbp),%rdx
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1293 <display_state+0x123>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1280 <display_state+0x110>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x1c(%rbp),%rcx
movslq (%rax,%rcx,4),%rcx
lea    0xde4(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    -0x38(%rbp),%rsi
movsbl %al,%edi
call   1050 <putc@plt>
mov    -0x38(%rbp),%rsi
mov    $0xa,%edi
call   1050 <putc@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    122e <display_state+0xbe>
jmp    1285 <display_state+0x115>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    121b <display_state+0xab>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_state>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r13
xor    %eax,%eax
call   1070 <home@plt>
lea    0xe5a(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
lea    0xe60(%rip),%rsi
lea    0x19(%rsp),%r12
mov    %r12,%rdi
mov    %ebp,%edx
call   1090 <sprintf@plt>
lea    0xe58(%rip),%rsi
mov    %r12,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbp
lea    0xe48(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
lea    0xe33(%rip),%rsi
mov    %rbp,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xe25(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,0xc(%rsp)
mov    %ebx,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xe12(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   10a0 <fwrite@plt>
test   %r15d,%r15d
jle    12ae <display_state+0x12e>
mov    %r15d,%eax
mov    %rax,0x10(%rsp)
mov    0xc(%rsp),%r12d
xor    %ebx,%ebx
lea    0xde9(%rip),%r14
jmp    126b <display_state+0xeb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    0x10(%rsp),%rbx
je     12ae <display_state+0x12e>
cmpl   $0x0,0xc(%rsp)
jle    1260 <display_state+0xe0>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13,%rbx,8),%rax
movslq (%rax,%r15,4),%rax
movsbl (%rax,%r14,1),%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <putc@plt>
add    $0x1,%r15
cmp    %r15,%r12
jne    1280 <display_state+0x100>
jmp    1260 <display_state+0xe0>
mov    %rbp,%rdi
call   1030 <fclose@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [20 x i8] c"Simulation tick %d\0A\00"
@.str.1 = [14 x i8] c"tick_%05d.pgm\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"P2\0A\00"
@.str.4 = [6 x i8] c"# %s\0A\00"
@.str.5 = [7 x i8] c"%d %d\0A\00"
@.str.6 = [4 x i8] c"2/n\00"
@.str.7 = [4 x i8] c"012\00"
define void @display_state(i32** nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [15 x i8]
%6 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%7 = tail call i32 (...) @home()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %1)
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%10 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 3, i64 1, i32* %10)
%12 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%13 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %2, i32 %3)
%14 = call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i64 3, i64 1, i32* %10)
%15 = icmp sgt i32 %3, 0
br i1 %15, label %16, label %39
16: ; preds = %4
%17 = icmp sgt i32 %2, 0
%18 = zext i32 %3 to i64
%19 = zext i32 %2 to i64
br label %20
20: ; preds = %16, %36
%21 = phi i64 [ 0, %16 ], [ %37, %36 ]
br i1 %17, label %22, label %36
22: ; preds = %20
%23 = getelementptr inbounds i32*, i32** %0, i64 %21
br label %24
24: ; preds = %22, %24
%25 = phi i64 [ 0, %22 ], [ %34, %24 ]
%26 = load i32*, i32** %23
%27 = getelementptr inbounds i32, i32* %26, i64 %25
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [4 x i8], [4 x i8]* @.str.7, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = call i32 @putc(i8 signext %31, i32* %10)
%33 = call i32 @putc(i8 signext 10, i32* %10)
%34 = add nuw nsw i64 %25, 1
%35 = icmp eq i64 %34, %19
br i1 %35, label %36, label %24
36: ; preds = %24, %20
%37 = add nuw nsw i64 %21, 1
%38 = icmp eq i64 %37, %18
br i1 %38, label %39, label %20
39: ; preds = %36, %4
%40 = call i32 @fclose(i32* %10)
ret void
}