#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;
struct stat {
  int /*<<< orphan*/ st_size;
};
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 *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ image_checksum32(void *, size_t);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int image_create_payload(void *payload_start, size_t payloadsz,
                         const char *payload_filename) {
  FILE *payload;
  struct stat s;
  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 = stat(payload_filename, &s);
  if (ret < 0) {
    fprintf(stderr, "Cannot stat payload file %s\n", payload_filename);
    fclose(payload);
    return ret;
  }
  ret = fread(payload_start, s.st_size, 1, payload);
  fclose(payload);
  if (ret != 1) {
    fprintf(stderr, "Cannot read payload file %s\n", payload_filename);
    return -1;
  }
  *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,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c4 <image_create_payload+0x64>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe4f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <image_create_payload+0x117>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    120b <image_create_payload+0xab>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe2d(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <image_create_payload+0x117>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x3c(%rbp)
je     125b <image_create_payload+0xfb>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdf2(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <image_create_payload+0x117>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <image_checksum32@plt>
mov    %eax,%ecx
mov    -0x38(%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   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe84(%rip),%rsi
mov    %rdx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d3 <image_create_payload+0x73>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1070 <stat@plt>
test   %eax,%eax
js     11e5 <image_create_payload+0x85>
mov    0x8(%rsp),%esi
mov    %r15,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x1,%ebp
jne    1209 <image_create_payload+0xa9>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <image_checksum32@plt>
mov    %eax,(%r15,%r14,1)
xor    %ebp,%ebp
jmp    1226 <image_create_payload+0xc6>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
jmp    1219 <image_create_payload+0xb9>
mov    %eax,%ebp
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    %r12,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1226 <image_create_payload+0xc6>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    %r12,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.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 stat payload file %s\0A\00"
@.str.3 = [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 %struct.stat
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%12 = load i8*, i8** %5
%13 = load i64, i64* %6
%14 = getelementptr i8, i8* %12, i64 %13
%15 = bitcast i8* %14 to i32*
store i32* %15, i32** %10
%16 = load i8*, i8** %7
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %8
%18 = load i32*, i32** %8
%19 = icmp ne i32* %18, null
br i1 %19, label %24, label %20
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %7
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %4
br label %55
24: ; preds = %3
%25 = load i8*, i8** %7
%26 = call i32 @stat(i8* %25, %struct.stat* %9)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %7
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %11
store i32 %35, i32* %4
br label %55
36: ; preds = %24
%37 = load i8*, i8** %5
%38 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load i32*, i32** %8
%41 = call i32 @fread(i8* %37, i32 %39, i32 1, i32* %40)
store i32 %41, i32* %11
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %11
%45 = icmp ne i32 %44, 1
br i1 %45, label %46, label %50
46: ; preds = %36
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %7
%49 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %48)
store i32 -1, i32* %4
br label %55
50: ; preds = %36
%51 = load i8*, i8** %5
%52 = load i64, i64* %6
%53 = call i32 @image_checksum32(i8* %51, i64 %52)
%54 = load i32*, i32** %10
store i32 %53, i32* %54
store i32 0, i32* %4
br label %55
55: ; preds = %50, %46, %29, %20
%56 = load i32, i32* %4
ret i32 %56
}

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