#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 */
struct stat {
  int /*<<< orphan*/ st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int asprintf(char **, char *, char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
int image_extract_v0(void *, char const *, int /*<<< orphan*/ *);
int image_extract_v1(void *, char const *, int /*<<< orphan*/ *);
int image_version(void *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int image_extract(const char *input, const char *output) {
  int fdi, ret;
  struct stat fdistat, fdostat;
  void *fdimap;
  char *focfgname;
  FILE *focfg;
  fdi = open(input, O_RDONLY);
  if (fdi < 0) {
    fprintf(stderr, "Cannot open input file %s: %m\n", input);
    return -1;
  }
  ret = fstat(fdi, &fdistat);
  if (ret < 0) {
    fprintf(stderr, "Cannot stat input file %s: %m\n", input);
    close(fdi);
    return -1;
  }
  fdimap = mmap(NULL, fdistat.st_size, PROT_READ, MAP_PRIVATE, fdi, 0);
  if (fdimap == MAP_FAILED) {
    fprintf(stderr, "Cannot map input file %s: %m\n", input);
    close(fdi);
    return -1;
  }
  close(fdi);
  ret = stat(output, &fdostat);
  if (ret < 0) {
    fprintf(stderr, "Cannot stat output directory %s: %m\n", output);
    munmap(fdimap, fdistat.st_size);
    return -1;
  }
  if (!S_ISDIR(fdostat.st_mode)) {
    fprintf(stderr, "Output %s should be a directory\n", output);
    munmap(fdimap, fdistat.st_size);
    return -1;
  }
  ret = asprintf(&focfgname, "%s/kwbimage.cfg", output);
  if (ret < 0) {
    fprintf(stderr, "Failed to allocate memory\n");
    munmap(fdimap, fdistat.st_size);
    return -1;
  }
  focfg = fopen(focfgname, "w+");
  if (!focfg) {
    fprintf(stderr, "Output file %s could not be created\n", focfgname);
    free(focfgname);
    munmap(fdimap, fdistat.st_size);
    return -1;
  }
  free(focfgname);
  if (image_version(fdimap) == 0)
    ret = image_extract_v0(fdimap, output, focfg);
  else if (image_version(fdimap) == 1)
    ret = image_extract_v1(fdimap, output, focfg);
  else {
    fprintf(stderr, "Invalid image version %d\n", image_version(fdimap));
    ret = -1;
  }
  fclose(focfg);
  munmap(fdimap, fdistat.st_size);
  return ret;
}


x86 assembly (clang, -O0):
<image_extract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   10f0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1246 <image_extract+0x56>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdcd(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14ec <image_extract+0x2fc>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1110 <fstat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    128e <image_extract+0x9e>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14ec <image_extract+0x2fc>
mov    -0x28(%rbp),%esi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
mov    0x2d27(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2cfe(%rip),%rcx
cmp    (%rcx),%rax
jne    12fa <image_extract+0x10a>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd5f(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14ec <image_extract+0x2fc>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    134f <image_extract+0x15f>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd2c(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10e0 <munmap@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14ec <image_extract+0x2fc>
mov    -0x2c(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1393 <image_extract+0x1a3>
mov    0x2c69(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10e0 <munmap@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14ec <image_extract+0x2fc>
mov    -0x18(%rbp),%rdx
lea    -0x40(%rbp),%rdi
lea    0xd00(%rip),%rsi
call   10d0 <asprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    13e3 <image_extract+0x1f3>
mov    0x2c15(%rip),%rax
mov    (%rax),%edi
lea    0xcee(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10e0 <munmap@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14ec <image_extract+0x2fc>
mov    -0x40(%rbp),%rdi
lea    0xcdf(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    143e <image_extract+0x24e>
mov    0x2bc7(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xcba(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10e0 <munmap@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14ec <image_extract+0x2fc>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1060 <image_version@plt>
cmp    $0x0,%eax
jne    1472 <image_extract+0x282>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <image_extract_v0@plt>
mov    %eax,-0x20(%rbp)
jmp    14d1 <image_extract+0x2e1>
mov    -0x38(%rbp),%rdi
call   1060 <image_version@plt>
cmp    $0x1,%eax
jne    149d <image_extract+0x2ad>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   10c0 <image_extract_v1@plt>
mov    %eax,-0x20(%rbp)
jmp    14cc <image_extract+0x2dc>
mov    0x2b2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <image_version@plt>
mov    -0x4c(%rbp),%edi
mov    %eax,%edx
lea    0xc37(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    14d1 <image_extract+0x2e1>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10e0 <munmap@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<image_extract>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
call   10f0 <open@plt>
test   %eax,%eax
js     130e <image_extract+0x11e>
mov    %eax,%ebp
lea    0x18(%rsp),%rsi
mov    %eax,%edi
call   1110 <fstat@plt>
test   %eax,%eax
js     132a <image_extract+0x13a>
mov    0x18(%rsp),%r14d
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    0x2d85(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%r15
mov    0x2d61(%rip),%rax
cmp    (%rax),%r15
je     133c <image_extract+0x14c>
mov    %ebp,%edi
call   1090 <close@plt>
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
js     135f <image_extract+0x16f>
mov    0x14(%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %eax,%eax
je     1371 <image_extract+0x181>
lea    0xe06(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %r12,%rdx
call   10d0 <asprintf@plt>
test   %eax,%eax
js     13aa <image_extract+0x1ba>
mov    0x8(%rsp),%rdi
lea    0xe10(%rip),%rsi
call   1100 <fopen@plt>
test   %rax,%rax
je     13c3 <image_extract+0x1d3>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1060 <image_version@plt>
mov    %r15,%rdi
test   %eax,%eax
je     13eb <image_extract+0x1fb>
call   1060 <image_version@plt>
cmp    $0x1,%eax
jne    13fa <image_extract+0x20a>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10c0 <image_extract_v1@plt>
mov    %eax,%ebp
jmp    1422 <image_extract+0x232>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
lea    0xce2(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1396 <image_extract+0x1a6>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    0xce5(%rip),%rsi
jmp    134c <image_extract+0x15c>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
lea    0xcf2(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    1396 <image_extract+0x1a6>
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
lea    0xced(%rip),%rsi
jmp    1381 <image_extract+0x191>
mov    0x2c58(%rip),%rax
mov    (%rax),%edi
lea    0xd00(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   10e0 <munmap@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c1f(%rip),%rax
mov    (%rax),%edi
lea    0xcf8(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    138b <image_extract+0x19b>
mov    0x2c06(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xcf8(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    138b <image_extract+0x19b>
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <image_extract_v0@plt>
mov    %eax,%ebp
jmp    1422 <image_extract+0x232>
mov    0x2bcf(%rip),%rax
mov    (%rax),%ebp
mov    %r15,%rdi
call   1060 <image_version@plt>
lea    0xce3(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   10e0 <munmap@plt>
jmp    139b <image_extract+0x1ab>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Cannot open input file %s: %m\0A\00"
@.str.1 = [31 x i8] c"Cannot stat input file %s: %m\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [30 x i8] c"Cannot map input file %s: %m\0A\00"
@.str.3 = [37 x i8] c"Cannot stat output directory %s: %m\0A\00"
@.str.4 = [33 x i8] c"Output %s should be a directory\0A\00"
@.str.5 = [16 x i8] c"%s/kwbimage.cfg\00"
@.str.6 = [27 x i8] c"Failed to allocate memory\0A\00"
@.str.7 = [3 x i8] c"w+\00"
@.str.8 = [37 x i8] c"Output file %s could not be created\0A\00"
@.str.9 = [26 x i8] c"Invalid image version %d\0A\00"
define i32 @image_extract(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca %struct.stat
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %4
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %138
22: ; preds = %2
%23 = load i32, i32* %6
%24 = call i32 @fstat(i32 %23, %struct.stat* %8)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %4
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %138
33: ; preds = %22
%34 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @MAP_PRIVATE
%38 = load i32, i32* %6
%39 = call i8* @mmap(i32* null, i32 %35, i32 %36, i32 %37, i32 %38, i32 0)
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = load i8*, i8** @MAP_FAILED
%42 = icmp eq i8* %40, %41
br i1 %42, label %43, label %49
43: ; preds = %33
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %4
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %3
br label %138
49: ; preds = %33
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i8*, i8** %5
%53 = call i32 @stat(i8* %52, %struct.stat* %9)
store i32 %53, i32* %7
%54 = load i32, i32* %7
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %49
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %5
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %58)
%60 = load i8*, i8** %10
%61 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @munmap(i8* %60, i32 %62)
store i32 -1, i32* %3
br label %138
64: ; preds = %49
%65 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%66 = load i32, i32* %65
%67 = call i32 @S_ISDIR(i32 %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %77, label %69
69: ; preds = %64
%70 = load i32, i32* @stderr
%71 = load i8*, i8** %5
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %71)
%73 = load i8*, i8** %10
%74 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i32 @munmap(i8* %73, i32 %75)
store i32 -1, i32* %3
br label %138
77: ; preds = %64
%78 = load i8*, i8** %5
%79 = call i32 @asprintf(i8** %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %78)
store i32 %79, i32* %7
%80 = load i32, i32* %7
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %89
82: ; preds = %77
%83 = load i32, i32* @stderr
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%85 = load i8*, i8** %10
%86 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%87 = load i32, i32* %86
%88 = call i32 @munmap(i8* %85, i32 %87)
store i32 -1, i32* %3
br label %138
89: ; preds = %77
%90 = load i8*, i8** %11
%91 = call i32* @fopen(i8* %90, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %91, i32** %12
%92 = load i32*, i32** %12
%93 = icmp ne i32* %92, null
br i1 %93, label %104, label %94
94: ; preds = %89
%95 = load i32, i32* @stderr
%96 = load i8*, i8** %11
%97 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0), i8* %96)
%98 = load i8*, i8** %11
%99 = call i32 @free(i8* %98)
%100 = load i8*, i8** %10
%101 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%102 = load i32, i32* %101
%103 = call i32 @munmap(i8* %100, i32 %102)
store i32 -1, i32* %3
br label %138
104: ; preds = %89
%105 = load i8*, i8** %11
%106 = call i32 @free(i8* %105)
%107 = load i8*, i8** %10
%108 = call i32 @image_version(i8* %107)
%109 = icmp eq i32 %108, 0
br i1 %109, label %110, label %115
110: ; preds = %104
%111 = load i8*, i8** %10
%112 = load i8*, i8** %5
%113 = load i32*, i32** %12
%114 = call i32 @image_extract_v0(i8* %111, i8* %112, i32* %113)
store i32 %114, i32* %7
br label %130
115: ; preds = %104
%116 = load i8*, i8** %10
%117 = call i32 @image_version(i8* %116)
%118 = icmp eq i32 %117, 1
br i1 %118, label %119, label %124
119: ; preds = %115
%120 = load i8*, i8** %10
%121 = load i8*, i8** %5
%122 = load i32*, i32** %12
%123 = call i32 @image_extract_v1(i8* %120, i8* %121, i32* %122)
store i32 %123, i32* %7
br label %129
124: ; preds = %115
%125 = load i32, i32* @stderr
%126 = load i8*, i8** %10
%127 = call i32 @image_version(i8* %126)
%128 = call i32 (i32, i8*, ...) @fprintf(i32 %125, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i32 %127)
store i32 -1, i32* %7
br label %129
129: ; preds = %124, %119
br label %130
130: ; preds = %129, %110
%131 = load i32*, i32** %12
%132 = call i32 @fclose(i32* %131)
%133 = load i8*, i8** %10
%134 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%135 = load i32, i32* %134
%136 = call i32 @munmap(i8* %133, i32 %135)
%137 = load i32, i32* %7
store i32 %137, i32* %3
br label %138
138: ; preds = %130, %94, %82, %69, %56, %43, %27, %18
%139 = load i32, i32* %3
ret i32 %139
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Cannot open input file %s: %m\0A\00"
@.str.1 = [31 x i8] c"Cannot stat input file %s: %m\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [30 x i8] c"Cannot map input file %s: %m\0A\00"
@.str.3 = [37 x i8] c"Cannot stat output directory %s: %m\0A\00"
@.str.4 = [33 x i8] c"Output %s should be a directory\0A\00"
@.str.5 = [16 x i8] c"%s/kwbimage.cfg\00"
@.str.6 = [27 x i8] c"Failed to allocate memory\0A\00"
@.str.7 = [3 x i8] c"w+\00"
@.str.8 = [37 x i8] c"Output file %s could not be created\0A\00"
@.str.9 = [26 x i8] c"Invalid image version %d\0A\00"
define i32 @image_extract(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = bitcast %struct.stat* %3 to i8*
%7 = bitcast %struct.stat* %4 to i8*
%8 = bitcast i8** %5 to i8*
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %89
15: ; preds = %2
%16 = call i32 @fstat(i32 %10, %struct.stat* %3)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = tail call i32 @close(i32 %10)
br label %89
22: ; preds = %15
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_PRIVATE
%27 = tail call i8* @mmap(i32* null, i32 %24, i32 %25, i32 %26, i32 %10, i32 0)
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %34
30: ; preds = %22
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%33 = tail call i32 @close(i32 %10)
br label %89
34: ; preds = %22
%35 = tail call i32 @close(i32 %10)
%36 = call i32 @stat(i8* %1, %struct.stat* %4)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%41 = tail call i32 @munmap(i8* %27, i32 %24)
br label %89
42: ; preds = %34
%43 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%44 = load i32, i32* %43
%45 = tail call i32 @S_ISDIR(i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%50 = tail call i32 @munmap(i8* %27, i32 %24)
br label %89
51: ; preds = %42
%52 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %1)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%57 = call i32 @munmap(i8* %27, i32 %24)
br label %89
58: ; preds = %51
%59 = load i8*, i8** %5
%60 = call i32* @fopen(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%61 = icmp eq i32* %60, null
br i1 %61, label %62, label %69
62: ; preds = %58
%63 = load i32, i32* @stderr
%64 = load i8*, i8** %5
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0), i8* %64)
%66 = load i8*, i8** %5
%67 = call i32 @free(i8* %66)
%68 = call i32 @munmap(i8* %27, i32 %24)
br label %89
69: ; preds = %58
%70 = load i8*, i8** %5
%71 = call i32 @free(i8* %70)
%72 = call i32 @image_version(i8* %27)
%73 = icmp eq i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %69
%75 = call i32 @image_extract_v0(i8* %27, i8* %1, i32* %60)
br label %85
76: ; preds = %69
%77 = call i32 @image_version(i8* %27)
%78 = icmp eq i32 %77, 1
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 @image_extract_v1(i8* %27, i8* %1, i32* %60)
br label %85
81: ; preds = %76
%82 = load i32, i32* @stderr
%83 = call i32 @image_version(i8* %27)
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i32 %83)
br label %85
85: ; preds = %79, %81, %74
%86 = phi i32 [ %75, %74 ], [ %80, %79 ], [ -1, %81 ]
%87 = call i32 @fclose(i32* %60)
%88 = call i32 @munmap(i8* %27, i32 %24)
br label %89
89: ; preds = %85, %62, %54, %47, %38, %30, %18, %12
%90 = phi i32 [ -1, %12 ], [ -1, %18 ], [ -1, %30 ], [ -1, %38 ], [ -1, %54 ], [ %86, %85 ], [ -1, %62 ], [ -1, %47 ]
ret i32 %90
}