#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int read_file(char *fname, char *buf, int size) {
  int num, fp;
  fp = open(fname, O_RDONLY);
  if (!fp)
    return -1;
  num = read(fp, buf, size);
  if (num < 0) {
    close(fp);
    return -1;
  }
  buf[num] = '\0';
  close(fp);
  return num;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    116e <read_file+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <read_file+0x88>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    119e <read_file+0x6e>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <read_file+0x88>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
je     1168 <read_file+0x38>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
test   %eax,%eax
js     116f <read_file+0x3f>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
jmp    1174 <read_file+0x44>
mov    $0xffffffff,%ebx
jmp    117b <read_file+0x4b>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @read_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %3
store i32 -1, i32* %4
br label %34
16: ; preds = %3
%17 = load i32, i32* %9
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = call i32 @read(i32 %17, i8* %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* %9
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %4
br label %34
26: ; preds = %16
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* %9
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %34
34: ; preds = %26, %23, %15
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @read_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %3
%8 = tail call i32 @read(i32 %5, i8* %1, i32 %2)
%9 = icmp slt i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %7, %10
%14 = phi i32 [ %8, %10 ], [ -1, %7 ]
%15 = tail call i32 @close(i32 %5)
br label %16
16: ; preds = %13, %3
%17 = phi i32 [ -1, %3 ], [ %14, %13 ]
ret i32 %17
}