#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 size_t uint8_t;
typedef int uint32_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(size_t *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void save_image(int index, uint8_t field[][80], uint32_t palet[]) {
  FILE *file;
  char filename[128];
  int row, col;
  uint32_t rgb;
  uint8_t b;
  sprintf(filename, "fire%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++) {
        rgb = palet[field[row][col]];
        b = (rgb >> 16) & 0xFF;
        fwrite(&b, 1, 1, file);
        b = (rgb >> 8) & 0xFF;
        fwrite(&b, 1, 1, file);
        b = (rgb >> 0) & 0xFF;
        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)
mov    %rdx,-0x18(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe89(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12d9 <save_image+0x189>
mov    -0x20(%rbp),%rdi
lea    0xe67(%rip),%rsi
mov    $0x50,%edx
mov    $0x8,%ecx
mov    $0xff,%r8d
call   1040 <fprintf@plt>
movl   $0x0,-0xa4(%rbp)
cmpl   $0x8,-0xa4(%rbp)
jge    12d0 <save_image+0x180>
movl   $0x0,-0xa8(%rbp)
cmpl   $0x50,-0xa8(%rbp)
jge    12b7 <save_image+0x167>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0xa4(%rbp),%rdx
imul   $0x280,%rdx,%rdx
add    %rdx,%rcx
movslq -0xa8(%rbp),%rdx
mov    (%rcx,%rdx,8),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0xac(%rbp)
mov    -0xac(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
cltq
mov    %rax,-0xb8(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0xb8(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0xac(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
cltq
mov    %rax,-0xb8(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0xb8(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0xac(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
cltq
mov    %rax,-0xb8(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0xb8(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0xa8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
jmp    11df <save_image+0x8f>
jmp    12bc <save_image+0x16c>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    11c8 <save_image+0x78>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%edx
lea    0xe90(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe8b(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1238 <save_image+0xe8>
mov    %rax,%r13
lea    0xe73(%rip),%rsi
mov    %rax,%rdi
mov    $0x50,%edx
mov    $0x8,%ecx
mov    $0xff,%r8d
call   1050 <fprintf@plt>
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15,%rbx,8),%rax
mov    (%r14,%rax,4),%ebp
mov    %ebp,%eax
shr    $0x10,%eax
movsbl %al,%edi
mov    %r13,%rsi
call   1040 <fputc@plt>
mov    %ebp,%eax
shr    $0x8,%eax
movsbl %al,%edi
mov    %r13,%rsi
call   1040 <fputc@plt>
movsbl %bpl,%edi
mov    %r13,%rsi
call   1040 <fputc@plt>
add    $0x1,%rbx
cmp    $0x50,%rbx
jne    11d0 <save_image+0x80>
add    $0x1,%r12
add    $0x280,%r15
cmp    $0x8,%r12
jne    11c0 <save_image+0x70>
mov    %r13,%rdi
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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