#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, ...);
int write_pgm(char *filename, int width, int height, void *ptr) {
  int i;
  unsigned char *p = ptr;
  FILE *f;
  if ((f = fopen(filename, "wt")) == NULL)
    return -1;
  fprintf(f, "P2\n%d %d\n255\n", width, height);
  for (i = 0; i < width * height; i++)
    fprintf(f, "%d\n", *p++);
  fclose(f);
  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    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea7(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1178 <write_pgm+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <write_pgm+0xbb>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
cmp    %ecx,%eax
jge    11db <write_pgm+0xab>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movzbl (%rax),%edx
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1197 <write_pgm+0x67>
mov    -0x38(%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
push   %rax
mov    %rcx,%r13
mov    %edx,%r15d
mov    %esi,%r12d
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11b7 <write_pgm+0x87>
mov    %rax,%r14
lea    0xea4(%rip),%rsi
mov    %rax,%rdi
mov    %r12d,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
imul   %r12d,%r15d
test   %r15d,%r15d
jle    11ab <write_pgm+0x7b>
mov    %r15d,%ebx
lea    0xe8f(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x0(%r13,%rbp,1),%edx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%rbp
cmp    %ebp,%ebx
jne    1190 <write_pgm+0x60>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11bc <write_pgm+0x8c>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wt\00"
@.str.1 = [14 x i8] c"P2\0A%d %d\0A255\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @write_pgm(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %9
store i8* %13, i8** %11
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %12
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %42
18: ; preds = %4
%19 = load i32*, i32** %12
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21)
store i32 0, i32* %10
br label %23
23: ; preds = %36, %18
%24 = load i32, i32* %10
%25 = load i32, i32* %7
%26 = load i32, i32* %8
%27 = mul nsw i32 %25, %26
%28 = icmp slt i32 %24, %27
br i1 %28, label %29, label %39
29: ; preds = %23
%30 = load i32*, i32** %12
%31 = load i8*, i8** %11
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %11
%33 = load i8, i8* %31
%34 = zext i8 %33 to i32
%35 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %29
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
br label %23
39: ; preds = %23
%40 = load i32*, i32** %12
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %5
br label %42
42: ; preds = %39, %17
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@.str = [3 x i8] c"wt\00"
@.str.1 = [14 x i8] c"P2\0A%d %d\0A255\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @write_pgm(i8* nocapture %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %22, label %7
7: ; preds = %4
%8 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %2)
%9 = mul i32 %2, %1
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %7, %11
%12 = phi i8* [ %14, %11 ], [ %3, %7 ]
%13 = phi i32 [ %18, %11 ], [ 0, %7 ]
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = load i8, i8* %12
%16 = zext i8 %15 to i32
%17 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = add nuw nsw i32 %13, 1
%19 = icmp eq i32 %18, %9
br i1 %19, label %20, label %11
20: ; preds = %11, %7
%21 = tail call i32 @fclose(i32* %5)
br label %22
22: ; preds = %4, %20
%23 = phi i32 [ 0, %20 ], [ -1, %4 ]
ret i32 %23
}