#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, ...);
int /*<<< orphan*/ printf(char *);
void write_image(int image[827][943], char file_name[], int h, int w,
                 int levels) {
  FILE *fout;
  int i, j;
  fout = fopen("logo2.pgm", "w");
  if (fout == NULL) {
    printf("file opening error\n");
    exit(-1);
  }
  fprintf(fout, "P2\n# test \n%d %d\n%d\n", w, h, levels);
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      fprintf(fout, "%d\n", image[i][j]);
  fclose(fout);
  return;
}


x86 assembly (clang, -O0):
<write_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
lea    0xe8f(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a2 <write_image+0x52>
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    0xe69(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1230 <write_image+0xe0>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    121d <write_image+0xcd>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
imul   $0xebc,%rcx,%rcx
add    %rcx,%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11d8 <write_image+0x88>
jmp    1222 <write_image+0xd2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11c5 <write_image+0x75>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r12d
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rdi,%rbx
lea    0xe90(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1222 <write_image+0xd2>
mov    %rax,%r13
lea    0xe7d(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,0xc(%rsp)
mov    %ebp,%edx
mov    %r15d,%ecx
mov    %r12d,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r15d,%r15d
jle    120c <write_image+0xbc>
mov    %r15d,%eax
mov    %rax,0x10(%rsp)
mov    0xc(%rsp),%ebp
lea    0xe60(%rip),%r15
xor    %r14d,%r14d
jmp    11e2 <write_image+0x92>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r14
add    $0xebc,%rbx
cmp    0x10(%rsp),%r14
je     120c <write_image+0xbc>
cmpl   $0x0,0xc(%rsp)
jle    11d0 <write_image+0x80>
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    (%rbx,%r12,4),%edx
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%rbp
jne    11f0 <write_image+0xa0>
jmp    11d0 <write_image+0x80>
mov    %r13,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"logo2.pgm\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [20 x i8] c"file opening error\0A\00"
@.str.3 = [21 x i8] c"P2\0A# test \0A%d %d\0A%d\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @write_image([943 x i32]* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca [943 x i32]*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store [943 x i32]* %0, [943 x i32]** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%14 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %11
%15 = load i32*, i32** %11
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %5
%21 = load i32*, i32** %11
%22 = load i32, i32* %9
%23 = load i32, i32* %8
%24 = load i32, i32* %10
%25 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %22, i32 %23, i32 %24)
store i32 0, i32* %12
br label %26
26: ; preds = %50, %20
%27 = load i32, i32* %12
%28 = load i32, i32* %8
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %53
30: ; preds = %26
store i32 0, i32* %13
br label %31
31: ; preds = %46, %30
%32 = load i32, i32* %13
%33 = load i32, i32* %9
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %49
35: ; preds = %31
%36 = load i32*, i32** %11
%37 = load [943 x i32]*, [943 x i32]** %6
%38 = load i32, i32* %12
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [943 x i32], [943 x i32]* %37, i64 %39
%41 = load i32, i32* %13
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [943 x i32], [943 x i32]* %40, i64 0, i64 %42
%44 = load i32, i32* %43
%45 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %35
%47 = load i32, i32* %13
%48 = add nsw i32 %47, 1
store i32 %48, i32* %13
br label %31
49: ; preds = %31
br label %50
50: ; preds = %49
%51 = load i32, i32* %12
%52 = add nsw i32 %51, 1
store i32 %52, i32* %12
br label %26
53: ; preds = %26
%54 = load i32*, i32** %11
%55 = call i32 @fclose(i32* %54)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"logo2.pgm\00"
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [21 x i8] c"P2\0A# test \0A%d %d\0A%d\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@str = [19 x i8] c"file opening error\00"
define void @write_image([943 x i32]* nocapture %0, i8* nocapture readnone %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %5
%12 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %3, i32 %2, i32 %4)
%13 = icmp sgt i32 %2, 0
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = icmp sgt i32 %3, 0
%16 = zext i32 %2 to i64
%17 = zext i32 %3 to i64
br label %18
18: ; preds = %14, %27
%19 = phi i64 [ 0, %14 ], [ %28, %27 ]
br i1 %15, label %20, label %27
20: ; preds = %18, %20
%21 = phi i64 [ %25, %20 ], [ 0, %18 ]
%22 = getelementptr inbounds [943 x i32], [943 x i32]* %0, i64 %19, i64 %21
%23 = load i32, i32* %22
%24 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = add nuw nsw i64 %21, 1
%26 = icmp eq i64 %25, %17
br i1 %26, label %27, label %20
27: ; preds = %20, %18
%28 = add nuw nsw i64 %19, 1
%29 = icmp eq i64 %28, %16
br i1 %29, label %30, label %18
30: ; preds = %27, %11
%31 = tail call i32 @fclose(i32* %6)
ret void
}