#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 FILENAME_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void save_oam(char *fname, char *ext, unsigned char *cgb_atr,
              unsigned char *cgb_map, unsigned short map_size) {
  char fn[FILENAME_MAX];
  unsigned char buffer[] = "%00000000", j;
  unsigned short i;
  FILE *fp = NULL;
  strcpy(fn, fname);
  strcat(fn, ext);
  fp = fopen(fn, "w");
  fprintf(fp, ";                     7        6       5       4      3       2 "
              "       1        0    \n");
  fprintf(fp, "; y, x, chr, atr (priority, v-flip, h-flip, dmg pal, bank, "
              "palette, palette, palette)\n\n");
  fprintf(fp, "%s_oam:\n", fname);
  for (i = 0; i < map_size; i++) {
    if (cgb_atr[i] & 0x10) {
      cgb_atr[i] &= 0xef;
      for (j = 8; j > 0; j--) {
        if (cgb_atr[i] & 1)
          buffer[j] = 0x31;
        else
          buffer[j] = 0x30;
        cgb_atr[i] >>= 1;
      }
      fprintf(fp, "\tdb\t$00, $00, $%02x, %s\n", cgb_map[i], buffer);
    }
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<save_oam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %r8w,%ax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %ax,-0x22(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    0xe5d(%rip),%rax
mov    %rax,-0x42(%rbp)
mov    0xe5a(%rip),%ax
mov    %ax,-0x3a(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x58(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x50(%rbp),%rdi
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xea4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movw   $0x0,-0x46(%rbp)
movzwl -0x46(%rbp),%eax
movzwl -0x22(%rbp),%ecx
cmp    %ecx,%eax
jge    12f8 <save_oam+0x1a8>
mov    -0x18(%rbp),%rax
movzwl -0x46(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     12e2 <save_oam+0x192>
mov    -0x18(%rbp),%rax
movzwl -0x46(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
and    $0xef,%edx
mov    %dl,(%rax,%rcx,1)
movb   $0x8,-0x43(%rbp)
movzbl -0x43(%rbp),%eax
cmp    $0x0,%eax
jle    12c0 <save_oam+0x170>
mov    -0x18(%rbp),%rax
movzwl -0x46(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1298 <save_oam+0x148>
movzbl -0x43(%rbp),%eax
movb   $0x31,-0x42(%rbp,%rax,1)
jmp    12a1 <save_oam+0x151>
movzbl -0x43(%rbp),%eax
movb   $0x30,-0x42(%rbp,%rax,1)
mov    -0x18(%rbp),%rax
movzwl -0x46(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
sar    $0x1,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x43(%rbp),%al
add    $0xff,%al
mov    %al,-0x43(%rbp)
jmp    1265 <save_oam+0x115>
mov    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rax
movzwl -0x46(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    -0x42(%rbp),%rcx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12e7 <save_oam+0x197>
mov    -0x46(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x46(%rbp)
jmp    1224 <save_oam+0xd4>
mov    -0x50(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_oam>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,-0x3c(%rbp)
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
movabs $0x3030303030303025,%rax
30 30 30
mov    %rax,-0x38(%rbp)
movw   $0x30,-0x30(%rbp)
mov    %r12,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
lea    0xe3e(%rip),%rsi
mov    %r12,%rdi
call   1060 <fopen@plt>
mov    %rax,%r15
lea    0xe2e(%rip),%rdi
mov    $0x56,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
lea    0xe6c(%rip),%rdi
mov    $0x57,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
lea    0xeab(%rip),%rsi
mov    %r15,%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     12e1 <save_oam+0x181>
movzwl %ax,%r12d
xor    %r13d,%r13d
jmp    124d <save_oam+0xed>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13
cmp    %r13,%r12
je     12e1 <save_oam+0x181>
movzbl (%rbx,%r13,1),%eax
test   $0x10,%al
je     1240 <save_oam+0xe0>
mov    %eax,%ecx
and    $0x1,%cl
or     $0x30,%cl
mov    %eax,%edx
and    $0x2,%dl
cmp    $0x1,%dl
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %cl,-0x30(%rbp)
mov    %dl,-0x31(%rbp)
mov    %eax,%ecx
and    $0x4,%cl
cmp    $0x1,%cl
mov    $0x31,%cl
sbb    $0x0,%cl
mov    %eax,%edx
and    $0x8,%dl
cmp    $0x1,%dl
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %cl,-0x32(%rbp)
mov    %dl,-0x33(%rbp)
movb   $0x30,-0x34(%rbp)
mov    %eax,%ecx
and    $0x20,%cl
cmp    $0x1,%cl
mov    $0x31,%cl
sbb    $0x0,%cl
mov    %cl,-0x35(%rbp)
mov    %eax,%ecx
and    $0x40,%cl
cmp    $0x1,%cl
mov    $0x31,%cl
sbb    $0x0,%cl
mov    %cl,-0x36(%rbp)
shr    $0x7,%al
or     $0x30,%al
mov    %al,-0x37(%rbp)
movb   $0x0,(%rbx,%r13,1)
movzbl (%r14,%r13,1),%edx
mov    %r15,%rdi
lea    0xdf3(%rip),%rsi
lea    -0x38(%rbp),%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1240 <save_oam+0xe0>
mov    %r15,%rdi
call   1040 <fclose@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@__const.save_oam.buffer = [10 x i8] c"%00000000\00"
@.str = [2 x i8] c"w\00"
@.str.1 = [87 x i8] c"; 7 6 5 4 3 2 1 0 \0A\00"
@.str.2 = [88 x i8] c"; y, x, chr, atr (priority, v-flip, h-flip, dmg pal, bank, palette, palette, palette)\0A\0A\00"
@.str.3 = [9 x i8] c"%s_oam:\0A\00"
@.str.4 = [25 x i8] c"\09db\09$00, $00, $%02x, %s\0A\00"
define void @save_oam(i8* %0, i8* %1, i8* %2, i8* %3, i16 zeroext %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i16
%11 = alloca i8*
%12 = alloca i64
%13 = alloca [10 x i8]
%14 = alloca i8
%15 = alloca i16
%16 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i16 %4, i16* %10
%17 = load i32, i32* @FILENAME_MAX
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %11
%20 = alloca i8, i64 %18
store i64 %18, i64* %12
%21 = bitcast [10 x i8]* %13 to i8*
store i32* null, i32** %16
%22 = load i8*, i8** %6
%23 = call i32 @strcpy(i8* %20, i8* %22)
%24 = load i8*, i8** %7
%25 = call i32 @strcat(i8* %20, i8* %24)
%26 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %26, i32** %16
%27 = load i32*, i32** %16
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32*, i32** %16
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32*, i32** %16
%32 = load i8*, i8** %6
%33 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %32)
store i16 0, i16* %15
br label %34
34: ; preds = %102, %5
%35 = load i16, i16* %15
%36 = zext i16 %35 to i32
%37 = load i16, i16* %10
%38 = zext i16 %37 to i32
%39 = icmp slt i32 %36, %38
br i1 %39, label %40, label %105
40: ; preds = %34
%41 = load i8*, i8** %8
%42 = load i16, i16* %15
%43 = zext i16 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = and i32 %46, 16
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %101
49: ; preds = %40
%50 = load i8*, i8** %8
%51 = load i16, i16* %15
%52 = zext i16 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = and i32 %55, 239
%57 = trunc i32 %56 to i8
store i8 %57, i8* %53
store i8 8, i8* %14
br label %58
58: ; preds = %88, %49
%59 = load i8, i8* %14
%60 = zext i8 %59 to i32
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %91
62: ; preds = %58
%63 = load i8*, i8** %8
%64 = load i16, i16* %15
%65 = zext i16 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = and i32 %68, 1
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %62
%72 = load i8, i8* %14
%73 = zext i8 %72 to i64
%74 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 %73
store i8 49, i8* %74
br label %79
75: ; preds = %62
%76 = load i8, i8* %14
%77 = zext i8 %76 to i64
%78 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 %77
store i8 48, i8* %78
br label %79
79: ; preds = %75, %71
%80 = load i8*, i8** %8
%81 = load i16, i16* %15
%82 = zext i16 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = load i8, i8* %83
%85 = zext i8 %84 to i32
%86 = ashr i32 %85, 1
%87 = trunc i32 %86 to i8
store i8 %87, i8* %83
br label %88
88: ; preds = %79
%89 = load i8, i8* %14
%90 = add i8 %89, -1
store i8 %90, i8* %14
br label %58
91: ; preds = %58
%92 = load i32*, i32** %16
%93 = load i8*, i8** %9
%94 = load i16, i16* %15
%95 = zext i16 %94 to i64
%96 = getelementptr inbounds i8, i8* %93, i64 %95
%97 = load i8, i8* %96
%98 = zext i8 %97 to i32
%99 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 0
%100 = call i32 (i32*, i8*, ...) @fprintf(i32* %92, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %98, i8* %99)
br label %101
101: ; preds = %91, %40
br label %102
102: ; preds = %101
%103 = load i16, i16* %15
%104 = add i16 %103, 1
store i16 %104, i16* %15
br label %34
105: ; preds = %34
%106 = load i32*, i32** %16
%107 = call i32 @fclose(i32* %106)
%108 = load i8*, i8** %11
ret void
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@__const.save_oam.buffer = [10 x i8] c"%00000000\00"
@.str = [2 x i8] c"w\00"
@.str.1 = [87 x i8] c"; 7 6 5 4 3 2 1 0 \0A\00"
@.str.2 = [88 x i8] c"; y, x, chr, atr (priority, v-flip, h-flip, dmg pal, bank, palette, palette, palette)\0A\0A\00"
@.str.3 = [9 x i8] c"%s_oam:\0A\00"
@.str.4 = [25 x i8] c"\09db\09$00, $00, $%02x, %s\0A\00"
define void @save_oam(i8* %0, i8* %1, i8* nocapture %2, i8* nocapture %3, i16 zeroext %4) {
%6 = alloca [10 x i8]
%7 = load i32, i32* @FILENAME_MAX
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%11 = call i32 @strcpy(i8* %9, i8* %0)
%12 = call i32 @strcat(i8* %9, i8* %1)
%13 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = call i64 @fwrite(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.1, i64 0, i64 0), i64 86, i64 1, i32* %13)
%15 = call i64 @fwrite(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.2, i64 0, i64 0), i64 87, i64 1, i32* %13)
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%17 = icmp eq i16 %4, 0
br i1 %17, label %61, label %18
18: ; preds = %5
%19 = zext i16 %4 to i64
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 8
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 7
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 6
%23 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 5
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 4
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 3
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 2
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 1
br label %28
28: ; preds = %18, %58
%29 = phi i64 [ 0, %18 ], [ %59, %58 ]
%30 = getelementptr inbounds i8, i8* %2, i64 %29
%31 = load i8, i8* %30
%32 = and i8 %31, 16
%33 = icmp eq i8 %32, 0
br i1 %33, label %58, label %34
34: ; preds = %28
%35 = and i8 %31, 1
%36 = or i8 %35, 48
store i8 %36, i8* %20
%37 = and i8 %31, 2
%38 = icmp eq i8 %37, 0
%39 = select i1 %38, i8 48, i8 49
store i8 %39, i8* %21
%40 = and i8 %31, 4
%41 = icmp eq i8 %40, 0
%42 = select i1 %41, i8 48, i8 49
store i8 %42, i8* %22
%43 = and i8 %31, 8
%44 = icmp eq i8 %43, 0
%45 = select i1 %44, i8 48, i8 49
store i8 %45, i8* %23
store i8 48, i8* %24
%46 = and i8 %31, 32
%47 = icmp eq i8 %46, 0
%48 = select i1 %47, i8 48, i8 49
store i8 %48, i8* %25
%49 = and i8 %31, 64
%50 = icmp eq i8 %49, 0
%51 = select i1 %50, i8 48, i8 49
store i8 %51, i8* %26
%52 = icmp sgt i8 %31, -1
%53 = select i1 %52, i8 48, i8 49
store i8 %53, i8* %27
store i8 0, i8* %30
%54 = getelementptr inbounds i8, i8* %3, i64 %29
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %56, i8* %10)
br label %58
58: ; preds = %28, %34
%59 = add nuw nsw i64 %29, 1
%60 = icmp eq i64 %59, %19
br i1 %60, label %61, label %28
61: ; preds = %58, %5
%62 = call i32 @fclose(i32* %13)
ret void
}