#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 */
int /*<<< orphan*/ EXIT_FAILURE;
unsigned int cells_alive(unsigned char *, unsigned int, unsigned int);
int /*<<< orphan*/ evolve(unsigned char *, unsigned char *, unsigned int,
                          unsigned int, int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, size_t);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ swap(unsigned char **, unsigned char **);
unsigned int gol(unsigned char *grid, unsigned int dim_x, unsigned int dim_y,
                 unsigned int time_steps, unsigned int num_threads) {
  unsigned char *grid_in, *grid_out, *grid_tmp;
  size_t size = sizeof(unsigned char) * dim_x * dim_y;
  grid_tmp = malloc(size);
  if (grid_tmp == NULL)
    exit(EXIT_FAILURE);
  memset(grid_tmp, 0, size);
  grid_in = grid;
  grid_out = grid_tmp;
  for (int t = 0; t < time_steps; ++t) {
    for (int y = 0; y < dim_y; ++y) {
      for (int x = 0; x < dim_x; ++x) {
        evolve(grid_in, grid_out, dim_x, dim_y, x, y);
      }
    }
    swap(&grid_in, &grid_out);
  }
  if (grid != grid_in)
    memcpy(grid, grid_in, size);
  free(grid_tmp);
  return cells_alive(grid, dim_x, dim_y);
}


