#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*/ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(void *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ image_checksum32(void *, size_t);
int /*<<< orphan*/ stderr;
int image_create_payload(void *payload_start, size_t payloadsz,
                         const char *payload_filename) {
  FILE *payload;
  uint32_t *payload_checksum = (uint32_t *)(payload_start + payloadsz);
  int ret;
  payload = fopen(payload_filename, "r");
  if (!payload) {
    fprintf(stderr, "Cannot open payload file %s\n", payload_filename);
    return -1;
  }
  ret = fread(payload_start, payloadsz, 1, payload);
  if (ret != 1) {
    fprintf(stderr, "Cannot read payload file %s\n", payload_filename);
    return -1;
  }
  fclose(payload);
  *payload_checksum = image_checksum32(payload_start, payloadsz);
  return 0;
}


x86 assembly (clang, -O0):
<image_create_payload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b4 <image_create_payload+0x64>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe5f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <image_create_payload+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
je     11fc <image_create_payload+0xac>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <image_create_payload+0xd1>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <image_checksum32@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<image_create_payload>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe98(%rip),%rsi
mov    %rdx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ab <image_create_payload+0x5b>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11bd <image_create_payload+0x6d>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <image_checksum32@plt>
mov    %eax,(%r15,%r14,1)
xor    %eax,%eax
jmp    11da <image_create_payload+0x8a>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
jmp    11cd <image_create_payload+0x7d>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
mov    %r12,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Cannot open payload file %s\0A\00"
@.str.2 = [29 x i8] c"Cannot read payload file %s\0A\00"
define i32 @image_create_payload(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%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** %5
%12 = load i64, i64* %6
%13 = getelementptr i8, i8* %11, i64 %12
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %9
%15 = load i8*, i8** %7
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %7
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 -1, i32* %4
br label %41
23: ; preds = %3
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = load i32*, i32** %8
%27 = call i32 @fread(i8* %24, i64 %25, i32 1, i32* %26)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 1
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %7
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 -1, i32* %4
br label %41
34: ; preds = %23
%35 = load i32*, i32** %8
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %5
%38 = load i64, i64* %6
%39 = call i32 @image_checksum32(i8* %37, i64 %38)
%40 = load i32*, i32** %9
store i32 %39, i32* %40
store i32 0, i32* %4
br label %41
41: ; preds = %34, %30, %19
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Cannot open payload file %s\0A\00"
@.str.2 = [29 x i8] c"Cannot read payload file %s\0A\00"
define i32 @image_create_payload(i8* %0, i64 %1, i8* %2) {
%4 = getelementptr i8, i8* %0, i64 %1
%5 = bitcast i8* %4 to i32*
%6 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %20
11: ; preds = %3
%12 = tail call i32 @fread(i8* %0, i64 %1, i32 1, i32* %6)
%13 = icmp eq i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %20
17: ; preds = %11
%18 = tail call i32 @fclose(i32* %6)
%19 = tail call i32 @image_checksum32(i8* %0, i64 %1)
store i32 %19, i32* %5
br label %20
20: ; preds = %17, %14, %8
%21 = phi i32 [ -1, %14 ], [ 0, %17 ], [ -1, %8 ]
ret i32 %21
}