signed int writeppm(char *name, signed int xres, signed int yres,
                    unsigned char *imgdata) {
  struct _IO_FILE$link40 *ofp;
  signed int y;
  signed int xbytes = 3 * xres;
  ofp = fopen(name, "wb");
  if (ofp == ((struct _IO_FILE$link40 *)((void *)0)))
    return 1;
  fprintf(ofp, "P6\n");
  fprintf(ofp, "%d %d\n", xres, yres);
  fprintf(ofp, "255\n");
  y = 0;
  while (!(y >= yres)) {
    unsigned long int return_value_fwrite$1;
    return_value_fwrite$1 = fwrite(
        (const void *)&imgdata[(signed long int)(((yres - y) - 1) * xbytes)],
        (unsigned long int)1, (unsigned long int)xbytes, ofp);
    if (!(return_value_fwrite$1 == (unsigned long int)xbytes)) {
      fclose(ofp);
      return 5;
    }
    y = y + 1;
  }
  fclose(ofp);
  return 0;
}


x86 assembly (clang, -O0):
<writeppm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
imul   $0x3,-0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1050 <fopen@plt>
cltq
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118c <writeppm+0x4c>
movl   $0x1,-0x4(%rbp)
jmp    125f <writeppm+0x11f>
mov    -0x28(%rbp),%rdi
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
setge  %al
xor    $0xff,%al
test   $0x1,%al
jne    11e7 <writeppm+0xa7>
jmp    124d <writeppm+0x10d>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%eax
sub    -0x2c(%rbp),%eax
sub    $0x1,%eax
imul   -0x30(%rbp),%eax
cltq
add    %rax,%rdi
movslq -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x0,%al
call   1060 <fwrite@plt>
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x30(%rbp),%rcx
cmp    %rcx,%rax
je     123f <writeppm+0xff>
mov    -0x28(%rbp),%rdi
mov    $0x0,%al
call   1030 <fclose@plt>
movl   $0x5,-0x4(%rbp)
jmp    125f <writeppm+0x11f>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11cf <writeppm+0x8f>
mov    -0x28(%rbp),%rdi
mov    $0x0,%al
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeppm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebp
mov    %esi,%ebx
lea    0xea7(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <fopen@plt>
test   %eax,%eax
je     11eb <writeppm+0xab>
movslq %eax,%r12
lea    0xe8e(%rip),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe81(%rip),%rsi
mov    %r12,%rdi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe73(%rip),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %ebp,%ebp
jle    11f9 <writeppm+0xb9>
lea    (%rbx,%rbx,2),%r15d
movslq %r15d,%r13
mov    %rbx,%rax
mov    %ebp,%ebx
add    $0xffffffff,%ebp
imul   %eax,%ebp
lea    0x0(%rbp,%rbp,2),%ebp
nop
movslq %ebp,%rdi
add    %r14,%rdi
mov    $0x1,%esi
mov    %r13,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1060 <fwrite@plt>
cmp    %r13d,%eax
jne    11f3 <writeppm+0xb3>
sub    %r15d,%ebp
add    $0xffffffffffffffff,%rbx
jne    11c0 <writeppm+0x80>
xor    %r15d,%r15d
jmp    11f9 <writeppm+0xb9>
mov    $0x1,%r15d
jmp    1203 <writeppm+0xc3>
mov    $0x5,%r15d
mov    %r12,%rdi
xor    %eax,%eax
call   1030 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%"struct._IO_FILE$link40" = type opaque
@.str = [3 x i8] c"wb\00"
@.str.1 = [4 x i8] c"P6\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"255\0A\00"
define i32 @writeppm(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 %"struct._IO_FILE$link40"*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%14 = load i32, i32* %7
%15 = mul nsw i32 3, %14
store i32 %15, i32* %12
%16 = load i8*, i8** %6
%17 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fopen to i32 (i8*, i8*, ...)*)(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to %"struct._IO_FILE$link40"*
store %"struct._IO_FILE$link40"* %19, %"struct._IO_FILE$link40"** %10
%20 = load %"struct._IO_FILE$link40"*, %"struct._IO_FILE$link40"** %10
%21 = icmp eq %"struct._IO_FILE$link40"* %20, null
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 1, i32* %5
br label %65
23: ; preds = %4
%24 = load %"struct._IO_FILE$link40"*, %"struct._IO_FILE$link40"** %10
%25 = call i32 (%"struct._IO_FILE$link40"*, i8*, ...) bitcast (i32 (...)* @fprintf to i32 (%"struct._IO_FILE$link40"*, i8*, ...)*)(%"struct._IO_FILE$link40"* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%26 = load %"struct._IO_FILE$link40"*, %"struct._IO_FILE$link40"** %10
%27 = load i32, i32* %7
%28 = load i32, i32* %8
%29 = call i32 (%"struct._IO_FILE$link40"*, i8*, i32, i32, ...) bitcast (i32 (...)* @fprintf to i32 (%"struct._IO_FILE$link40"*, i8*, i32, i32, ...)*)(%"struct._IO_FILE$link40"* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %28)
%30 = load %"struct._IO_FILE$link40"*, %"struct._IO_FILE$link40"** %10
%31 = call i32 (%"struct._IO_FILE$link40"*, i8*, ...) bitcast (i32 (...)* @fprintf to i32 (%"struct._IO_FILE$link40"*, i8*, ...)*)(%"struct._IO_FILE$link40"* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %11
br label %32
32: ; preds = %59, %23
%33 = load i32, i32* %11
%34 = load i32, i32* %8
%35 = icmp sge i32 %33, %34
%36 = xor i1 %35, true
br i1 %36, label %37, label %62
37: ; preds = %32
%38 = load i8*, i8** %9
%39 = load i32, i32* %8
%40 = load i32, i32* %11
%41 = sub nsw i32 %39, %40
%42 = sub nsw i32 %41, 1
%43 = load i32, i32* %12
%44 = mul nsw i32 %42, %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %38, i64 %45
%47 = load i32, i32* %12
%48 = sext i32 %47 to i64
%49 = load %"struct._IO_FILE$link40"*, %"struct._IO_FILE$link40"** %10
%50 = call i32 (i8*, i64, i64, %"struct._IO_FILE$link40"*, ...) bitcast (i32 (...)* @fwrite to i32 (i8*, i64, i64, %"struct._IO_FILE$link40"*, ...)*)(i8* %46, i64 1, i64 %48, %"struct._IO_FILE$link40"* %49)
%51 = sext i32 %50 to i64
store i64 %51, i64* %13
%52 = load i64, i64* %13
%53 = load i32, i32* %12
%54 = sext i32 %53 to i64
%55 = icmp eq i64 %52, %54
br i1 %55, label %59, label %56
56: ; preds = %37
%57 = load %"struct._IO_FILE$link40"*, %"struct._IO_FILE$link40"** %10
%58 = call i32 (%"struct._IO_FILE$link40"*, ...) bitcast (i32 (...)* @fclose to i32 (%"struct._IO_FILE$link40"*, ...)*)(%"struct._IO_FILE$link40"* %57)
store i32 5, i32* %5
br label %65
59: ; preds = %37
%60 = load i32, i32* %11
%61 = add nsw i32 %60, 1
store i32 %61, i32* %11
br label %32
62: ; preds = %32
%63 = load %"struct._IO_FILE$link40"*, %"struct._IO_FILE$link40"** %10
%64 = call i32 (%"struct._IO_FILE$link40"*, ...) bitcast (i32 (...)* @fclose to i32 (%"struct._IO_FILE$link40"*, ...)*)(%"struct._IO_FILE$link40"* %63)
store i32 0, i32* %5
br label %65
65: ; preds = %62, %56, %22
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
%"struct._IO_FILE$link40" = type opaque
@.str = [3 x i8] c"wb\00"
@.str.1 = [4 x i8] c"P6\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"255\0A\00"
define i32 @writeppm(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = mul nsw i32 %1, 3
%6 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fopen to i32 (i8*, i8*, ...)*)(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = sext i32 %6 to i64
%8 = inttoptr i64 %7 to %"struct._IO_FILE$link40"*
%9 = icmp eq i32 %6, 0
br i1 %9, label %34, label %10
10: ; preds = %4
%11 = tail call i32 (%"struct._IO_FILE$link40"*, i8*, ...) bitcast (i32 (...)* @fprintf to i32 (%"struct._IO_FILE$link40"*, i8*, ...)*)(%"struct._IO_FILE$link40"* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 (%"struct._IO_FILE$link40"*, i8*, i32, i32, ...) bitcast (i32 (...)* @fprintf to i32 (%"struct._IO_FILE$link40"*, i8*, i32, i32, ...)*)(%"struct._IO_FILE$link40"* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
%13 = tail call i32 (%"struct._IO_FILE$link40"*, i8*, ...) bitcast (i32 (...)* @fprintf to i32 (%"struct._IO_FILE$link40"*, i8*, ...)*)(%"struct._IO_FILE$link40"* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp sgt i32 %2, 0
br i1 %14, label %15, label %31
15: ; preds = %10
%16 = sext i32 %5 to i64
%17 = zext i32 %2 to i64
br label %20
18: ; preds = %20
%19 = icmp eq i64 %30, %17
br i1 %19, label %31, label %20
20: ; preds = %15, %18
%21 = phi i64 [ 0, %15 ], [ %30, %18 ]
%22 = trunc i64 %21 to i32
%23 = xor i32 %22, -1
%24 = add i32 %23, %2
%25 = mul nsw i32 %24, %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %3, i64 %26
%28 = tail call i32 (i8*, i64, i64, %"struct._IO_FILE$link40"*, ...) bitcast (i32 (...)* @fwrite to i32 (i8*, i64, i64, %"struct._IO_FILE$link40"*, ...)*)(i8* %27, i64 1, i64 %16, %"struct._IO_FILE$link40"* %8)
%29 = icmp eq i32 %28, %5
%30 = add nuw nsw i64 %21, 1
br i1 %29, label %18, label %31
31: ; preds = %18, %20, %10
%32 = phi i32 [ 0, %10 ], [ 0, %18 ], [ 5, %20 ]
%33 = tail call i32 (%"struct._IO_FILE$link40"*, ...) bitcast (i32 (...)* @fclose to i32 (%"struct._IO_FILE$link40"*, ...)*)(%"struct._IO_FILE$link40"* %8)
br label %34
34: ; preds = %31, %4
%35 = phi i32 [ 1, %4 ], [ %32, %31 ]
ret i32 %35
}