#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int, int);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void save_image(int index, uint8_t frame[][80][3]) {
  FILE *file;
  char filename[128];
  int row, col;
  uint8_t r, g, b;
  sprintf(filename, "scroll%03d.ppm", index);
  file = fopen(filename, "wb");
  if (file != NULL) {
    fprintf(file, "P6\n%d %d\n%d\n", 80, 8, 255);
    for (row = 0; row < 8; row++) {
      for (col = 0; col < 80; col++) {
        r = frame[row][col][0];
        fwrite(&r, 1, 1, file);
        g = frame[row][col][1];
        fwrite(&g, 1, 1, file);
        b = frame[row][col][2];
        fwrite(&b, 1, 1, file);
      }
    }
    fclose(file);
  }
}


x86 assembly (clang, -O0):
<save_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe8d(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12e6 <save_image+0x196>
mov    -0x18(%rbp),%rdi
lea    0xe6d(%rip),%rsi
mov    $0x50,%edx
mov    $0x8,%ecx
mov    $0xff,%r8d
call   1040 <fprintf@plt>
movl   $0x0,-0xa4(%rbp)
cmpl   $0x8,-0xa4(%rbp)
jge    12dd <save_image+0x18d>
movl   $0x0,-0xa8(%rbp)
cmpl   $0x50,-0xa8(%rbp)
jge    12c4 <save_image+0x174>
mov    -0x10(%rbp),%rax
movslq -0xa4(%rbp),%rcx
imul   $0x3c0,%rcx,%rcx
add    %rcx,%rax
movslq -0xa8(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    (%rax),%eax
mov    %eax,-0xac(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0xac(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x10(%rbp),%rax
movslq -0xa4(%rbp),%rcx
imul   $0x3c0,%rcx,%rcx
add    %rcx,%rax
movslq -0xa8(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x4(%rax),%eax
mov    %eax,-0xb0(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0xb0(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x10(%rbp),%rax
movslq -0xa4(%rbp),%rcx
imul   $0x3c0,%rcx,%rcx
add    %rcx,%rax
movslq -0xa8(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
mov    %eax,-0xb4(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0xb4(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0xa8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
jmp    11db <save_image+0x8b>
jmp    12c9 <save_image+0x179>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    11c4 <save_image+0x74>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_image>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r14
mov    %edi,%edx
lea    0xe96(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe93(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     122d <save_image+0xdd>
mov    %rax,%r12
lea    0xe7b(%rip),%rsi
mov    %rax,%rdi
mov    $0x50,%edx
mov    $0x8,%ecx
mov    $0xff,%r8d
call   1050 <fprintf@plt>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x8,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movsbl -0x8(%r14,%rbx,1),%edi
mov    %r12,%rsi
call   1040 <fputc@plt>
movsbl -0x4(%r14,%rbx,1),%edi
mov    %r12,%rsi
call   1040 <fputc@plt>
movsbl (%r14,%rbx,1),%edi
mov    %r12,%rsi
call   1040 <fputc@plt>
add    $0xc,%rbx
cmp    $0x3c8,%rbx
jne    11d0 <save_image+0x80>
add    $0x1,%r15
add    $0x3c0,%r14
cmp    $0x8,%r15
jne    11c0 <save_image+0x70>
mov    %r12,%rdi
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"scroll%03d.ppm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [13 x i8] c"P6\0A%d %d\0A%d\0A\00"
define void @save_image(i32 %0, [80 x [3 x i32]]* nocapture %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %36, label %8
8: ; preds = %2
%9 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 80, i32 8, i32 255)
br label %10
10: ; preds = %8, %31
%11 = phi i64 [ 0, %8 ], [ %32, %31 ]
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %29, %12 ]
%14 = getelementptr inbounds [80 x [3 x i32]], [80 x [3 x i32]]* %1, i64 %11, i64 %13, i64 0
%15 = load i32, i32* %14
%16 = shl i32 %15, 24
%17 = ashr exact i32 %16, 24
%18 = tail call i32 @fputc(i32 %17, i32* %6)
%19 = getelementptr inbounds [80 x [3 x i32]], [80 x [3 x i32]]* %1, i64 %11, i64 %13, i64 1
%20 = load i32, i32* %19
%21 = shl i32 %20, 24
%22 = ashr exact i32 %21, 24
%23 = tail call i32 @fputc(i32 %22, i32* %6)
%24 = getelementptr inbounds [80 x [3 x i32]], [80 x [3 x i32]]* %1, i64 %11, i64 %13, i64 2
%25 = load i32, i32* %24
%26 = shl i32 %25, 24
%27 = ashr exact i32 %26, 24
%28 = tail call i32 @fputc(i32 %27, i32* %6)
%29 = add nuw nsw i64 %13, 1
%30 = icmp eq i64 %29, 80
br i1 %30, label %31, label %12
31: ; preds = %12
%32 = add nuw nsw i64 %11, 1
%33 = icmp eq i64 %32, 8
br i1 %33, label %34, label %10
34: ; preds = %31
%35 = tail call i32 @fclose(i32* %6)
br label %36
36: ; preds = %34, %2
ret void
}