#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 *, ...);
void saveppm(char *fname, int **img, int x, int y) {
  int i, j;
  FILE *fp = fopen(fname, "w");
  fprintf(fp, "P3\n%d %d\n255\n", x, y);
  for (i = 0; i < x; i++, fprintf(fp, "\n"))
    for (j = 0; j < y; j++)
      if (img[i][j])
        fprintf(fp, "%d %d %d\n", img[i][j] % 256, img[i][j] % 256,
                img[i][j] % 256);
      else
        fprintf(fp, "0 0 0\n");
  fclose(fp);
  return;
}


x86 assembly (clang, -O0):
<saveppm>:
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    -0x8(%rbp),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe97(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    126f <saveppm+0x13f>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    124a <saveppm+0x11a>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     1225 <saveppm+0xf5>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %edx,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rdx
mov    (%rax,%rdx,8),%rax
movslq -0x20(%rbp),%rdx
mov    (%rax,%rdx,4),%eax
mov    $0x100,%esi
cltd
idiv   %esi
mov    %edx,%r8d
mov    -0x2c(%rbp),%edx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1237 <saveppm+0x107>
mov    -0x28(%rbp),%rdi
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    123c <saveppm+0x10c>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    118c <saveppm+0x5c>
jmp    124f <saveppm+0x11f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1179 <saveppm+0x49>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saveppm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbp
lea    0xe85(%rip),%rsi
mov    %rax,%rdi
mov    %r15d,%edx
mov    %r14d,0xc(%rsp)
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r15d,%r15d
jle    1244 <saveppm+0xf4>
mov    %r15d,%eax
mov    %rax,0x10(%rsp)
mov    0xc(%rsp),%r14d
lea    0xe61(%rip),%r12
xor    %r13d,%r13d
jmp    11d8 <saveppm+0x88>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r13
mov    $0xa,%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
cmp    0x10(%rsp),%r13
je     1244 <saveppm+0xf4>
cmpl   $0x0,0xc(%rsp)
jle    11c0 <saveppm+0x70>
xor    %r15d,%r15d
jmp    1212 <saveppm+0xc2>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x6,%esi
mov    $0x1,%edx
lea    0xe19(%rip),%rdi
mov    %rbp,%rcx
call   1070 <fwrite@plt>
add    $0x1,%r15
cmp    %r15,%r14
je     11c0 <saveppm+0x70>
mov    (%rbx,%r13,8),%rax
mov    (%rax,%r15,4),%ecx
test   %ecx,%ecx
je     11f0 <saveppm+0xa0>
lea    0xff(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
and    $0xffffff00,%eax
sub    %eax,%ecx
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ecx,%edx
mov    %ecx,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1209 <saveppm+0xb9>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [14 x i8] c"P3\0A%d %d\0A255\0A\00"
@.str.2 = [10 x i8] c"%d %d %d\0A\00"
@.str.3 = [7 x i8] c"0 0 0\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @saveppm(i8* %0, i32** %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i8* %0, i8** %5
store i32** %1, i32*** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16)
store i32 0, i32* %9
br label %18
18: ; preds = %79, %4
%19 = load i32, i32* %9
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %84
22: ; preds = %18
store i32 0, i32* %10
br label %23
23: ; preds = %75, %22
%24 = load i32, i32* %10
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %78
27: ; preds = %23
%28 = load i32**, i32*** %6
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32*, i32** %28, i64 %30
%32 = load i32*, i32** %31
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = load i32, i32* %35
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %71
38: ; preds = %27
%39 = load i32*, i32** %11
%40 = load i32**, i32*** %6
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32*, i32** %40, i64 %42
%44 = load i32*, i32** %43
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = srem i32 %48, 256
%50 = load i32**, i32*** %6
%51 = load i32, i32* %9
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32*, i32** %50, i64 %52
%54 = load i32*, i32** %53
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
%58 = load i32, i32* %57
%59 = srem i32 %58, 256
%60 = load i32**, i32*** %6
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32*, i32** %60, i64 %62
%64 = load i32*, i32** %63
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i32, i32* %64, i64 %66
%68 = load i32, i32* %67
%69 = srem i32 %68, 256
%70 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %59, i32 %69)
br label %74
71: ; preds = %27
%72 = load i32*, i32** %11
%73 = call i32 (i32*, i8*, ...) @fprintf(i32* %72, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %74
74: ; preds = %71, %38
br label %75
75: ; preds = %74
%76 = load i32, i32* %10
%77 = add nsw i32 %76, 1
store i32 %77, i32* %10
br label %23
78: ; preds = %23
br label %79
79: ; preds = %78
%80 = load i32, i32* %9
%81 = add nsw i32 %80, 1
store i32 %81, i32* %9
%82 = load i32*, i32** %11
%83 = call i32 (i32*, i8*, ...) @fprintf(i32* %82, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %18
84: ; preds = %18
%85 = load i32*, i32** %11
%86 = call i32 @fclose(i32* %85)
ret void
}

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