#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 blockhunk(int /*<<< orphan*/ *);
int bsshunk(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
unsigned long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int reloc16hunk(int /*<<< orphan*/ *);
int reloc32hunk(int /*<<< orphan*/ *);
int symbolhunk(int /*<<< orphan*/ *);
int hunkformat(unsigned long type, unsigned long lwsize, FILE *fp) {
  printf("Position = %ld\n", ftell(fp) - 4);
  if (type == 0) {
    printf("Null Hunk\n");
    fclose(fp);
    exit(2);
  } else if (type == 0x3E9) {
    printf("HUNK_CODE (0x%X)\n", type);
    return blockhunk(fp);
  } else if (type == 0x3EA) {
    printf("HUNK_DATA (0x%X)\n", type);
    return blockhunk(fp);
  } else if (type == 0x3EB) {
    printf("HUNK_BSS (0x%X)\n", type);
    return bsshunk(fp);
  } else if (type == 0x3EC) {
    printf("HUNK_RELOC32 (0x%X)\n", type);
    return reloc32hunk(fp);
  } else if (type == 0x3F0) {
    printf("HUNK_SYMBOL (0x%X)\n", type);
    return symbolhunk(fp);
  } else if (type == 0x3F7) {
    printf("HUNK_DREL32 (0x%X)\n", type);
    return reloc16hunk(fp);
  } else if (type == 0x3F2) {
    printf("HUNK_END (0x%X)\n", type);
    return 1;
  } else {
    printf("UNKNOWN HUNK TYPE (0x%X)\n", type);
    fclose(fp);
    exit(2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<hunkformat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,%rsi
sub    $0x4,%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11ee <hunkformat+0x5e>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
cmpq   $0x3e9,-0x10(%rbp)
jne    121f <hunkformat+0x8f>
mov    -0x10(%rbp),%rsi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <blockhunk@plt>
mov    %eax,-0x4(%rbp)
jmp    1365 <hunkformat+0x1d5>
cmpq   $0x3ea,-0x10(%rbp)
jne    1250 <hunkformat+0xc0>
mov    -0x10(%rbp),%rsi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <blockhunk@plt>
mov    %eax,-0x4(%rbp)
jmp    1365 <hunkformat+0x1d5>
cmpq   $0x3eb,-0x10(%rbp)
jne    1281 <hunkformat+0xf1>
mov    -0x10(%rbp),%rsi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <bsshunk@plt>
mov    %eax,-0x4(%rbp)
jmp    1365 <hunkformat+0x1d5>
cmpq   $0x3ec,-0x10(%rbp)
jne    12b2 <hunkformat+0x122>
mov    -0x10(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1090 <reloc32hunk@plt>
mov    %eax,-0x4(%rbp)
jmp    1365 <hunkformat+0x1d5>
cmpq   $0x3f0,-0x10(%rbp)
jne    12e3 <hunkformat+0x153>
mov    -0x10(%rbp),%rsi
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <symbolhunk@plt>
mov    %eax,-0x4(%rbp)
jmp    1365 <hunkformat+0x1d5>
cmpq   $0x3f7,-0x10(%rbp)
jne    1314 <hunkformat+0x184>
mov    -0x10(%rbp),%rsi
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <reloc16hunk@plt>
mov    %eax,-0x4(%rbp)
jmp    1365 <hunkformat+0x1d5>
cmpq   $0x3f2,-0x10(%rbp)
jne    1340 <hunkformat+0x1b0>
mov    -0x10(%rbp),%rsi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <hunkformat+0x1d5>
mov    -0x10(%rbp),%rsi
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hunkformat>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
mov    %rdx,%rdi
call   1090 <ftell@plt>
lea    -0x4(%rax),%rsi
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    -0x3e9(%rbx),%rax
cmp    $0xe,%rax
ja     12bd <hunkformat+0x11d>
lea    0xe24(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe60(%rip),%rdi
mov    $0x3e9,%esi
jmp    11ff <hunkformat+0x5f>
lea    0xe64(%rip),%rdi
mov    $0x3ea,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    10b0 <blockhunk@plt>
lea    0xe54(%rip),%rdi
mov    $0x3eb,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <bsshunk@plt>
lea    0xe43(%rip),%rdi
mov    $0x3ec,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    10a0 <reloc32hunk@plt>
lea    0xe36(%rip),%rdi
mov    $0x3f0,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <symbolhunk@plt>
lea    0xe3c(%rip),%rdi
mov    $0x3f2,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe08(%rip),%rdi
mov    $0x3f7,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <reloc16hunk@plt>
test   %rbx,%rbx
jne    12d0 <hunkformat+0x130>
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
jmp    12e1 <hunkformat+0x141>
lea    0xdf8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Position = %ld\0A\00"
@.str.1 = [11 x i8] c"Null Hunk\0A\00"
@.str.2 = [18 x i8] c"HUNK_CODE (0x%X)\0A\00"
@.str.3 = [18 x i8] c"HUNK_DATA (0x%X)\0A\00"
@.str.4 = [17 x i8] c"HUNK_BSS (0x%X)\0A\00"
@.str.5 = [21 x i8] c"HUNK_RELOC32 (0x%X)\0A\00"
@.str.6 = [20 x i8] c"HUNK_SYMBOL (0x%X)\0A\00"
@.str.7 = [20 x i8] c"HUNK_DREL32 (0x%X)\0A\00"
@.str.8 = [17 x i8] c"HUNK_END (0x%X)\0A\00"
@.str.9 = [26 x i8] c"UNKNOWN HUNK TYPE (0x%X)\0A\00"
define i32 @hunkformat(i64 %0, i64 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
store i64 %0, i64* %5
store i64 %1, i64* %6
store i32* %2, i32** %7
%8 = load i32*, i32** %7
%9 = call i64 @ftell(i32* %8)
%10 = sub i64 %9, 4
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %10)
%12 = load i64, i64* %5
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32*, i32** %7
%17 = call i32 @fclose(i32* %16)
%18 = call i32 @exit(i32 2)
unreachable
19: ; preds = %3
%20 = load i64, i64* %5
%21 = icmp eq i64 %20, 1001
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i64, i64* %5
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %23)
%25 = load i32*, i32** %7
%26 = call i32 @blockhunk(i32* %25)
store i32 %26, i32* %4
br label %79
27: ; preds = %19
%28 = load i64, i64* %5
%29 = icmp eq i64 %28, 1002
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i64, i64* %5
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %31)
%33 = load i32*, i32** %7
%34 = call i32 @blockhunk(i32* %33)
store i32 %34, i32* %4
br label %79
35: ; preds = %27
%36 = load i64, i64* %5
%37 = icmp eq i64 %36, 1003
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i64, i64* %5
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 %39)
%41 = load i32*, i32** %7
%42 = call i32 @bsshunk(i32* %41)
store i32 %42, i32* %4
br label %79
43: ; preds = %35
%44 = load i64, i64* %5
%45 = icmp eq i64 %44, 1004
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i64, i64* %5
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i64 %47)
%49 = load i32*, i32** %7
%50 = call i32 @reloc32hunk(i32* %49)
store i32 %50, i32* %4
br label %79
51: ; preds = %43
%52 = load i64, i64* %5
%53 = icmp eq i64 %52, 1008
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i64, i64* %5
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %55)
%57 = load i32*, i32** %7
%58 = call i32 @symbolhunk(i32* %57)
store i32 %58, i32* %4
br label %79
59: ; preds = %51
%60 = load i64, i64* %5
%61 = icmp eq i64 %60, 1015
br i1 %61, label %62, label %67
62: ; preds = %59
%63 = load i64, i64* %5
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i64 %63)
%65 = load i32*, i32** %7
%66 = call i32 @reloc16hunk(i32* %65)
store i32 %66, i32* %4
br label %79
67: ; preds = %59
%68 = load i64, i64* %5
%69 = icmp eq i64 %68, 1010
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i64, i64* %5
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i64 %71)
store i32 1, i32* %4
br label %79
73: ; preds = %67
%74 = load i64, i64* %5
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i64 %74)
%76 = load i32*, i32** %7
%77 = call i32 @fclose(i32* %76)
%78 = call i32 @exit(i32 2)
unreachable
79: ; preds = %70, %62, %54, %46, %38, %30, %22
%80 = load i32, i32* %4
ret i32 %80
}

