#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 {
  scalar_t__ st_size;
};
typedef int ssize_t;

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


x86 assembly (clang, -O0):
<copy_blob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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    11d9 <copy_blob+0x79>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <copy_blob+0x165>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     1231 <copy_blob+0xd1>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12c5 <copy_blob+0x165>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    126e <copy_blob+0x10e>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12c5 <copy_blob+0x165>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x34(%rbp),%eax
mov    -0x30(%rbp),%rcx
cmp    %ecx,%eax
je     12bf <copy_blob+0x15f>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12c5 <copy_blob+0x165>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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     11d5 <copy_blob+0x75>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
je     1209 <copy_blob+0xa9>
mov    0x2e37(%rip),%rax
mov    (%rax),%ebx
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe64(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xfffffffe,%ebx
jmp    1274 <copy_blob+0x114>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ebx
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe0d(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1274 <copy_blob+0x114>
mov    (%rsp),%r13
cmp    %r15,%r13
jbe    123a <copy_blob+0xda>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xfffffffd,%ebx
jmp    1274 <copy_blob+0x114>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r13,%rdx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
cmp    %r13d,%ebx
je     1274 <copy_blob+0x114>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@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
@stderr = global i32 0
@.str = [28 x i8] c"Error opening file: %s: %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 %22
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %6
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %20)
store i32 -1, i32* %4
br label %63
22: ; preds = %3
%23 = load i32, i32* %8
%24 = call i64 @fstat(i32 %23, %struct.stat* %9)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
store i32 -2, i32* %4
br label %63
33: ; preds = %22
%34 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* %7
%37 = icmp ugt i64 %35, %36
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %6
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
store i32 -3, i32* %4
br label %63
44: ; preds = %33
%45 = load i32, i32* %8
%46 = load i8*, i8** %5
%47 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%48 = load i64, i64* %47
%49 = call i32 @read(i32 %45, i8* %46, i64 %48)
store i32 %49, i32* %10
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %10
%53 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%54 = load i64, i64* %53
%55 = trunc i64 %54 to i32
%56 = icmp ne i32 %52, %55
br i1 %56, label %57, label %61
57: ; preds = %44
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %6
%60 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %59)
store i32 -4, i32* %4
br label %63
61: ; preds = %44
%62 = load i32, i32* %10
store i32 %62, i32* %4
br label %63
63: ; preds = %61, %57, %38, %26, %16
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Error opening file: %s: %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 %14
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %12)
br label %39
14: ; preds = %3
%15 = call i64 @fstat(i32 %7, %struct.stat* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = tail call i32 @close(i32 %7)
br label %39
23: ; preds = %14
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%25 = load i64, i64* %24
%26 = icmp ugt i64 %25, %2
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%30 = tail call i32 @close(i32 %7)
br label %39
31: ; preds = %23
%32 = tail call i32 @read(i32 %7, i8* %0, i64 %25)
%33 = tail call i32 @close(i32 %7)
%34 = trunc i64 %25 to i32
%35 = icmp eq i32 %32, %34
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %39
39: ; preds = %31, %36, %27, %17, %9
%40 = phi i32 [ -1, %9 ], [ -2, %17 ], [ -3, %27 ], [ -4, %36 ], [ %32, %31 ]
ret i32 %40
}