#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 */

/* Variables and functions */
char **allocate2D(int);
int /*<<< orphan*/ display(char **, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getchar();
char **initialize(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stderr;
int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ updateGrid(char **, char **, int);
int main(int argc, char **argv) {
  int n;
  int ngen;
  char **grid;
  char **newGrid;
  if (argc < 2) {
    fprintf(stderr, "usage: %s n [seed]\n", argv[0]);
    return 1;
  }
  n = strtol(argv[1], NULL, 10);
  int seed;
  if (3 == argc)
    seed = strtol(argv[2], NULL, 10);
  else
    seed = -1;
  grid = initialize(seed, n);
  newGrid = allocate2D(n);
  if (!grid || !newGrid) {
    fprintf(stderr, "couldn't allocate memory for grid\n");
    return 1;
  }
  display(grid, n);
  printf("enter number of generations: ");
  scanf("%d", &ngen);
  getchar();
  while (ngen > 0) {
    updateGrid(grid, newGrid, n);
    char **temp = grid;
    grid = newGrid;
    newGrid = temp;
    display(grid, n);
    getchar();
    ngen--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11da <main+0x4a>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1303 <main+0x173>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    $0x3,%eax
cmp    -0x8(%rbp),%eax
jne    121f <main+0x8f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,-0x2c(%rbp)
jmp    1226 <main+0x96>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <initialize@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <allocate2D@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1257 <main+0xc7>
cmpq   $0x0,-0x28(%rbp)
jne    127a <main+0xea>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1303 <main+0x173>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <display@plt>
lea    0xdaa(%rip),%rdi
call   1040 <printf@plt>
lea    0xdbc(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <scanf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
cmpl   $0x0,-0x18(%rbp)
jle    12fc <main+0x16c>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10a0 <updateGrid@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <display@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    12a9 <main+0x119>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11c3 <main+0x33>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe49(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12b2 <main+0x122>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,%r14d
mov    $0xffffffff,%edi
cmp    $0x3,%ebp
jne    11f4 <main+0x64>
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,%edi
mov    %r14d,%esi
call   1050 <initialize@plt>
mov    %rax,%r15
mov    %r14d,%edi
call   1080 <allocate2D@plt>
test   %r15,%r15
je     129b <main+0x10b>
mov    %rax,%r12
test   %rax,%rax
je     129b <main+0x10b>
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <display@plt>
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
lea    0xe1b(%rip),%rdi
lea    0xc(%rsp),%rsi
call   10b0 <scanf@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <getchar@plt>
cmpl   $0x0,0xc(%rsp)
jle    12b7 <main+0x127>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   10a0 <updateGrid@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1030 <display@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    0xc(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0xc(%rsp)
mov    %r15,%r12
mov    %rbx,%r15
cmp    $0x1,%eax
jg     1260 <main+0xd0>
jmp    12b7 <main+0x127>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s n [seed]\0A\00"
@.str.1 = [35 x i8] c"couldn't allocate memory for grid\0A\00"
@.str.2 = [30 x i8] c"enter number of generations: \00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 1, i32* %3
br label %72
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @strtol(i8* %23, i32* null, i32 10)
store i32 %24, i32* %6
%25 = load i32, i32* %4
%26 = icmp eq i32 3, %25
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @strtol(i8* %30, i32* null, i32 10)
store i32 %31, i32* %10
br label %33
32: ; preds = %20
store i32 -1, i32* %10
br label %33
33: ; preds = %32, %27
%34 = load i32, i32* %10
%35 = load i32, i32* %6
%36 = call i8** @initialize(i32 %34, i32 %35)
store i8** %36, i8*** %8
%37 = load i32, i32* %6
%38 = call i8** @allocate2D(i32 %37)
store i8** %38, i8*** %9
%39 = load i8**, i8*** %8
%40 = icmp ne i8** %39, null
br i1 %40, label %41, label %44
41: ; preds = %33
%42 = load i8**, i8*** %9
%43 = icmp ne i8** %42, null
br i1 %43, label %47, label %44
44: ; preds = %41, %33
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %72
47: ; preds = %41
%48 = load i8**, i8*** %8
%49 = load i32, i32* %6
%50 = call i32 @display(i8** %48, i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %7)
%53 = call i32 (...) @getchar()
br label %54
54: ; preds = %57, %47
%55 = load i32, i32* %7
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %71
57: ; preds = %54
%58 = load i8**, i8*** %8
%59 = load i8**, i8*** %9
%60 = load i32, i32* %6
%61 = call i32 @updateGrid(i8** %58, i8** %59, i32 %60)
%62 = load i8**, i8*** %8
store i8** %62, i8*** %11
%63 = load i8**, i8*** %9
store i8** %63, i8*** %8
%64 = load i8**, i8*** %11
store i8** %64, i8*** %9
%65 = load i8**, i8*** %8
%66 = load i32, i32* %6
%67 = call i32 @display(i8** %65, i32 %66)
%68 = call i32 (...) @getchar()
%69 = load i32, i32* %7
%70 = add nsw i32 %69, -1
store i32 %70, i32* %7
br label %54
71: ; preds = %54
store i32 0, i32* %3
br label %72
72: ; preds = %71, %44, %14
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s n [seed]\0A\00"
@.str.1 = [35 x i8] c"couldn't allocate memory for grid\0A\00"
@.str.2 = [30 x i8] c"enter number of generations: \00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %45
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @strtol(i8* nocapture %12, i32* null, i32 10)
%14 = icmp eq i32 %0, 3
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @strtol(i8* nocapture %17, i32* null, i32 10)
br label %19
19: ; preds = %10, %15
%20 = phi i32 [ %18, %15 ], [ -1, %10 ]
%21 = tail call i8** @initialize(i32 %20, i32 %13)
%22 = tail call i8** @allocate2D(i32 %13)
%23 = icmp ne i8** %21, null
%24 = icmp ne i8** %22, null
%25 = select i1 %23, i1 %24, i1 false
br i1 %25, label %29, label %26
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %45
29: ; preds = %19
%30 = tail call i32 @display(i8** %21, i32 %13)
%31 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%33 = call i32 (...) @getchar()
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %45
36: ; preds = %29, %36
%37 = phi i8** [ %38, %36 ], [ %22, %29 ]
%38 = phi i8** [ %37, %36 ], [ %21, %29 ]
%39 = call i32 @updateGrid(i8** %38, i8** %37, i32 %13)
%40 = call i32 @display(i8** %37, i32 %13)
%41 = call i32 (...) @getchar()
%42 = load i32, i32* %3
%43 = add nsw i32 %42, -1
store i32 %43, i32* %3
%44 = icmp sgt i32 %42, 1
br i1 %44, label %36, label %45
45: ; preds = %36, %29, %26, %6
%46 = phi i32 [ 1, %6 ], [ 1, %26 ], [ 0, %29 ], [ 0, %36 ]
ret i32 %46
}