x86 assembly (clang, -O0):
<gol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x0,%rax
mov    -0x10(%rbp),%ecx
imul   %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d1 <gol+0x51>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1280 <gol+0x100>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    1265 <gol+0xe5>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1252 <gol+0xd2>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x44(%rbp),%r8d
mov    -0x40(%rbp),%r9d
call   1070 <evolve@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    121d <gol+0x9d>
jmp    1257 <gol+0xd7>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    120a <gol+0x8a>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <swap@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11f7 <gol+0x77>
mov    -0x8(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     129f <gol+0x11f>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1090 <cells_alive@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gol>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r13d
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,0x8(%rsp)
mov    %esi,%eax
mov    %edx,%r15d
imul   %rax,%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     1283 <gol+0x103>
xor    %r12d,%r12d
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <memset@plt>
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
mov    %r14,0x20(%rsp)
mov    %r14,0x10(%rsp)
mov    %rax,%r14
test   %r13d,%r13d
je     125e <gol+0xde>
mov    %r15,0x18(%rsp)
jmp    1206 <gol+0x86>
nopl   (%rax)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   10a0 <swap@plt>
add    $0x1,%r12d
cmp    %r13d,%r12d
je     1243 <gol+0xc3>
test   %ebx,%ebx
je     11f0 <gol+0x70>
xor    %r15d,%r15d
jmp    1219 <gol+0x99>
nop
add    $0x1,%r15d
cmp    %ebx,%r15d
je     11f0 <gol+0x70>
test   %ebp,%ebp
je     1210 <gol+0x90>
xor    %r14d,%r14d
mov    (%rsp),%rdi
mov    0x10(%rsp),%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %r14d,%r8d
mov    %r15d,%r9d
call   1070 <evolve@plt>
add    $0x1,%r14d
cmp    %r14d,%ebp
jne    1220 <gol+0xa0>
jmp    1210 <gol+0x90>
mov    (%rsp),%rsi
mov    0x8(%rsp),%r14
cmp    %r14,%rsi
mov    0x18(%rsp),%rdx
je     125e <gol+0xde>
mov    %r14,%rdi
call   1050 <memcpy@plt>
mov    0x20(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1090 <cells_alive@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
define i32 @gol(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%18 = load i32, i32* %7
%19 = zext i32 %18 to i64
%20 = mul i64 1, %19
%21 = load i32, i32* %8
%22 = zext i32 %21 to i64
%23 = mul i64 %20, %22
store i64 %23, i64* %14
%24 = load i64, i64* %14
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %13
%26 = load i8*, i8** %13
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %5
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %5
%32 = load i8*, i8** %13
%33 = load i64, i64* %14
%34 = call i32 @memset(i8* %32, i32 0, i64 %33)
%35 = load i8*, i8** %6
store i8* %35, i8** %11
%36 = load i8*, i8** %13
store i8* %36, i8** %12
store i32 0, i32* %15
br label %37
37: ; preds = %68, %31
%38 = load i32, i32* %15
%39 = load i32, i32* %9
%40 = icmp ult i32 %38, %39
br i1 %40, label %41, label %71
41: ; preds = %37
store i32 0, i32* %16
br label %42
42: ; preds = %63, %41
%43 = load i32, i32* %16
%44 = load i32, i32* %8
%45 = icmp ult i32 %43, %44
br i1 %45, label %46, label %66
46: ; preds = %42
store i32 0, i32* %17
br label %47
47: ; preds = %59, %46
%48 = load i32, i32* %17
%49 = load i32, i32* %7
%50 = icmp ult i32 %48, %49
br i1 %50, label %51, label %62
51: ; preds = %47
%52 = load i8*, i8** %11
%53 = load i8*, i8** %12
%54 = load i32, i32* %7
%55 = load i32, i32* %8
%56 = load i32, i32* %17
%57 = load i32, i32* %16
%58 = call i32 @evolve(i8* %52, i8* %53, i32 %54, i32 %55, i32 %56, i32 %57)
br label %59
59: ; preds = %51
%60 = load i32, i32* %17
%61 = add nsw i32 %60, 1
store i32 %61, i32* %17
br label %47
62: ; preds = %47
br label %63
63: ; preds = %62
%64 = load i32, i32* %16
%65 = add nsw i32 %64, 1
store i32 %65, i32* %16
br label %42
66: ; preds = %42
%67 = call i32 @swap(i8** %11, i8** %12)
br label %68
68: ; preds = %66
%69 = load i32, i32* %15
%70 = add nsw i32 %69, 1
store i32 %70, i32* %15
br label %37
71: ; preds = %37
%72 = load i8*, i8** %6
%73 = load i8*, i8** %11
%74 = icmp ne i8* %72, %73
br i1 %74, label %75, label %80
75: ; preds = %71
%76 = load i8*, i8** %6
%77 = load i8*, i8** %11
%78 = load i64, i64* %14
%79 = call i32 @memcpy(i8* %76, i8* %77, i64 %78)
br label %80
80: ; preds = %75, %71
%81 = load i8*, i8** %13
%82 = call i32 @free(i8* %81)
%83 = load i8*, i8** %6
%84 = load i32, i32* %7
%85 = load i32, i32* %8
%86 = call i32 @cells_alive(i8* %83, i32 %84, i32 %85)
ret i32 %86
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
define i32 @gol(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = bitcast i8** %6 to i8*
%9 = bitcast i8** %7 to i8*
%10 = zext i32 %1 to i64
%11 = zext i32 %2 to i64
%12 = mul nuw i64 %11, %10
%13 = tail call i8* @malloc(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %5
%19 = tail call i32 @memset(i8* %13, i32 0, i64 %12)
store i8* %0, i8** %6
store i8* %13, i8** %7
%20 = icmp eq i32 %3, 0
br i1 %20, label %47, label %21
21: ; preds = %18
%22 = icmp eq i32 %2, 0
%23 = icmp eq i32 %1, 0
br label %24
24: ; preds = %21, %31
%25 = phi i32 [ 0, %21 ], [ %33, %31 ]
br i1 %22, label %31, label %29
26: ; preds = %31
%27 = load i8*, i8** %6
%28 = icmp eq i8* %27, %0
br i1 %28, label %47, label %45
29: ; preds = %24, %35
%30 = phi i32 [ %36, %35 ], [ 0, %24 ]
br i1 %23, label %35, label %38
31: ; preds = %35, %24
%32 = call i32 @swap(i8** %6, i8** %7)
%33 = add nuw i32 %25, 1
%34 = icmp eq i32 %33, %3
br i1 %34, label %26, label %24
35: ; preds = %38, %29
%36 = add nuw i32 %30, 1
%37 = icmp eq i32 %36, %2
br i1 %37, label %31, label %29
38: ; preds = %29, %38
%39 = phi i32 [ %43, %38 ], [ 0, %29 ]
%40 = load i8*, i8** %6
%41 = load i8*, i8** %7
%42 = call i32 @evolve(i8* %40, i8* %41, i32 %1, i32 %2, i32 %39, i32 %30)
%43 = add nuw i32 %39, 1
%44 = icmp eq i32 %43, %1
br i1 %44, label %35, label %38
45: ; preds = %26
%46 = call i32 @memcpy(i8* %0, i8* %27, i64 %12)
br label %47
47: ; preds = %18, %45, %26
%48 = call i32 @free(i8* %13)
%49 = call i32 @cells_alive(i8* %0, i32 %1, i32 %2)
ret i32 %49
}