#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*/ uint8_t;
struct stat {
  int st_size;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int fstat(int, struct stat *);
int /*<<< orphan*/ *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
uint8_t *load_container(char *filename, ssize_t *buf_size) {
  int fd, n;
  uint8_t *buf = NULL;
  struct stat stat_buf;
  fd = open(filename, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "can't open %s\n", filename);
    return NULL;
  }
  if (fstat(fd, &stat_buf) == -1) {
    fprintf(stderr, "can't stat %s\n", filename);
    close(fd);
    return NULL;
  }
  buf = malloc(stat_buf.st_size);
  if (buf == NULL) {
    fprintf(stderr, "can't malloc\n");
    close(fd);
    return NULL;
  }
  n = read(fd, buf, stat_buf.st_size);
  if (n != stat_buf.st_size) {
    fprintf(stderr, "can't read %s\n", filename);
    close(fd);
    free(buf);
    return NULL;
  }
  close(fd);
  *buf_size = n;
  return buf;
}


x86 assembly (clang, -O0):
<load_container>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11cf <load_container+0x5f>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <load_container+0x157>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1214 <load_container+0xa4>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <load_container+0x157>
mov    -0x30(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1257 <load_container+0xe7>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <load_container+0x157>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     12ae <load_container+0x13e>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <load_container+0x157>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_container>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11de <load_container+0x6e>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     11fd <load_container+0x8d>
mov    0x8(%rsp),%r12d
mov    %r12d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     121c <load_container+0xac>
mov    %rax,%r14
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r12d,%edx
call   1050 <read@plt>
cmp    %r12d,%eax
jne    123f <load_container+0xcf>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12d,(%r15)
jmp    126b <load_container+0xfb>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    126b <load_container+0xfb>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1236 <load_container+0xc6>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    126b <load_container+0xfb>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"can't open %s\0A\00"
@.str.1 = [15 x i8] c"can't stat %s\0A\00"
@.str.2 = [14 x i8] c"can't malloc\0A\00"
@.str.3 = [15 x i8] c"can't read %s\0A\00"
define i32* @load_container(i8* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* null, i32** %8
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32* null, i32** %3
br label %64
19: ; preds = %2
%20 = load i32, i32* %6
%21 = call i32 @fstat(i32 %20, %struct.stat* %9)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %4
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
store i32* null, i32** %3
br label %64
29: ; preds = %19
%30 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32* @malloc(i32 %31)
store i32* %32, i32** %8
%33 = load i32*, i32** %8
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
store i32* null, i32** %3
br label %64
40: ; preds = %29
%41 = load i32, i32* %6
%42 = load i32*, i32** %8
%43 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @read(i32 %41, i32* %42, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%48 = load i32, i32* %47
%49 = icmp ne i32 %46, %48
br i1 %49, label %50, label %58
50: ; preds = %40
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %4
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
%56 = load i32*, i32** %8
%57 = call i32 @free(i32* %56)
store i32* null, i32** %3
br label %64
58: ; preds = %40
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %7
%62 = load i32*, i32** %5
store i32 %61, i32* %62
%63 = load i32*, i32** %8
store i32* %63, i32** %3
br label %64
64: ; preds = %58, %50, %35, %23, %15
%65 = load i32*, i32** %3
ret i32* %65
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"can't open %s\0A\00"
@.str.1 = [15 x i8] c"can't stat %s\0A\00"
@.str.2 = [14 x i8] c"can't malloc\0A\00"
@.str.3 = [15 x i8] c"can't read %s\0A\00"
define noalias i32* @load_container(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %37
11: ; preds = %2
%12 = call i32 @fstat(i32 %6, %struct.stat* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%17 = tail call i32 @close(i32 %6)
br label %37
18: ; preds = %11
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i32* @malloc(i32 %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @close(i32 %6)
br label %37
27: ; preds = %18
%28 = tail call i32 @read(i32 %6, i32* %21, i32 %20)
%29 = icmp eq i32 %28, %20
br i1 %29, label %35, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%33 = tail call i32 @close(i32 %6)
%34 = tail call i32 @free(i32* %21)
br label %37
35: ; preds = %27
%36 = tail call i32 @close(i32 %6)
store i32 %20, i32* %1
br label %37
37: ; preds = %35, %30, %23, %14, %8
%38 = phi i32* [ null, %8 ], [ null, %14 ], [ null, %23 ], [ null, %30 ], [ %21, %35 ]
ret i32* %38
}