#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 asprintf(char **, char *, char const *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ free(char *);
int fwrite(void const *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
int image_extract_binary_hdr_v1(const void *binary, const char *output,
                                FILE *focfg, int hdrnum, size_t binsz) {
  char *binaryoutname;
  FILE *binaryout;
  const unsigned int *args;
  unsigned int nargs;
  int ret, i;
  args = binary;
  nargs = args[0];
  args++;
  ret = asprintf(&binaryoutname, "%s/binary.%d", output, hdrnum);
  if (ret < 0) {
    fprintf(stderr, "Couldn't not allocate memory\n");
    return ret;
  }
  binaryout = fopen(binaryoutname, "w+");
  if (!binaryout) {
    fprintf(stderr, "Couldn't open output file %s\n", binaryoutname);
    free(binaryoutname);
    return -1;
  }
  ret = fwrite(binary + (nargs + 1) * sizeof(unsigned int),
               binsz - (nargs + 1) * sizeof(unsigned int), 1, binaryout);
  if (ret != 1) {
    fprintf(stderr, "Could not write to output file %s\n", binaryoutname);
    fclose(binaryout);
    free(binaryoutname);
    return -1;
  }
  fclose(binaryout);
  fprintf(focfg, "BINARY %s", binaryoutname);
  for (i = 0; i < nargs; i++)
    fprintf(focfg, " %08x", args[i]);
  fprintf(focfg, "\n");
  free(binaryoutname);
  return 0;
}


x86 assembly (clang, -O0):
<image_extract_binary_hdr_v1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    -0x38(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1060 <asprintf@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    11df <image_extract_binary_hdr_v1+0x7f>
mov    0x2e1d(%rip),%rax
mov    (%rax),%rdi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1329 <image_extract_binary_hdr_v1+0x1c9>
mov    -0x38(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    122f <image_extract_binary_hdr_v1+0xcf>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rdx
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <image_extract_binary_hdr_v1+0x1c9>
mov    -0x10(%rbp),%rdi
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x2,%rax
sub    %rax,%rsi
mov    -0x40(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x1,-0x50(%rbp)
je     12aa <image_extract_binary_hdr_v1+0x14a>
mov    0x2d69(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rdx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <image_extract_binary_hdr_v1+0x1c9>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jae    1307 <image_extract_binary_hdr_v1+0x1a7>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12d0 <image_extract_binary_hdr_v1+0x170>
mov    -0x20(%rbp),%rdi
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<image_extract_binary_hdr_v1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r15
mov    %rdx,%r14
mov    %rsi,%rdx
mov    %rdi,%rbx
mov    (%rdi),%r13d
lea    0xe7f(%rip),%rsi
mov    %rsp,%rdi
call   1060 <asprintf@plt>
test   %eax,%eax
js     124e <image_extract_binary_hdr_v1+0xee>
mov    (%rsp),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1273 <image_extract_binary_hdr_v1+0x113>
mov    %rax,%r12
lea    0x1(%r13),%eax
lea    (%rbx,%rax,4),%rdi
shl    $0x2,%rax
sub    %rax,%r15
mov    %r15,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
jne    1291 <image_extract_binary_hdr_v1+0x131>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    (%rsp),%rdx
lea    0xe87(%rip),%rsi
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r13,%r13
je     122a <image_extract_binary_hdr_v1+0xca>
lea    0xe78(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x4(%rbx,%rbp,4),%edx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1210 <image_extract_binary_hdr_v1+0xb0>
lea    0xe19(%rip),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    12c4 <image_extract_binary_hdr_v1+0x164>
mov    %eax,%r12d
mov    0x2d88(%rip),%rax
mov    (%rax),%rcx
lea    0xdab(%rip),%rdi
mov    $0x1d,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    12c4 <image_extract_binary_hdr_v1+0x164>
mov    0x2d66(%rip),%rax
mov    (%rax),%rdi
mov    (%rsp),%rdx
lea    0xda6(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12b5 <image_extract_binary_hdr_v1+0x155>
mov    0x2d48(%rip),%rax
mov    (%rax),%rdi
mov    (%rsp),%rdx
lea    0xda6(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"%s/binary.%d\00"
@stderr = global i32* null
@.str.1 = [30 x i8] c"Couldn't not allocate memory\0A\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [30 x i8] c"Couldn't open output file %s\0A\00"
@.str.4 = [35 x i8] c"Could not write to output file %s\0A\00"
@.str.5 = [10 x i8] c"BINARY %s\00"
@.str.6 = [6 x i8] c" %08x\00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @image_extract_binary_hdr_v1(i8* %0, i8* %1, i32* %2, i32 %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32* %2, i32** %9
store i32 %3, i32* %10
store i64 %4, i64* %11
%18 = load i8*, i8** %7
%19 = bitcast i8* %18 to i32*
store i32* %19, i32** %14
%20 = load i32*, i32** %14
%21 = getelementptr inbounds i32, i32* %20, i64 0
%22 = load i32, i32* %21
store i32 %22, i32* %15
%23 = load i32*, i32** %14
%24 = getelementptr inbounds i32, i32* %23, i32 1
store i32* %24, i32** %14
%25 = load i8*, i8** %8
%26 = load i32, i32* %10
%27 = call i32 @asprintf(i8** %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %26)
store i32 %27, i32* %16
%28 = load i32, i32* %16
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %5
%31 = load i32*, i32** @stderr
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* %16
store i32 %33, i32* %6
br label %96
34: ; preds = %5
%35 = load i8*, i8** %12
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %36, i32** %13
%37 = load i32*, i32** %13
%38 = icmp ne i32* %37, null
br i1 %38, label %45, label %39
39: ; preds = %34
%40 = load i32*, i32** @stderr
%41 = load i8*, i8** %12
%42 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = load i8*, i8** %12
%44 = call i32 @free(i8* %43)
store i32 -1, i32* %6
br label %96
45: ; preds = %34
%46 = load i8*, i8** %7
%47 = load i32, i32* %15
%48 = add i32 %47, 1
%49 = zext i32 %48 to i64
%50 = mul i64 %49, 4
%51 = getelementptr i8, i8* %46, i64 %50
%52 = load i64, i64* %11
%53 = load i32, i32* %15
%54 = add i32 %53, 1
%55 = zext i32 %54 to i64
%56 = mul i64 %55, 4
%57 = sub i64 %52, %56
%58 = load i32*, i32** %13
%59 = call i32 @fwrite(i8* %51, i64 %57, i32 1, i32* %58)
store i32 %59, i32* %16
%60 = load i32, i32* %16
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %70
62: ; preds = %45
%63 = load i32*, i32** @stderr
%64 = load i8*, i8** %12
%65 = call i32 (i32*, i8*, ...) @fprintf(i32* %63, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %64)
%66 = load i32*, i32** %13
%67 = call i32 @fclose(i32* %66)
%68 = load i8*, i8** %12
%69 = call i32 @free(i8* %68)
store i32 -1, i32* %6
br label %96
70: ; preds = %45
%71 = load i32*, i32** %13
%72 = call i32 @fclose(i32* %71)
%73 = load i32*, i32** %9
%74 = load i8*, i8** %12
%75 = call i32 (i32*, i8*, ...) @fprintf(i32* %73, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %74)
store i32 0, i32* %17
br label %76
76: ; preds = %88, %70
%77 = load i32, i32* %17
%78 = load i32, i32* %15
%79 = icmp ult i32 %77, %78
br i1 %79, label %80, label %91
80: ; preds = %76
%81 = load i32*, i32** %9
%82 = load i32*, i32** %14
%83 = load i32, i32* %17
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i32, i32* %82, i64 %84
%86 = load i32, i32* %85
%87 = call i32 (i32*, i8*, ...) @fprintf(i32* %81, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %86)
br label %88
88: ; preds = %80
%89 = load i32, i32* %17
%90 = add nsw i32 %89, 1
store i32 %90, i32* %17
br label %76
91: ; preds = %76
%92 = load i32*, i32** %9
%93 = call i32 (i32*, i8*, ...) @fprintf(i32* %92, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%94 = load i8*, i8** %12
%95 = call i32 @free(i8* %94)
store i32 0, i32* %6
br label %96
96: ; preds = %91, %62, %39, %30
%97 = load i32, i32* %6
ret i32 %97
}

LLVM IR (-O2):
@.str = [13 x i8] c"%s/binary.%d\00"
@stderr = global i32* null
@.str.1 = [30 x i8] c"Couldn't not allocate memory\0A\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [30 x i8] c"Couldn't open output file %s\0A\00"
@.str.4 = [35 x i8] c"Could not write to output file %s\0A\00"
@.str.5 = [10 x i8] c"BINARY %s\00"
@.str.6 = [6 x i8] c" %08x\00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @image_extract_binary_hdr_v1(i8* nocapture %0, i8* %1, i32* %2, i32 %3, i64 %4) {
%6 = alloca i8*
%7 = bitcast i8** %6 to i8*
%8 = bitcast i8* %0 to i32*
%9 = load i32, i32* %8
%10 = getelementptr inbounds i8, i8* %0, i64 4
%11 = bitcast i8* %10 to i32*
%12 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %5
%15 = load i32*, i32** @stderr
%16 = call i64 bitcast (i32 (i8*, i64, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i64 29, i64 1, i32* %15)
br label %60
17: ; preds = %5
%18 = load i8*, i8** %6
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i32*, i32** @stderr
%23 = load i8*, i8** %6
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
br label %60
27: ; preds = %17
%28 = add i32 %9, 1
%29 = zext i32 %28 to i64
%30 = shl nuw nsw i64 %29, 2
%31 = getelementptr i8, i8* %0, i64 %30
%32 = sub i64 %4, %30
%33 = call i32 @fwrite(i8* %31, i64 %32, i32 1, i32* %19)
%34 = icmp eq i32 %33, 1
br i1 %34, label %42, label %35
35: ; preds = %27
%36 = load i32*, i32** @stderr
%37 = load i8*, i8** %6
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %37)
%39 = call i32 @fclose(i32* %19)
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
br label %60
42: ; preds = %27
%43 = call i32 @fclose(i32* %19)
%44 = load i8*, i8** %6
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %44)
%46 = icmp eq i32 %9, 0
br i1 %46, label %56, label %47
47: ; preds = %42
%48 = zext i32 %9 to i64
br label %49
49: ; preds = %47, %49
%50 = phi i64 [ 0, %47 ], [ %54, %49 ]
%51 = getelementptr inbounds i32, i32* %11, i64 %50
%52 = load i32, i32* %51
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %52)
%54 = add nuw nsw i64 %50, 1
%55 = icmp eq i64 %54, %48
br i1 %55, label %56, label %49
56: ; preds = %49, %42
%57 = call i64 bitcast (i32 (i8*, i64, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i64 1, i64 1, i32* %2)
%58 = load i8*, i8** %6
%59 = call i32 @free(i8* %58)
br label %60
60: ; preds = %56, %35, %21, %14
%61 = phi i32 [ %12, %14 ], [ -1, %35 ], [ 0, %56 ], [ -1, %21 ]
ret i32 %61
}