LLVM IR (-O2):
@.str = [16 x i8] c"Position = %ld\0A\00"
@.str.2 = [18 x i8] c"HUNK_CODE (0x%X)\0A\00"
@.str.3 = [18 x i8] c"HUNK_DATA (0x%X)\0A\00"
@.str.4 = [17 x i8] c"HUNK_BSS (0x%X)\0A\00"
@.str.5 = [21 x i8] c"HUNK_RELOC32 (0x%X)\0A\00"
@.str.6 = [20 x i8] c"HUNK_SYMBOL (0x%X)\0A\00"
@.str.7 = [20 x i8] c"HUNK_DREL32 (0x%X)\0A\00"
@.str.8 = [17 x i8] c"HUNK_END (0x%X)\0A\00"
@.str.9 = [26 x i8] c"UNKNOWN HUNK TYPE (0x%X)\0A\00"
@str = [10 x i8] c"Null Hunk\00"
define i32 @hunkformat(i64 %0, i64 %1, i32* %2) {
%4 = tail call i64 @ftell(i32* %2)
%5 = add i64 %4, -4
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %5)
switch i64 %0, label %31 [
i64 0, label %7
i64 1001, label %11
i64 1002, label %14
i64 1003, label %17
i64 1004, label %20
i64 1008, label %23
i64 1015, label %26
i64 1010, label %29
]
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @fclose(i32* %2)
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %3
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 1001)
%13 = tail call i32 @blockhunk(i32* %2)
br label %35
14: ; preds = %3
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 1002)
%16 = tail call i32 @blockhunk(i32* %2)
br label %35
17: ; preds = %3
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 1003)
%19 = tail call i32 @bsshunk(i32* %2)
br label %35
20: ; preds = %3
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i64 1004)
%22 = tail call i32 @reloc32hunk(i32* %2)
br label %35
23: ; preds = %3
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 1008)
%25 = tail call i32 @symbolhunk(i32* %2)
br label %35
26: ; preds = %3
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i64 1015)
%28 = tail call i32 @reloc16hunk(i32* %2)
br label %35
29: ; preds = %3
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i64 1010)
br label %35
31: ; preds = %3
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i64 %0)
%33 = tail call i32 @fclose(i32* %2)
%34 = tail call i32 @exit(i32 2)
unreachable
35: ; preds = %29, %26, %23, %20, %17, %14, %11
%36 = phi i32 [ %13, %11 ], [ %16, %14 ], [ %19, %17 ], [ %22, %20 ], [ %25, %23 ], [ %28, %26 ], [ 1, %29 ]
ret i32 %36
}