#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*/ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ die(char *, char const *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
scalar_t__ fwrite(int /*<<< orphan*/, int, int, int /*<<< orphan*/ *);
int save_ppm(const char *filename, uint32_t *pixels) {
  FILE *f;
  int y, x;
  f = fopen(filename, "wb");
  if (f == NULL)
    die("Can't open '%s'", filename);
  fprintf(f, "P6\n%d %d\n255\n", 640, 480);
  for (y = 0; y < 480; y++) {
    for (x = 0; x < 640; x++)
      if (fwrite(pixels++, 3, 1, f) == 0) {
        die("Can't read '%s'.  Has PPM got size %d x %d?", filename, 640, 480);
        fclose(f);
        return 0;
      }
  }
  fclose(f);
  return 1;
}


x86 assembly (clang, -O0):
<save_ppm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <save_ppm+0x41>
mov    -0x10(%rbp),%rsi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1060 <die@plt>
mov    -0x20(%rbp),%rdi
lea    0xe77(%rip),%rsi
mov    $0x280,%edx
mov    $0x1e0,%ecx
call   1040 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x1e0,-0x24(%rbp)
jge    1258 <save_ppm+0x108>
movl   $0x0,-0x28(%rbp)
cmpl   $0x280,-0x28(%rbp)
jge    1245 <save_ppm+0xf5>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x18(%rbp)
mov    %eax,%edi
mov    -0x20(%rbp),%rcx
mov    $0x3,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x0,%rax
jne    1232 <save_ppm+0xe2>
mov    -0x10(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x280,%edx
mov    $0x1e0,%ecx
mov    $0x0,%al
call   1060 <die@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1268 <save_ppm+0x118>
jmp    1237 <save_ppm+0xe7>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11c6 <save_ppm+0x76>
jmp    124a <save_ppm+0xfa>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b2 <save_ppm+0x62>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_ppm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe98(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1186 <save_ppm+0x36>
lea    0xe87(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <die@plt>
lea    0xe86(%rip),%rsi
mov    %r13,%rdi
mov    $0x280,%edx
mov    $0x1e0,%ecx
call   1040 <fprintf@plt>
xor    %r12d,%r12d
mov    %r15d,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
test   %rax,%rax
je     11ef <save_ppm+0x9f>
add    $0xfffffffffffffffc,%rbx
add    $0x4,%ebp
cmp    $0xfffff600,%ebx
jne    11b0 <save_ppm+0x60>
add    $0x1,%r12d
sub    %rbx,%r15
cmp    $0x1e0,%r12d
jne    11a2 <save_ppm+0x52>
mov    $0x1,%ebp
jmp    120c <save_ppm+0xbc>
lea    0xe2b(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
mov    $0x280,%edx
mov    $0x1e0,%ecx
xor    %eax,%eax
call   1060 <die@plt>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %ebp,%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"wb\00"
@.str.1 = [16 x i8] c"Can't open '%s'\00"
@.str.2 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
@.str.3 = [44 x i8] c"Can't read '%s'. Has PPM got size %d x %d?\00"
define i32 @save_ppm(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %2
%17 = load i32*, i32** %6
%18 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 640, i32 480)
store i32 0, i32* %7
br label %19
19: ; preds = %43, %16
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 480
br i1 %21, label %22, label %46
22: ; preds = %19
store i32 0, i32* %8
br label %23
23: ; preds = %39, %22
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 640
br i1 %25, label %26, label %42
26: ; preds = %23
%27 = load i32*, i32** %5
%28 = getelementptr inbounds i32, i32* %27, i32 1
store i32* %28, i32** %5
%29 = ptrtoint i32* %27 to i32
%30 = load i32*, i32** %6
%31 = call i64 @fwrite(i32 %29, i32 3, i32 1, i32* %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i8*, i8** %4
%35 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %34, i32 640, i32 480)
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
store i32 0, i32* %3
br label %49
38: ; preds = %26
br label %39
39: ; preds = %38
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
br label %23
42: ; preds = %23
br label %43
43: ; preds = %42
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %19
46: ; preds = %19
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
store i32 1, i32* %3
br label %49
49: ; preds = %46, %33
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [16 x i8] c"Can't open '%s'\00"
@.str.2 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
@.str.3 = [44 x i8] c"Can't read '%s'. Has PPM got size %d x %d?\00"
define i32 @save_ppm(i8* %0, i32* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %7
7: ; preds = %5, %2
%8 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 640, i32 480)
br label %9
9: ; preds = %7, %25
%10 = phi i32 [ 0, %7 ], [ %26, %25 ]
%11 = phi i32* [ %1, %7 ], [ %22, %25 ]
br label %12
12: ; preds = %9, %21
%13 = phi i32 [ 0, %9 ], [ %23, %21 ]
%14 = phi i32* [ %11, %9 ], [ %22, %21 ]
%15 = ptrtoint i32* %14 to i64
%16 = trunc i64 %15 to i32
%17 = tail call i64 @fwrite(i32 %16, i32 3, i32 1, i32* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 640, i32 480)
br label %28
21: ; preds = %12
%22 = getelementptr inbounds i32, i32* %14, i64 1
%23 = add nuw nsw i32 %13, 1
%24 = icmp eq i32 %23, 640
br i1 %24, label %25, label %12
25: ; preds = %21
%26 = add nuw nsw i32 %10, 1
%27 = icmp eq i32 %26, 480
br i1 %27, label %28, label %9
28: ; preds = %25, %19
%29 = phi i32 [ 0, %19 ], [ 1, %25 ]
%30 = tail call i32 @fclose(i32* %3)
ret i32 %29
}