#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int fwrite(void *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int image_extract_payload(void *payload, size_t sz, const char *output) {
  char *imageoutname;
  FILE *imageout;
  int ret;
  ret = asprintf(&imageoutname, "%s/payload", output);
  if (ret < 0) {
    fprintf(stderr, "Cannot allocate memory\n");
    return -1;
  }
  imageout = fopen(imageoutname, "w+");
  if (!imageout) {
    fprintf(stderr, "Could not open output file %s\n", imageoutname);
    free(imageoutname);
    return -1;
  }
  ret = fwrite(payload, sz, 1, imageout);
  if (ret != 1) {
    fprintf(stderr, "Could not write to open file %s\n", imageoutname);
    fclose(imageout);
    free(imageoutname);
    return -1;
  }
  fclose(imageout);
  free(imageoutname);
  return 0;
}


x86 assembly (clang, -O0):
<image_extract_payload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1060 <asprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11b8 <image_extract_payload+0x58>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <image_extract_payload+0x11c>
mov    -0x28(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1207 <image_extract_payload+0xa7>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <image_extract_payload+0x11c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
je     1263 <image_extract_payload+0x103>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <image_extract_payload+0x11c>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<image_extract_payload>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe8a(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
test   %eax,%eax
js     11cb <image_extract_payload+0x6b>
mov    0x8(%rsp),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e4 <image_extract_payload+0x84>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
jne    1202 <image_extract_payload+0xa2>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1235 <image_extract_payload+0xd5>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1230 <image_extract_payload+0xd0>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xe2d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1226 <image_extract_payload+0xc6>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xe2e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s/payload\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Cannot allocate memory\0A\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [31 x i8] c"Could not open output file %s\0A\00"
@.str.4 = [33 x i8] c"Could not write to open file %s\0A\00"
define i32 @image_extract_payload(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%11 = load i8*, i8** %7
%12 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %49
18: ; preds = %3
%19 = load i8*, i8** %8
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = icmp ne i32* %21, null
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %8
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %8
%28 = call i32 @free(i8* %27)
store i32 -1, i32* %4
br label %49
29: ; preds = %18
%30 = load i8*, i8** %5
%31 = load i64, i64* %6
%32 = load i32*, i32** %9
%33 = call i32 @fwrite(i8* %30, i64 %31, i32 1, i32* %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %44
36: ; preds = %29
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %8
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %9
%41 = call i32 @fclose(i32* %40)
%42 = load i8*, i8** %8
%43 = call i32 @free(i8* %42)
store i32 -1, i32* %4
br label %49
44: ; preds = %29
%45 = load i32*, i32** %9
%46 = call i32 @fclose(i32* %45)
%47 = load i8*, i8** %8
%48 = call i32 @free(i8* %47)
store i32 0, i32* %4
br label %49
49: ; preds = %44, %36, %23, %15
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s/payload\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Cannot allocate memory\0A\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [31 x i8] c"Could not open output file %s\0A\00"
@.str.4 = [33 x i8] c"Could not write to open file %s\0A\00"
define i32 @image_extract_payload(i8* nocapture %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %35
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
br label %35
21: ; preds = %11
%22 = call i32 @fwrite(i8* %0, i64 %1, i32 1, i32* %13)
%23 = icmp eq i32 %22, 1
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %4
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = call i32 @fclose(i32* %13)
%29 = load i8*, i8** %4
%30 = call i32 @free(i8* %29)
br label %35
31: ; preds = %21
%32 = call i32 @fclose(i32* %13)
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %31, %24, %15, %8
%36 = phi i32 [ -1, %8 ], [ -1, %24 ], [ 0, %31 ], [ -1, %15 ]
ret i32 %36
}