#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 char *uint32_t;
typedef int uint16_t;
typedef int ssize_t;
typedef int off_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ extract_file(int, char *, char *, char *);
int /*<<< orphan*/ generate_name(char *, char *);
int lseek(int, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, ...);
int main(int argc, char *argv[]) {
  if (argc > 1) {
    char ext[3];
    char fname[9] = "0000.EXT";
    int fdres;
    off_t backupcur;
    ssize_t bytesread;
    uint16_t magicnum;
    uint32_t fbegin;
    uint32_t footerbegin;
    uint32_t fsize;
    fdres = open(argv[1], O_RDONLY);
    read(fdres, &magicnum, 2);
    if (magicnum != 0x921A) {
      printf("Incorrect magic number!\n"
             "Not a IONA ressource file!\n");
      exit(EXIT_FAILURE);
    }
    read(fdres, &footerbegin, 4);
    lseek(fdres, footerbegin, SEEK_SET);
    for (;;) {
      bytesread = read(fdres, &ext, 3);
      if (bytesread < 3) {
        break;
      }
      lseek(fdres, 3, SEEK_CUR);
      read(fdres, &fbegin, 4);
      read(fdres, &fsize, 4);
      backupcur = lseek(fdres, 2, SEEK_CUR);
      generate_name(fname, ext);
      extract_file(fdres, fname, fbegin, fsize);
      lseek(fdres, backupcur, SEEK_SET);
    }
    close(fdres);
    return EXIT_SUCCESS;
  }
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1304 <main+0x184>
mov    0xe59(%rip),%rax
mov    %rax,-0x1c(%rbp)
mov    0xe57(%rip),%al
mov    %al,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1080 <read@plt>
cmpl   $0x921a,-0x2c(%rbp)
je     1207 <main+0x87>
lea    0xe15(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    -0x20(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <lseek@plt>
mov    -0x20(%rbp),%edi
lea    -0x13(%rbp),%rsi
mov    $0x3,%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x3,-0x28(%rbp)
jge    1256 <main+0xd6>
jmp    12eb <main+0x16b>
mov    -0x20(%rbp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
mov    $0x3,%esi
mov    $0x0,%al
call   1060 <lseek@plt>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    -0x20(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    -0x20(%rbp),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
mov    $0x2,%esi
mov    $0x0,%al
call   1060 <lseek@plt>
mov    %eax,-0x24(%rbp)
lea    -0x1c(%rbp),%rdi
lea    -0x13(%rbp),%rsi
call   1030 <generate_name@plt>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x48(%rbp),%rcx
call   1040 <extract_file@plt>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    0x2cf3(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <lseek@plt>
jmp    1231 <main+0xb1>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1310 <main+0x190>
mov    0x2cbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0x2,%edi
jl     12e5 <main+0x165>
movabs $0x5458452e30303030,%rax
45 58 54
mov    %rax,0x10(%rsp)
movb   $0x0,0x18(%rsp)
mov    0x8(%rsi),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x2,%edx
xor    %eax,%eax
call   1080 <read@plt>
cmpl   $0x921a,0xc(%rsp)
jne    12fd <main+0x17d>
lea    0x28(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1080 <read@plt>
mov    0x28(%rsp),%rsi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <lseek@plt>
lea    0x9(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
xor    %eax,%eax
call   1080 <read@plt>
cmp    $0x3,%eax
jl     12d5 <main+0x155>
mov    0x2dbb(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0x9(%rsp),%r13
nopl   0x0(%rax)
mov    (%r14),%edx
mov    %ebx,%edi
mov    $0x3,%esi
xor    %eax,%eax
call   1060 <lseek@plt>
mov    %ebx,%edi
lea    0x30(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1080 <read@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1080 <read@plt>
mov    (%r14),%edx
mov    %ebx,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   1060 <lseek@plt>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <generate_name@plt>
mov    0x30(%rsp),%rdx
mov    0x20(%rsp),%rcx
mov    %ebx,%edi
mov    %r12,%rsi
call   1050 <extract_file@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <lseek@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x3,%edx
xor    %eax,%eax
call   1080 <read@plt>
cmp    $0x2,%eax
jg     1240 <main+0xc0>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2cdd(%rip),%rax
jmp    12ec <main+0x16c>
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd05(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.fname = [9 x i8] c"0000.EXT\00"
@O_RDONLY = global i32 0
@.str = [52 x i8] c"Incorrect magic number!\0ANot a IONA ressource file!\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [3 x i8]
%7 = alloca [9 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %72
17: ; preds = %2
%18 = bitcast [9 x i8]* %7 to i8*
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = call i32 (i32, ...) @read(i32 %24, i32* %11, i32 2)
%26 = load i32, i32* %11
%27 = icmp ne i32 %26, 37402
br i1 %27, label %28, label %32
28: ; preds = %17
%29 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %17
%33 = load i32, i32* %8
%34 = call i32 (i32, ...) @read(i32 %33, i8** %13, i32 4)
%35 = load i32, i32* %8
%36 = load i8*, i8** %13
%37 = load i32, i32* @SEEK_SET
%38 = call i32 (i32, ...) @lseek(i32 %35, i8* %36, i32 %37)
br label %39
39: ; preds = %45, %32
%40 = load i32, i32* %8
%41 = call i32 (i32, ...) @read(i32 %40, [3 x i8]* %6, i32 3)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp slt i32 %42, 3
br i1 %43, label %44, label %45
44: ; preds = %39
br label %68
45: ; preds = %39
%46 = load i32, i32* %8
%47 = load i32, i32* @SEEK_CUR
%48 = call i32 (i32, ...) @lseek(i32 %46, i32 3, i32 %47)
%49 = load i32, i32* %8
%50 = call i32 (i32, ...) @read(i32 %49, i8** %12, i32 4)
%51 = load i32, i32* %8
%52 = call i32 (i32, ...) @read(i32 %51, i8** %14, i32 4)
%53 = load i32, i32* %8
%54 = load i32, i32* @SEEK_CUR
%55 = call i32 (i32, ...) @lseek(i32 %53, i32 2, i32 %54)
store i32 %55, i32* %9
%56 = getelementptr inbounds [9 x i8], [9 x i8]* %7, i64 0, i64 0
%57 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%58 = call i32 @generate_name(i8* %56, i8* %57)
%59 = load i32, i32* %8
%60 = getelementptr inbounds [9 x i8], [9 x i8]* %7, i64 0, i64 0
%61 = load i8*, i8** %12
%62 = load i8*, i8** %14
%63 = call i32 @extract_file(i32 %59, i8* %60, i8* %61, i8* %62)
%64 = load i32, i32* %8
%65 = load i32, i32* %9
%66 = load i32, i32* @SEEK_SET
%67 = call i32 (i32, ...) @lseek(i32 %64, i32 %65, i32 %66)
br label %39
68: ; preds = %44
%69 = load i32, i32* %8
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* @EXIT_SUCCESS
store i32 %71, i32* %3
br label %74
72: ; preds = %2
%73 = load i32, i32* @EXIT_FAILURE
store i32 %73, i32* %3
br label %74
74: ; preds = %72, %68
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@__const.main.fname = [9 x i8] c"0000.EXT\00"
@O_RDONLY = global i32 0
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [51 x i8] c"Incorrect magic number!\0ANot a IONA ressource file!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i8]
%4 = alloca [9 x i8]
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %53
10: ; preds = %2
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%13 = bitcast i32* %5 to i8*
%14 = bitcast i8** %6 to i8*
%15 = bitcast i8** %7 to i8*
%16 = bitcast i8** %8 to i8*
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 @open(i8* %18, i32 %19)
%21 = call i32 (i32, ...) @read(i32 %20, i32* %5, i32 2)
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 37402
br i1 %23, label %28, label %24
24: ; preds = %10
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %10
%29 = call i32 (i32, ...) @read(i32 %20, i8** %7, i32 4)
%30 = load i8*, i8** %7
%31 = load i32, i32* @SEEK_SET
%32 = call i32 (i32, ...) @lseek(i32 %20, i8* %30, i32 %31)
%33 = call i32 (i32, ...) @read(i32 %20, [3 x i8]* %3, i32 3)
%34 = icmp slt i32 %33, 3
br i1 %34, label %50, label %35
35: ; preds = %28, %35
%36 = load i32, i32* @SEEK_CUR
%37 = call i32 (i32, ...) @lseek(i32 %20, i32 3, i32 %36)
%38 = call i32 (i32, ...) @read(i32 %20, i8** %6, i32 4)
%39 = call i32 (i32, ...) @read(i32 %20, i8** %8, i32 4)
%40 = load i32, i32* @SEEK_CUR
%41 = call i32 (i32, ...) @lseek(i32 %20, i32 2, i32 %40)
%42 = call i32 @generate_name(i8* %12, i8* %11)
%43 = load i8*, i8** %6
%44 = load i8*, i8** %8
%45 = call i32 @extract_file(i32 %20, i8* %12, i8* %43, i8* %44)
%46 = load i32, i32* @SEEK_SET
%47 = call i32 (i32, ...) @lseek(i32 %20, i32 %41, i32 %46)
%48 = call i32 (i32, ...) @read(i32 %20, [3 x i8]* %3, i32 3)
%49 = icmp slt i32 %48, 3
br i1 %49, label %50, label %35
50: ; preds = %35, %28
%51 = call i32 @close(i32 %20)
%52 = load i32, i32* @EXIT_SUCCESS
br label %55
53: ; preds = %2
%54 = load i32, i32* @EXIT_FAILURE
br label %55
55: ; preds = %53, %50
%56 = phi i32 [ %52, %50 ], [ %54, %53 ]
ret i32 %56
}