#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 /*<<< orphan*/ dump_sprite_file(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ dump_tile_file(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ usage();
int generate_header(char *outfile, char *type) {
  int do_tile = 0;
  int do_sprite = 0;
  int do_only_header = 0;
  FILE *file;
  if (!strcmp(type, "TILE")) {
    do_tile = 1;
  } else if (!strcmp(type, "SPRITE")) {
    do_sprite = 1;
  } else if (!strcmp(type, "TILEH")) {
    do_tile = 1;
    do_only_header = 1;
  } else if (!strcmp(type, "SPRITEH")) {
    do_sprite = 1;
    do_only_header = 1;
  } else {
    printf("Unsupported output type.\n");
    usage();
    return -1;
  }
  file = fopen(outfile, "w");
  if (file == NULL) {
    fprintf(stderr, "Cannot open %s file for writing\n", outfile);
    return -1;
  }
  if (do_sprite)
    dump_sprite_file(file, do_only_header);
  else if (do_tile)
    dump_tile_file(file, do_only_header);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<generate_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11ca <generate_header+0x4a>
movl   $0x1,-0x1c(%rbp)
jmp    1275 <generate_header+0xf5>
mov    -0x18(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11ef <generate_header+0x6f>
movl   $0x1,-0x20(%rbp)
jmp    1270 <generate_header+0xf0>
mov    -0x18(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    121b <generate_header+0x9b>
movl   $0x1,-0x1c(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    126b <generate_header+0xeb>
mov    -0x18(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1247 <generate_header+0xc7>
movl   $0x1,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1266 <generate_header+0xe6>
lea    0xdcc(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <generate_header+0x17f>
jmp    126b <generate_header+0xeb>
jmp    1270 <generate_header+0xf0>
jmp    1275 <generate_header+0xf5>
mov    -0x10(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b9 <generate_header+0x139>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd8e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <generate_header+0x17f>
cmpl   $0x0,-0x20(%rbp)
je     12d4 <generate_header+0x154>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <dump_sprite_file@plt>
jmp    12ef <generate_header+0x16f>
cmpl   $0x0,-0x1c(%rbp)
je     12ea <generate_header+0x16a>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <dump_tile_file@plt>
jmp    12ef <generate_header+0x16f>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_header>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbp
mov    %rdi,%r12
lea    0xe6b(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11fd <generate_header+0x7d>
lea    0xe5d(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1207 <generate_header+0x87>
lea    0xe51(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strcmp@plt>
mov    $0x1,%r14d
test   %eax,%eax
je     1211 <generate_header+0x91>
lea    0xe3e(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1218 <generate_header+0x98>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <usage@plt>
mov    $0xffffffff,%eax
jmp    127e <generate_header+0xfe>
xor    %r15d,%r15d
mov    $0x1,%bl
xor    %r14d,%r14d
jmp    121d <generate_header+0x9d>
xor    %ebx,%ebx
mov    $0x1,%r15b
xor    %r14d,%r14d
jmp    121d <generate_header+0x9d>
mov    $0x1,%bl
xor    %r15d,%r15d
jmp    121d <generate_header+0x9d>
mov    $0x1,%r15b
xor    %ebx,%ebx
lea    0xdf6(%rip),%rsi
mov    %r12,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     124a <generate_header+0xca>
mov    %rax,%rbp
test   %bl,%bl
je     1269 <generate_header+0xe9>
test   %r15b,%r15b
jne    1274 <generate_header+0xf4>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1050 <dump_tile_file@plt>
jmp    1274 <generate_header+0xf4>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
mov    %r12,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    127e <generate_header+0xfe>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1080 <dump_sprite_file@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"TILE\00"
@.str.1 = [7 x i8] c"SPRITE\00"
@.str.2 = [6 x i8] c"TILEH\00"
@.str.3 = [8 x i8] c"SPRITEH\00"
@.str.4 = [26 x i8] c"Unsupported output type.\0A\00"
@.str.5 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.6 = [33 x i8] c"Cannot open %s file for writing\0A\00"
define i32 @generate_header(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = load i8*, i8** %5
%11 = call i32 @strcmp(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 1, i32* %6
br label %35
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = call i32 @strcmp(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %14
store i32 1, i32* %7
br label %34
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = call i32 @strcmp(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %19
store i32 1, i32* %6
store i32 1, i32* %8
br label %33
24: ; preds = %19
%25 = load i8*, i8** %5
%26 = call i32 @strcmp(i8* %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %24
store i32 1, i32* %7
store i32 1, i32* %8
br label %32
29: ; preds = %24
%30 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %62
32: ; preds = %28
br label %33
33: ; preds = %32, %23
br label %34
34: ; preds = %33, %18
br label %35
35: ; preds = %34, %13
%36 = load i8*, i8** %4
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %37, i32** %9
%38 = load i32*, i32** %9
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %4
%43 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %42)
store i32 -1, i32* %3
br label %62
44: ; preds = %35
%45 = load i32, i32* %7
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32*, i32** %9
%49 = load i32, i32* %8
%50 = call i32 @dump_sprite_file(i32* %48, i32 %49)
br label %59
51: ; preds = %44
%52 = load i32, i32* %6
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i32*, i32** %9
%56 = load i32, i32* %8
%57 = call i32 @dump_tile_file(i32* %55, i32 %56)
br label %58
58: ; preds = %54, %51
br label %59
59: ; preds = %58, %47
%60 = load i32*, i32** %9
%61 = call i32 @fclose(i32* %60)
store i32 0, i32* %3
br label %62
62: ; preds = %59, %40, %29
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [5 x i8] c"TILE\00"
@.str.1 = [7 x i8] c"SPRITE\00"
@.str.2 = [6 x i8] c"TILEH\00"
@.str.3 = [8 x i8] c"SPRITEH\00"
@.str.5 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.6 = [33 x i8] c"Cannot open %s file for writing\0A\00"
@str = [25 x i8] c"Unsupported output type.\00"
define i32 @generate_header(i8* %0, i8* nocapture %1) {
%3 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %2
%6 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %5
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 (...) @usage()
br label %34
17: ; preds = %11, %8, %5, %2
%18 = phi i1 [ false, %2 ], [ true, %5 ], [ false, %8 ], [ true, %11 ]
%19 = phi i1 [ true, %2 ], [ false, %5 ], [ true, %8 ], [ false, %11 ]
%20 = phi i32 [ 0, %2 ], [ 0, %5 ], [ 1, %8 ], [ 1, %11 ]
%21 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %0)
br label %34
26: ; preds = %17
br i1 %19, label %29, label %27
27: ; preds = %26
%28 = tail call i32 @dump_sprite_file(i32* %21, i32 %20)
br label %32
29: ; preds = %26
br i1 %18, label %32, label %30
30: ; preds = %29
%31 = tail call i32 @dump_tile_file(i32* %21, i32 %20)
br label %32
32: ; preds = %29, %30, %27
%33 = tail call i32 @fclose(i32* %21)
br label %34
34: ; preds = %32, %23, %14
%35 = phi i32 [ -1, %14 ], [ -1, %23 ], [ 0, %32 ]
ret i32 %35
}