#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 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(int *, int, int, int /*<<< orphan*/ *);
int get_rgb(char);
int /*<<< orphan*/ sprintf(char *, char *, int);
void save_image(int index, char *image) {
  FILE *file;
  char filename[128];
  int row, col;
  char c;
  uint32_t rgb;
  uint8_t b;
  sprintf(filename, "invaders%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++) {
        c = image[row * 80 + col];
        rgb = get_rgb(c);
        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)
lea    -0xa0(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe7d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12de <save_image+0x17e>
mov    -0x18(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x50,%edx
mov    $0x8,%ecx
mov    $0xff,%r8d
call   1050 <fprintf@plt>
movl   $0x0,-0xa4(%rbp)
cmpl   $0x8,-0xa4(%rbp)
jge    12d5 <save_image+0x175>
movl   $0x0,-0xa8(%rbp)
cmpl   $0x50,-0xa8(%rbp)
jge    12bc <save_image+0x15c>
mov    -0x10(%rbp),%rax
imul   $0x50,-0xa4(%rbp),%ecx
add    -0xa8(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0xa9(%rbp)
movsbl -0xa9(%rbp),%edi
call   1030 <get_rgb@plt>
mov    %eax,-0xb0(%rbp)
mov    -0xb0(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %eax,-0xb4(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0xb4(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1080 <fwrite@plt>
mov    -0xb0(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %eax,-0xb4(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0xb4(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1080 <fwrite@plt>
mov    -0xb0(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
mov    %eax,-0xb4(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0xb4(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1080 <fwrite@plt>
mov    -0xa8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
jmp    11eb <save_image+0x8b>
jmp    12c1 <save_image+0x161>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    11d4 <save_image+0x74>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_image>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r14
mov    %edi,%edx
lea    0xe85(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
lea    0xe84(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1241 <save_image+0xe1>
mov    %rax,%r12
lea    0xe6c(%rip),%rsi
mov    %rax,%rdi
mov    $0x50,%edx
mov    $0x8,%ecx
mov    $0xff,%r8d
call   1060 <fprintf@plt>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xffffffffffffffb0,%rbx
nopw   0x0(%rax,%rax,1)
movsbl 0x50(%r14,%rbx,1),%edi
call   1030 <get_rgb@plt>
mov    %eax,%ebp
shr    $0x10,%eax
movsbl %al,%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
mov    %ebp,%eax
shr    $0x8,%eax
movsbl %al,%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
movsbl %bpl,%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
inc    %rbx
jne    11e0 <save_image+0x80>
add    $0x1,%r15
add    $0x50,%r14
cmp    $0x8,%r15
jne    11d0 <save_image+0x70>
mov    %r12,%rdi
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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