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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ fstat(int, struct stat *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int read(int, void *, size_t);
char *strerror(int /*<<< orphan*/);
ssize_t copy_blob(void *dest, const char *src_file, size_t room) {
  int fd;
  struct stat fd_stat;
  ssize_t bytes;
  fd = open(src_file, O_RDONLY);
  if (fd < 0) {
    printf("Error: %s\n", strerror(errno));
    return -1;
  }
  if (fstat(fd, &fd_stat)) {
    printf("fstat error: %s\n", strerror(errno));
    return -2;
  }
  if (fd_stat.st_size > room) {
    printf("Error: %s will not fit.  Exiting.\n", src_file);
    return -3;
  }
  bytes = read(fd, dest, (size_t)fd_stat.st_size);
  close(fd);
  if (bytes != (ssize_t)fd_stat.st_size) {
    printf("Error while reading %s\n", src_file);
    return -4;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<copy_blob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11bc <copy_blob+0x5c>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <copy_blob+0x111>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11fb <copy_blob+0x9b>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe21(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1271 <copy_blob+0x111>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1225 <copy_blob+0xc5>
mov    -0x18(%rbp),%rsi
lea    0xe08(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1271 <copy_blob+0x111>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%eax
mov    -0x30(%rbp),%rcx
cmp    %ecx,%eax
je     126b <copy_blob+0x10b>
mov    -0x18(%rbp),%rsi
lea    0xde5(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1271 <copy_blob+0x111>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_blob>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11be <copy_blob+0x5e>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
je     11e2 <copy_blob+0x82>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5c(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0xfffffffe,%ebx
jmp    1230 <copy_blob+0xd0>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe2d(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1230 <copy_blob+0xd0>
mov    (%rsp),%r13
cmp    %r15,%r13
jbe    1201 <copy_blob+0xa1>
lea    0xe2a(%rip),%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
mov    $0xfffffffd,%ebx
jmp    1230 <copy_blob+0xd0>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r13,%rdx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    %r13d,%ebx
je     1230 <copy_blob+0xd0>
lea    0xe1c(%rip),%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
mov    $0xfffffffc,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"fstat error: %s\0A\00"
@.str.2 = [35 x i8] c"Error: %s will not fit. Exiting.\0A\00"
@.str.3 = [24 x i8] c"Error while reading %s\0A\00"
define i32 @copy_blob(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %4
br label %54
20: ; preds = %3
%21 = load i32, i32* %8
%22 = call i64 @fstat(i32 %21, %struct.stat* %9)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 -2, i32* %4
br label %54
28: ; preds = %20
%29 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* %7
%32 = icmp ugt i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i8*, i8** %6
%35 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 -3, i32* %4
br label %54
36: ; preds = %28
%37 = load i32, i32* %8
%38 = load i8*, i8** %5
%39 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%40 = load i64, i64* %39
%41 = call i32 @read(i32 %37, i8* %38, i64 %40)
store i32 %41, i32* %10
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %10
%45 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%46 = load i64, i64* %45
%47 = trunc i64 %46 to i32
%48 = icmp ne i32 %44, %47
br i1 %48, label %49, label %52
49: ; preds = %36
%50 = load i8*, i8** %6
%51 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %50)
store i32 -4, i32* %4
br label %54
52: ; preds = %36
%53 = load i32, i32* %10
store i32 %53, i32* %4
br label %54
54: ; preds = %52, %49, %33, %24, %16
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"fstat error: %s\0A\00"
@.str.2 = [35 x i8] c"Error: %s will not fit. Exiting.\0A\00"
@.str.3 = [24 x i8] c"Error while reading %s\0A\00"
define i32 @copy_blob(i8* nocapture %0, i8* %1, i64 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %1, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %33
13: ; preds = %3
%14 = call i64 @fstat(i32 %7, %struct.stat* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %33
20: ; preds = %13
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%22 = load i64, i64* %21
%23 = icmp ugt i64 %22, %2
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %33
26: ; preds = %20
%27 = tail call i32 @read(i32 %7, i8* %0, i64 %22)
%28 = tail call i32 @close(i32 %7)
%29 = trunc i64 %22 to i32
%30 = icmp eq i32 %27, %29
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %33
33: ; preds = %26, %31, %24, %16, %9
%34 = phi i32 [ -1, %9 ], [ -2, %16 ], [ -3, %24 ], [ -4, %31 ], [ %27, %26 ]
ret i32 %34
}