#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
size_t image_raster_pixel_index_from_tile(size_t, size_t, size_t);
void image_raster_from_game_boy_save_ram(FILE *save_file, uint8_t *image_raster,
                                         uint8_t photo_index) {
  char tile[16];
  fseek(save_file, 0x2000 + (0x1000 * photo_index), 0);
  for (size_t i = 0; i < 16 * 14 * 2; i += 2) {
    fread(tile, 1, sizeof tile, save_file);
    uint8_t pixel_value;
    for (size_t j = 0, y = 0; j < 16; j += 2, y++) {
      for (size_t k = 0, x = 7; k < 8; k++, x--) {
        pixel_value =
            ((tile[j] >> k) & 0x01) + (((tile[j + 1] >> k) & 0x01) << 1);
        pixel_value = pixel_value ^ 3;
        image_raster[image_raster_pixel_index_from_tile(i / 2, x, y)] =
            pixel_value;
      }
    }
  }
}


x86 assembly (clang, -O0):
<image_raster_from_game_boy_save_ram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
shl    $0xc,%esi
add    $0x2000,%esi
xor    %edx,%edx
call   1050 <fseek@plt>
movq   $0x0,-0x38(%rbp)
cmpq   $0x1c0,-0x38(%rbp)
jae    1279 <image_raster_from_game_boy_save_ram+0x149>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
cmpq   $0x10,-0x48(%rbp)
jae    1263 <image_raster_from_game_boy_save_ram+0x133>
movq   $0x0,-0x58(%rbp)
movq   $0x7,-0x60(%rbp)
cmpq   $0x8,-0x58(%rbp)
jae    1241 <image_raster_from_game_boy_save_ram+0x111>
mov    -0x48(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%eax
mov    -0x58(%rbp),%rcx
sar    %cl,%eax
and    $0x1,%eax
mov    -0x48(%rbp),%rcx
movsbl -0x2f(%rbp,%rcx,1),%edx
mov    -0x58(%rbp),%rcx
sar    %cl,%edx
mov    %edx,%ecx
and    $0x1,%ecx
shl    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
xor    $0x3,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x38(%rbp),%rdi
shr    $0x1,%rdi
mov    -0x60(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    -0x6c(%rbp),%edx
mov    %rax,%rcx
mov    -0x68(%rbp),%rax
mov    %edx,(%rax,%rcx,4)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x60(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x60(%rbp)
jmp    11b2 <image_raster_from_game_boy_save_ram+0x82>
jmp    1246 <image_raster_from_game_boy_save_ram+0x116>
mov    -0x48(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1197 <image_raster_from_game_boy_save_ram+0x67>
jmp    1268 <image_raster_from_game_boy_save_ram+0x138>
mov    -0x38(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x38(%rbp)
jmp    1162 <image_raster_from_game_boy_save_ram+0x32>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<image_raster_from_game_boy_save_ram>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
shl    $0xc,%edx
lea    0x2000(%rdx),%esi
xor    %r13d,%r13d
mov    %rdi,(%rsp)
xor    %edx,%edx
call   1050 <fseek@plt>
nopl   0x0(%rax,%rax,1)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    (%rsp),%rcx
call   1030 <fread@plt>
mov    %r13,0x8(%rsp)
shr    %r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x10(%rsp,%rbx,2),%r15d
movzbl 0x11(%rsp,%rbx,2),%r14d
mov    %r15d,%eax
and    $0x1,%eax
mov    %r14d,%ecx
and    $0x1,%ecx
lea    (%rax,%rcx,2),%ebp
xor    $0x3,%ebp
mov    $0x7,%esi
mov    %r13,%rdi
mov    %rbx,%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    %ebp,(%r12,%rax,4)
mov    %r15d,%eax
shr    %eax
and    $0x1,%eax
mov    %r14d,%ebp
and    $0x2,%ebp
or     %eax,%ebp
xor    $0x3,%ebp
mov    $0x6,%esi
mov    %r13,%rdi
mov    %rbx,%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    %ebp,(%r12,%rax,4)
mov    %r15d,%eax
shr    $0x2,%eax
and    $0x1,%eax
mov    %r14d,%ebp
shr    %ebp
and    $0x2,%ebp
or     %eax,%ebp
xor    $0x3,%ebp
mov    $0x5,%esi
mov    %r13,%rdi
mov    %rbx,%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    %ebp,(%r12,%rax,4)
mov    %r15d,%eax
shr    $0x3,%eax
and    $0x1,%eax
mov    %r14d,%ebp
shr    $0x2,%ebp
and    $0x2,%ebp
or     %eax,%ebp
xor    $0x3,%ebp
mov    $0x4,%esi
mov    %r13,%rdi
mov    %rbx,%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    %ebp,(%r12,%rax,4)
mov    %r15d,%eax
shr    $0x4,%eax
and    $0x1,%eax
mov    %r14d,%ebp
shr    $0x3,%ebp
and    $0x2,%ebp
or     %eax,%ebp
xor    $0x3,%ebp
mov    $0x3,%esi
mov    %r13,%rdi
mov    %rbx,%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    %ebp,(%r12,%rax,4)
mov    %r15d,%eax
shr    $0x5,%eax
and    $0x1,%eax
mov    %r14d,%ebp
shr    $0x4,%ebp
and    $0x2,%ebp
or     %eax,%ebp
xor    $0x3,%ebp
mov    $0x2,%esi
mov    %r13,%rdi
mov    %rbx,%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    %ebp,(%r12,%rax,4)
mov    %r15d,%eax
shr    $0x6,%eax
and    $0x1,%eax
mov    %r14d,%ebp
shr    $0x5,%ebp
and    $0x2,%ebp
or     %eax,%ebp
xor    $0x3,%ebp
mov    $0x1,%esi
mov    %r13,%rdi
mov    %rbx,%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    %ebp,(%r12,%rax,4)
shr    $0x7,%r15d
shr    $0x7,%r14d
lea    (%r15,%r14,2),%ebp
xor    $0x3,%ebp
mov    %r13,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <image_raster_pixel_index_from_tile@plt>
mov    %ebp,(%r12,%rax,4)
add    $0x1,%rbx
cmp    $0x8,%rbx
jne    1190 <image_raster_from_game_boy_save_ram+0x60>
mov    0x8(%rsp),%rcx
lea    0x2(%rcx),%r13
cmp    $0x1be,%rcx
jb     1160 <image_raster_from_game_boy_save_ram+0x30>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @image_raster_from_game_boy_save_ram(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [16 x i8]
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%14 = load i32*, i32** %4
%15 = load i32, i32* %6
%16 = mul nsw i32 4096, %15
%17 = add nsw i32 8192, %16
%18 = call i32 @fseek(i32* %14, i32 %17, i32 0)
store i64 0, i64* %8
br label %19
19: ; preds = %75, %3
%20 = load i64, i64* %8
%21 = icmp ult i64 %20, 448
br i1 %21, label %22, label %78
22: ; preds = %19
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%24 = load i32*, i32** %4
%25 = call i32 @fread(i8* %23, i32 1, i32 16, i32* %24)
store i64 0, i64* %10
store i64 0, i64* %11
br label %26
26: ; preds = %69, %22
%27 = load i64, i64* %10
%28 = icmp ult i64 %27, 16
br i1 %28, label %29, label %74
29: ; preds = %26
store i64 0, i64* %12
store i64 7, i64* %13
br label %30
30: ; preds = %63, %29
%31 = load i64, i64* %12
%32 = icmp ult i64 %31, 8
br i1 %32, label %33, label %68
33: ; preds = %30
%34 = load i64, i64* %10
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = load i64, i64* %12
%39 = trunc i64 %38 to i32
%40 = ashr i32 %37, %39
%41 = and i32 %40, 1
%42 = load i64, i64* %10
%43 = add i64 %42, 1
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = load i64, i64* %12
%48 = trunc i64 %47 to i32
%49 = ashr i32 %46, %48
%50 = and i32 %49, 1
%51 = shl i32 %50, 1
%52 = add nsw i32 %41, %51
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = xor i32 %53, 3
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = load i32*, i32** %5
%57 = load i64, i64* %8
%58 = udiv i64 %57, 2
%59 = load i64, i64* %13
%60 = load i64, i64* %11
%61 = call i64 @image_raster_pixel_index_from_tile(i64 %58, i64 %59, i64 %60)
%62 = getelementptr inbounds i32, i32* %56, i64 %61
store i32 %55, i32* %62
br label %63
63: ; preds = %33
%64 = load i64, i64* %12
%65 = add i64 %64, 1
store i64 %65, i64* %12
%66 = load i64, i64* %13
%67 = add i64 %66, -1
store i64 %67, i64* %13
br label %30
68: ; preds = %30
br label %69
69: ; preds = %68
%70 = load i64, i64* %10
%71 = add i64 %70, 2
store i64 %71, i64* %10
%72 = load i64, i64* %11
%73 = add i64 %72, 1
store i64 %73, i64* %11
br label %26
74: ; preds = %26
br label %75
75: ; preds = %74
%76 = load i64, i64* %8
%77 = add i64 %76, 2
store i64 %77, i64* %8
br label %19
78: ; preds = %19
ret void
}

LLVM IR (-O2):
define void @image_raster_from_game_boy_save_ram(i32* nocapture %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = shl nsw i32 %2, 12
%7 = add nsw i32 %6, 8192
%8 = tail call i32 @fseek(i32* %0, i32 %7, i32 0)
br label %10
9: ; preds = %88
ret void
10: ; preds = %3, %88
%11 = phi i64 [ 0, %3 ], [ %89, %88 ]
%12 = call i32 @fread(i8* %5, i32 1, i32 16, i32* %0)
%13 = lshr exact i64 %11, 1
br label %14
14: ; preds = %10, %14
%15 = phi i64 [ 0, %10 ], [ %86, %14 ]
%16 = phi i64 [ 0, %10 ], [ %85, %14 ]
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = or i64 %16, 1
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = and i32 %19, 1
%25 = shl nuw nsw i32 %23, 1
%26 = and i32 %25, 2
%27 = or i32 %26, %24
%28 = xor i32 %27, 3
%29 = tail call i64 @image_raster_pixel_index_from_tile(i64 %13, i64 7, i64 %15)
%30 = getelementptr inbounds i32, i32* %1, i64 %29
store i32 %28, i32* %30
%31 = lshr i32 %19, 1
%32 = and i32 %31, 1
%33 = and i32 %23, 2
%34 = or i32 %33, %32
%35 = xor i32 %34, 3
%36 = tail call i64 @image_raster_pixel_index_from_tile(i64 %13, i64 6, i64 %15)
%37 = getelementptr inbounds i32, i32* %1, i64 %36
store i32 %35, i32* %37
%38 = lshr i32 %19, 2
%39 = and i32 %38, 1
%40 = lshr i32 %23, 1
%41 = and i32 %40, 2
%42 = or i32 %41, %39
%43 = xor i32 %42, 3
%44 = tail call i64 @image_raster_pixel_index_from_tile(i64 %13, i64 5, i64 %15)
%45 = getelementptr inbounds i32, i32* %1, i64 %44
store i32 %43, i32* %45
%46 = lshr i32 %19, 3
%47 = and i32 %46, 1
%48 = lshr i32 %23, 2
%49 = and i32 %48, 2
%50 = or i32 %49, %47
%51 = xor i32 %50, 3
%52 = tail call i64 @image_raster_pixel_index_from_tile(i64 %13, i64 4, i64 %15)
%53 = getelementptr inbounds i32, i32* %1, i64 %52
store i32 %51, i32* %53
%54 = lshr i32 %19, 4
%55 = and i32 %54, 1
%56 = lshr i32 %23, 3
%57 = and i32 %56, 2
%58 = or i32 %57, %55
%59 = xor i32 %58, 3
%60 = tail call i64 @image_raster_pixel_index_from_tile(i64 %13, i64 3, i64 %15)
%61 = getelementptr inbounds i32, i32* %1, i64 %60
store i32 %59, i32* %61
%62 = lshr i32 %19, 5
%63 = and i32 %62, 1
%64 = lshr i32 %23, 4
%65 = and i32 %64, 2
%66 = or i32 %65, %63
%67 = xor i32 %66, 3
%68 = tail call i64 @image_raster_pixel_index_from_tile(i64 %13, i64 2, i64 %15)
%69 = getelementptr inbounds i32, i32* %1, i64 %68
store i32 %67, i32* %69
%70 = lshr i32 %19, 6
%71 = and i32 %70, 1
%72 = lshr i32 %23, 5
%73 = and i32 %72, 2
%74 = or i32 %73, %71
%75 = xor i32 %74, 3
%76 = tail call i64 @image_raster_pixel_index_from_tile(i64 %13, i64 1, i64 %15)
%77 = getelementptr inbounds i32, i32* %1, i64 %76
store i32 %75, i32* %77
%78 = lshr i32 %19, 7
%79 = lshr i32 %23, 6
%80 = and i32 %79, 2
%81 = or i32 %80, %78
%82 = xor i32 %81, 3
%83 = tail call i64 @image_raster_pixel_index_from_tile(i64 %13, i64 0, i64 %15)
%84 = getelementptr inbounds i32, i32* %1, i64 %83
store i32 %82, i32* %84
%85 = add nuw nsw i64 %16, 2
%86 = add nuw nsw i64 %15, 1
%87 = icmp eq i64 %86, 8
br i1 %87, label %88, label %14
88: ; preds = %14
%89 = add nuw nsw i64 %11, 2
%90 = icmp ult i64 %11, 446
br i1 %90, label %10, label %9
}