#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*/ printf(char *, char *);
int write_pgm(char *path, int height, int width, int grey_levels,
              unsigned char data[]) {
  FILE *file = fopen(path, "w");
  if (!file) {
    printf("Unable to open %s.\n", path);
    return -1;
  }
  fprintf(file, "%s\n%d %d\n%d\n", "P2", width, height, grey_levels);
  int i, j;
  for (j = 0; j < height; j++) {
    for (i = 0; i < width; i++) {
      fprintf(file, "%d ", data[j * width + i]);
    }
    fprintf(file, "\n");
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<write_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1194 <write_pgm+0x54>
mov    -0x10(%rbp),%rsi
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <write_pgm+0x103>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
lea    0xe6c(%rip),%rsi
lea    0xe72(%rip),%rdx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1233 <write_pgm+0xf3>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1213 <write_pgm+0xd3>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    -0x34(%rbp),%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d2 <write_pgm+0x92>
mov    -0x30(%rbp),%rdi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11bf <write_pgm+0x7f>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%rbx
mov    %ecx,%r13d
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%r15
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1239 <write_pgm+0xe9>
mov    %rax,%rbp
lea    0xe8a(%rip),%rsi
lea    0xe90(%rip),%rdx
mov    %rax,%rdi
mov    %r14d,0x4(%rsp)
mov    %r14d,%ecx
mov    %r12d,%r8d
mov    %r13d,%r9d
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %r12d,%r12d
jle    122d <write_pgm+0xdd>
mov    0x4(%rsp),%eax
movslq %eax,%rcx
mov    %rcx,0x10(%rsp)
mov    %r12d,%ecx
mov    %rcx,0x8(%rsp)
mov    %eax,%r14d
lea    0xe58(%rip),%r12
xor    %r13d,%r13d
jmp    11fd <write_pgm+0xad>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
add    $0x1,%r13
add    0x10(%rsp),%rbx
cmp    0x8(%rsp),%r13
je     122d <write_pgm+0xdd>
cmpl   $0x0,0x4(%rsp)
jle    11e0 <write_pgm+0x90>
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
movzbl (%rbx,%r15,1),%edx
mov    %rbp,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%r15
cmp    %r15,%r14
jne    1210 <write_pgm+0xc0>
jmp    11e0 <write_pgm+0x90>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    124d <write_pgm+0xfd>
lea    0xdc2(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [20 x i8] c"Unable to open %s.\0A\00"
@.str.2 = [13 x i8] c"%s\0A%d %d\0A%d\0A\00"
@.str.3 = [3 x i8] c"P2\00"
@.str.4 = [4 x i8] c"%d \00"
define i32 @write_pgm(i8* %0, i32 %1, i32 %2, i32 %3, i8* nocapture %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %37
10: ; preds = %5
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %2, i32 %1, i32 %3)
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %35
13: ; preds = %10
%14 = icmp sgt i32 %2, 0
%15 = sext i32 %2 to i64
%16 = zext i32 %1 to i64
%17 = zext i32 %2 to i64
br label %18
18: ; preds = %13, %31
%19 = phi i64 [ 0, %13 ], [ %33, %31 ]
br i1 %14, label %20, label %31
20: ; preds = %18
%21 = mul nsw i64 %19, %15
br label %22
22: ; preds = %20, %22
%23 = phi i64 [ 0, %20 ], [ %29, %22 ]
%24 = add nsw i64 %23, %21
%25 = getelementptr inbounds i8, i8* %4, i64 %24
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = add nuw nsw i64 %23, 1
%30 = icmp eq i64 %29, %17
br i1 %30, label %31, label %22
31: ; preds = %22, %18
%32 = tail call i32 @fputc(i32 10, i32* %6)
%33 = add nuw nsw i64 %19, 1
%34 = icmp eq i64 %33, %16
br i1 %34, label %35, label %18
35: ; preds = %31, %10
%36 = tail call i32 @fclose(i32* %6)
br label %37
37: ; preds = %35, %8
%38 = phi i32 [ 0, %35 ], [ -1, %8 ]
ret i32 %38
}