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

/* Variables and functions */
int fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int pnm_write(FILE *fp, const uint8_t *rgb, int w, int h, const char *comment) {
  int wr;
  if (comment)
    wr = fprintf(fp, "P3\n%d %d\n# %s\n255\n", w, h, comment);
  else
    wr = fprintf(fp, "P3\n%d %d\n255\n", w, h);
  if (wr < 0)
    return -1;
  for (int r = 0; r < h; r++) {
    const char *sep = "";
    for (int c = 0; c < w; c++) {
      int red = *rgb++;
      int grn = *rgb++;
      int blu = *rgb++;
      fprintf(fp, "%s%d %d %d", sep, red, grn, blu);
      sep = " ";
    }
    fputc('\n', fp);
  }
  return 0;
}


x86 assembly (clang, -O0):
<pnm_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1169 <pnm_write+0x49>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%r8
lea    0xea6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    %eax,-0x2c(%rbp)
jmp    1184 <pnm_write+0x64>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    119a <pnm_write+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <pnm_write+0x144>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    125d <pnm_write+0x13d>
lea    0xe6c(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1241 <pnm_write+0x121>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%ecx
mov    -0x44(%rbp),%r8d
mov    -0x48(%rbp),%r9d
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
lea    0xdfd(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11bf <pnm_write+0x9f>
mov    -0x10(%rbp),%rsi
mov    $0xa,%edi
call   1030 <fputc@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11a1 <pnm_write+0x81>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pnm_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%r12
test   %r8,%r8
je     121e <pnm_write+0xfe>
lea    0xeb6(%rip),%rsi
mov    %r12,%rdi
mov    %r14d,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %eax,%eax
js     123d <pnm_write+0x11d>
test   %r15d,%r15d
jle    121a <pnm_write+0xfa>
mov    %r14,0x10(%rsp)
lea    -0x1(%r14),%eax
mov    %eax,0x8(%rsp)
lea    0xea2(%rip),%r13
xor    %r14d,%r14d
mov    %r15d,0xc(%rsp)
jmp    11ab <pnm_write+0x8b>
nopl   0x0(%rax)
mov    %rbx,%r13
mov    $0xa,%edi
mov    %r12,%rsi
call   1030 <fputc@plt>
add    $0x1,%r14d
cmp    0xc(%rsp),%r14d
je     121a <pnm_write+0xfa>
mov    %r13,%rbx
mov    0x10(%rsp),%r13
test   %r13d,%r13d
jle    1190 <pnm_write+0x70>
mov    0x0(%rbp),%ecx
mov    0x4(%rbp),%r8d
mov    0x8(%rbp),%r9d
add    $0xc,%rbp
mov    %r12,%rdi
mov    %rbx,%rsi
lea    0xe4c(%rip),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x8(%rsp),%r15d
cmp    $0x1,%r13d
mov    %rbx,%r13
lea    0xe3e(%rip),%rbx
je     1193 <pnm_write+0x73>
mov    0x0(%rbp),%ecx
mov    0x4(%rbp),%r8d
mov    0x8(%rbp),%r9d
add    $0xc,%rbp
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0xffffffff,%r15d
jne    11f0 <pnm_write+0xd0>
jmp    1193 <pnm_write+0x73>
xor    %eax,%eax
jmp    1242 <pnm_write+0x122>
lea    0xdee(%rip),%rsi
mov    %r12,%rdi
mov    %r14d,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %eax,%eax
jns    1162 <pnm_write+0x42>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [19 x i8] c"P3\0A%d %d\0A# %s\0A255\0A\00"
@.str.1 = [14 x i8] c"P3\0A%d %d\0A255\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [11 x i8] c"%s%d %d %d\00"
@.str.4 = [2 x i8] c" \00"
define i32 @pnm_write(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3, i8* %4) {
%6 = icmp eq i8* %4, null
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3, i8* %4)
br label %11
9: ; preds = %5
%10 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %3)
br label %11
11: ; preds = %9, %7
%12 = phi i32 [ %8, %7 ], [ %10, %9 ]
%13 = icmp slt i32 %12, 0
br i1 %13, label %47, label %14
14: ; preds = %11
%15 = icmp sgt i32 %3, 0
br i1 %15, label %16, label %47
16: ; preds = %14
%17 = icmp sgt i32 %2, 0
%18 = icmp eq i32 %2, 1
br label %19
19: ; preds = %16, %30
%20 = phi i32* [ %1, %16 ], [ %31, %30 ]
%21 = phi i32 [ 0, %16 ], [ %33, %30 ]
br i1 %17, label %22, label %30
22: ; preds = %19
%23 = getelementptr inbounds i32, i32* %20, i64 1
%24 = load i32, i32* %20
%25 = getelementptr inbounds i32, i32* %20, i64 2
%26 = load i32, i32* %23
%27 = getelementptr inbounds i32, i32* %20, i64 3
%28 = load i32, i32* %25
%29 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %26, i32 %28)
br i1 %18, label %30, label %35
30: ; preds = %35, %22, %19
%31 = phi i32* [ %20, %19 ], [ %27, %22 ], [ %42, %35 ]
%32 = tail call i32 @fputc(i8 signext 10, i32* %0)
%33 = add nuw nsw i32 %21, 1
%34 = icmp eq i32 %33, %3
br i1 %34, label %47, label %19
35: ; preds = %22, %35
%36 = phi i32* [ %42, %35 ], [ %27, %22 ]
%37 = phi i32 [ %45, %35 ], [ 1, %22 ]
%38 = getelementptr inbounds i32, i32* %36, i64 1
%39 = load i32, i32* %36
%40 = getelementptr inbounds i32, i32* %36, i64 2
%41 = load i32, i32* %38
%42 = getelementptr inbounds i32, i32* %36, i64 3
%43 = load i32, i32* %40
%44 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 %41, i32 %43)
%45 = add nuw nsw i32 %37, 1
%46 = icmp eq i32 %45, %2
br i1 %46, label %30, label %35
47: ; preds = %30, %14, %11
%48 = phi i32 [ -1, %11 ], [ 0, %14 ], [ 0, %30 ]
ret i32 %48
}