#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 */
scalar_t__ __mkdir(char *);
char *basepath(char *);
scalar_t__ calloc(int, unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ file_exists(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fwrite(unsigned char const *, unsigned int, int, int /*<<< orphan*/ *);
unsigned int le2int(unsigned char *);
int /*<<< orphan*/ memcpy(char *, ...);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ replace(char *);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int split_hxf(const unsigned char *infile, unsigned int size,
              const char *outpath) {
  FILE *outfile;
  char *filename;
  unsigned int filenamesize, filesize;
  while (size > 0) {
    filenamesize = le2int((unsigned char *)infile);
    infile += 4;
    size -= 4;
    if (size > 0) {
      filename = (char *)calloc(1, filenamesize + 1 + strlen(outpath));
      memcpy(filename, outpath, strlen(outpath));
      memcpy(&filename[strlen(outpath)], infile, filenamesize);
      replace(filename);
      infile += filenamesize + 1;
      size -= filenamesize + 1;
      filesize = le2int((unsigned char *)infile);
      infile += 4;
      size -= 4;
      {
        printf("[INFO] %s\n", basepath(filename));
        if (__mkdir(basepath(filename)) != 0) {
        }
      }
      if (!file_exists(filename)) {
        printf("[INFO] %s: %d bytes\n", filename, filesize);
        if ((outfile = fopen(filename, "wb")) == NULL) {
          fprintf(stderr, "[ERR]  Error opening file %s\n", filename);
          return -1;
        }
        if (filesize > 0) {
          if (fwrite(infile, filesize, 1, outfile) != 1) {
            fclose(outfile);
            fprintf(stderr, "[ERR]  Error writing to file %s\n", filename);
            return -2;
          }
        }
        fclose(outfile);
      }
      infile += filesize;
      size -= filesize;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<split_hxf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jbe    140b <split_hxf+0x23b>
mov    -0x10(%rbp),%rdi
call   10a0 <le2int@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jbe    1406 <split_hxf+0x236>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
add    %rcx,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%edx
xor    %eax,%eax
call   1090 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   10d0 <replace@plt>
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <le2int@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rdi
call   10b0 <basepath@plt>
mov    %rax,%rsi
lea    0xd0a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <basepath@plt>
mov    %rax,%rdi
call   10c0 <__mkdir@plt>
cmp    $0x0,%rax
je     131d <split_hxf+0x14d>
jmp    131d <split_hxf+0x14d>
mov    -0x30(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%eax
jne    13eb <split_hxf+0x21b>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    0xcce(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
lea    0xcd1(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1387 <split_hxf+0x1b7>
mov    0x2c77(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xcad(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1412 <split_hxf+0x242>
cmpl   $0x0,-0x38(%rbp)
jbe    13e2 <split_hxf+0x212>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   10f0 <fwrite@plt>
cmp    $0x1,%eax
je     13dd <split_hxf+0x20d>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2c21(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xc75(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1412 <split_hxf+0x242>
jmp    13e2 <split_hxf+0x212>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11e3 <split_hxf+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_hxf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
test   %esi,%esi
je     1342 <split_hxf+0x172>
mov    %esi,%ebp
mov    %rdi,%r12
jmp    120e <split_hxf+0x3e>
xchg   %ax,%ax
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    %rax,%r12
not    %r13d
add    %r13d,%ebp
sub    %ebx,%ebp
add    $0xfffffff8,%ebp
je     1342 <split_hxf+0x172>
mov    %r12,%rdi
call   10a0 <le2int@plt>
cmp    $0x4,%ebp
je     1342 <split_hxf+0x172>
mov    %eax,%r13d
add    $0x4,%r12
lea    0x1(%r13),%r15d
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r13,1),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <memcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r14,1),%rdi
mov    %r12,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1090 <memcpy@plt>
mov    %r14,%rdi
call   10d0 <replace@plt>
lea    (%r12,%r15,1),%rdi
call   10a0 <le2int@plt>
mov    %eax,%ebx
add    %r15,%r12
add    $0x4,%r12
mov    %r14,%rdi
call   10b0 <basepath@plt>
lea    0xd5c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   10b0 <basepath@plt>
mov    %rax,%rdi
call   10c0 <__mkdir@plt>
mov    %r14,%rdi
call   1030 <file_exists@plt>
test   %eax,%eax
jne    11f8 <split_hxf+0x28>
lea    0xd36(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
lea    0xd35(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1353 <split_hxf+0x183>
mov    %rax,%r15
test   %ebx,%ebx
je     11f0 <split_hxf+0x20>
mov    %r12,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10f0 <fwrite@plt>
cmp    $0x1,%eax
je     11f0 <split_hxf+0x20>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
lea    0xd0e(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    $0xfffffffe,%eax
jmp    1344 <split_hxf+0x174>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c86(%rip),%rax
mov    (%rax),%edi
lea    0xcc0(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1344 <split_hxf+0x174>

LLVM IR (-O0):
@.str = [11 x i8] c"[INFO] %s\0A\00"
@.str.1 = [21 x i8] c"[INFO] %s: %d bytes\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.3 = [30 x i8] c"[ERR] Error opening file %s\0A\00"
@.str.4 = [33 x i8] c"[ERR] Error writing to file %s\0A\00"
define i32 @split_hxf(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
br label %12
12: ; preds = %113, %3
%13 = load i32, i32* %6
%14 = icmp ugt i32 %13, 0
br i1 %14, label %15, label %114
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = call i32 @le2int(i8* %16)
store i32 %17, i32* %10
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 4
store i8* %19, i8** %5
%20 = load i32, i32* %6
%21 = sub i32 %20, 4
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ugt i32 %22, 0
br i1 %23, label %24, label %113
24: ; preds = %15
%25 = load i32, i32* %10
%26 = add i32 %25, 1
%27 = zext i32 %26 to i64
%28 = load i8*, i8** %7
%29 = call i64 @strlen(i8* %28)
%30 = add i64 %27, %29
%31 = trunc i64 %30 to i32
%32 = call i64 @calloc(i32 1, i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %9
%34 = load i8*, i8** %9
%35 = load i8*, i8** %7
%36 = load i8*, i8** %7
%37 = call i64 @strlen(i8* %36)
%38 = call i32 (i8*, ...) @memcpy(i8* %34, i8* %35, i64 %37)
%39 = load i8*, i8** %9
%40 = load i8*, i8** %7
%41 = call i64 @strlen(i8* %40)
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8*, i8** %5
%44 = load i32, i32* %10
%45 = call i32 (i8*, ...) @memcpy(i8* %42, i8* %43, i32 %44)
%46 = load i8*, i8** %9
%47 = call i32 @replace(i8* %46)
%48 = load i32, i32* %10
%49 = add i32 %48, 1
%50 = load i8*, i8** %5
%51 = zext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8* %52, i8** %5
%53 = load i32, i32* %10
%54 = add i32 %53, 1
%55 = load i32, i32* %6
%56 = sub i32 %55, %54
store i32 %56, i32* %6
%57 = load i8*, i8** %5
%58 = call i32 @le2int(i8* %57)
store i32 %58, i32* %11
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i64 4
store i8* %60, i8** %5
%61 = load i32, i32* %6
%62 = sub i32 %61, 4
store i32 %62, i32* %6
%63 = load i8*, i8** %9
%64 = call i8* @basepath(i8* %63)
%65 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %64)
%66 = load i8*, i8** %9
%67 = call i8* @basepath(i8* %66)
%68 = call i64 @__mkdir(i8* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %24
br label %71
71: ; preds = %70, %24
%72 = load i8*, i8** %9
%73 = call i32 @file_exists(i8* %72)
%74 = icmp ne i32 %73, 0
br i1 %74, label %105, label %75
75: ; preds = %71
%76 = load i8*, i8** %9
%77 = load i32, i32* %11
%78 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %76, i32 %77)
%79 = load i8*, i8** %9
%80 = call i32* @fopen(i8* %79, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %80, i32** %8
%81 = icmp eq i32* %80, null
br i1 %81, label %82, label %86
82: ; preds = %75
%83 = load i32, i32* @stderr
%84 = load i8*, i8** %9
%85 = call i32 @fprintf(i32 %83, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %84)
store i32 -1, i32* %4
br label %115
86: ; preds = %75
%87 = load i32, i32* %11
%88 = icmp ugt i32 %87, 0
br i1 %88, label %89, label %102
89: ; preds = %86
%90 = load i8*, i8** %5
%91 = load i32, i32* %11
%92 = load i32*, i32** %8
%93 = call i32 @fwrite(i8* %90, i32 %91, i32 1, i32* %92)
%94 = icmp ne i32 %93, 1
br i1 %94, label %95, label %101
95: ; preds = %89
%96 = load i32*, i32** %8
%97 = call i32 @fclose(i32* %96)
%98 = load i32, i32* @stderr
%99 = load i8*, i8** %9
%100 = call i32 @fprintf(i32 %98, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %99)
store i32 -2, i32* %4
br label %115
101: ; preds = %89
br label %102
102: ; preds = %101, %86
%103 = load i32*, i32** %8
%104 = call i32 @fclose(i32* %103)
br label %105
105: ; preds = %102, %71
%106 = load i32, i32* %11
%107 = load i8*, i8** %5
%108 = zext i32 %106 to i64
%109 = getelementptr inbounds i8, i8* %107, i64 %108
store i8* %109, i8** %5
%110 = load i32, i32* %11
%111 = load i32, i32* %6
%112 = sub i32 %111, %110
store i32 %112, i32* %6
br label %113
113: ; preds = %105, %15
br label %12
114: ; preds = %12
store i32 0, i32* %4
br label %115
115: ; preds = %114, %95, %82
%116 = load i32, i32* %4
ret i32 %116
}

LLVM IR (-O2):
@.str = [11 x i8] c"[INFO] %s\0A\00"
@.str.1 = [21 x i8] c"[INFO] %s: %d bytes\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.3 = [30 x i8] c"[ERR] Error opening file %s\0A\00"
@.str.4 = [33 x i8] c"[ERR] Error writing to file %s\0A\00"
define i32 @split_hxf(i8* %0, i32 %1, i8* %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %62, label %5
5: ; preds = %3, %54
%6 = phi i8* [ %56, %54 ], [ %0, %3 ]
%7 = phi i32 [ %60, %54 ], [ %1, %3 ]
%8 = tail call i32 @le2int(i8* %6)
%9 = getelementptr inbounds i8, i8* %6, i64 4
%10 = icmp eq i32 %7, 4
br i1 %10, label %62, label %11
11: ; preds = %5
%12 = add i32 %8, 1
%13 = zext i32 %12 to i64
%14 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%15 = trunc i64 %14 to i32
%16 = add i32 %12, %15
%17 = tail call i64 @calloc(i32 1, i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%20 = tail call i32 (i8*, ...) @memcpy(i8* %18, i8* %2, i64 %19)
%21 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = tail call i32 (i8*, ...) @memcpy(i8* %22, i8* %9, i32 %8)
%24 = tail call i32 @replace(i8* %18)
%25 = getelementptr inbounds i8, i8* %9, i64 %13
%26 = tail call i32 @le2int(i8* %25)
%27 = getelementptr inbounds i8, i8* %25, i64 4
%28 = tail call i8* @basepath(i8* %18)
%29 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = tail call i8* @basepath(i8* %18)
%31 = tail call i64 @__mkdir(i8* %30)
%32 = tail call i32 @file_exists(i8* %18)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %54
34: ; preds = %11
%35 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %26)
%36 = tail call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = inttoptr i64 %17 to i8*
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %39)
br label %62
42: ; preds = %34
%43 = icmp eq i32 %26, 0
br i1 %43, label %52, label %44
44: ; preds = %42
%45 = tail call i32 @fwrite(i8* %27, i32 %26, i32 1, i32* %36)
%46 = icmp eq i32 %45, 1
br i1 %46, label %52, label %47
47: ; preds = %44
%48 = inttoptr i64 %17 to i8*
%49 = tail call i32 @fclose(i32* %36)
%50 = load i32, i32* @stderr
%51 = tail call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %48)
br label %62
52: ; preds = %44, %42
%53 = tail call i32 @fclose(i32* %36)
br label %54
54: ; preds = %11, %52
%55 = zext i32 %26 to i64
%56 = getelementptr inbounds i8, i8* %27, i64 %55
%57 = xor i32 %8, -1
%58 = add i32 %7, -8
%59 = add i32 %58, %57
%60 = sub i32 %59, %26
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %5
62: ; preds = %5, %54, %3, %47, %38
%63 = phi i32 [ -1, %38 ], [ -2, %47 ], [ 0, %3 ], [ 0, %54 ], [ 0, %5 ]
ret i32 %63
}