#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 const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int write_ppm_file(const char *filename, int wid, int height, int *R, int *G,
                   int *B) {
  FILE *fptr;
  double **new_plot;
  int x, y;
  fptr = fopen(filename, "w");
  if (fptr == NULL)
    return -1;
  fprintf(fptr, "P6\n");
  fprintf(fptr, "%d %d\n", wid, height);
  fprintf(fptr, "255\n");
  for (y = 0; y < height; y++) {
    for (x = 0; x < wid; x++) {
      int ind = x + (y * wid);
      fprintf(fptr, "%c", R[ind]);
      fprintf(fptr, "%c", G[ind]);
      fprintf(fptr, "%c", B[ind]);
    }
  }
  fclose(fptr);
  return 0;
}


x86 assembly (clang, -O0):
<write_ppm_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea7(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1179 <write_ppm_file+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <write_ppm_file+0x142>
mov    -0x38(%rbp),%rdi
lea    0xe7e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1262 <write_ppm_file+0x132>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    124f <write_ppm_file+0x11f>
mov    -0x44(%rbp),%eax
mov    -0x48(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11cf <write_ppm_file+0x9f>
jmp    1254 <write_ppm_file+0x124>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11bc <write_ppm_file+0x8c>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ppm_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %esi,%r13d
lea    0xe8c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     126b <write_ppm_file+0x11b>
mov    %rax,%rbp
lea    0xe76(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe61(%rip),%rsi
mov    %rbp,%rdi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe51(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
test   %r12d,%r12d
jle    125f <write_ppm_file+0x10f>
movslq %r13d,%rax
mov    %r12d,%ecx
mov    %rcx,0x10(%rsp)
mov    %r13d,%r12d
shl    $0x2,%rax
mov    %rax,0x18(%rsp)
xor    %ecx,%ecx
mov    %r13d,0xc(%rsp)
jmp    1223 <write_ppm_file+0xd3>
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rcx
add    $0x1,%rcx
mov    0x18(%rsp),%rax
add    %rax,%rbx
add    %rax,%r15
add    %rax,%r14
cmp    0x10(%rsp),%rcx
mov    0xc(%rsp),%r13d
je     125f <write_ppm_file+0x10f>
mov    %rcx,0x20(%rsp)
test   %r13d,%r13d
jle    1200 <write_ppm_file+0xb0>
xor    %r13d,%r13d
mov    (%rbx,%r13,4),%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
mov    (%r15,%r13,4),%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
mov    (%r14,%r13,4),%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
add    $0x1,%r13
cmp    %r13,%r12
jne    1230 <write_ppm_file+0xe0>
jmp    1200 <write_ppm_file+0xb0>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1270 <write_ppm_file+0x120>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\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 @write_ppm_file(i8* nocapture %0, i32 %1, i32 %2, i32* nocapture %3, i32* nocapture %4, i32* nocapture %5) {
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %42, label %9
9: ; preds = %6
%10 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 3, i64 1, i32* %7)
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
%12 = tail call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 4, i64 1, i32* %7)
%13 = icmp sgt i32 %2, 0
br i1 %13, label %14, label %40
14: ; preds = %9
%15 = icmp sgt i32 %1, 0
%16 = sext i32 %1 to i64
%17 = zext i32 %2 to i64
%18 = zext i32 %1 to i64
br label %19
19: ; preds = %14, %37
%20 = phi i64 [ 0, %14 ], [ %38, %37 ]
br i1 %15, label %21, label %37
21: ; preds = %19
%22 = mul nsw i64 %20, %16
br label %23
23: ; preds = %21, %23
%24 = phi i64 [ 0, %21 ], [ %35, %23 ]
%25 = add nsw i64 %24, %22
%26 = getelementptr inbounds i32, i32* %3, i64 %25
%27 = load i32, i32* %26
%28 = tail call i32 @fputc(i32 %27, i32* %7)
%29 = getelementptr inbounds i32, i32* %4, i64 %25
%30 = load i32, i32* %29
%31 = tail call i32 @fputc(i32 %30, i32* %7)
%32 = getelementptr inbounds i32, i32* %5, i64 %25
%33 = load i32, i32* %32
%34 = tail call i32 @fputc(i32 %33, i32* %7)
%35 = add nuw nsw i64 %24, 1
%36 = icmp eq i64 %35, %18
br i1 %36, label %37, label %23
37: ; preds = %23, %19
%38 = add nuw nsw i64 %20, 1
%39 = icmp eq i64 %38, %17
br i1 %39, label %40, label %19
40: ; preds = %37, %9
%41 = tail call i32 @fclose(i32* %7)
br label %42
42: ; preds = %6, %40
%43 = phi i32 [ 0, %40 ], [ -1, %6 ]
ret i32 %43
}