#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 atoi(char *);
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int read_proc(char *filename) {
  char buf[100];
  int fd, rc;
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    return -1;
  rc = read(fd, buf, 99);
  if (rc < 0) {
    int saved_errno = errno;
    close(fd);
    errno = saved_errno;
    return -1;
  }
  close(fd);
  if (rc == 0)
    return -1;
  buf[rc] = '\0';
  return atoi(buf);
}


x86 assembly (clang, -O0):
<read_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    1180 <read_proc+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <read_proc+0xd8>
mov    -0x84(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x63,%edx
call   1040 <read@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0x0,-0x88(%rbp)
jge    11dc <read_proc+0x9c>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x84(%rbp),%edi
call   1030 <close@plt>
mov    -0x8c(%rbp),%ecx
mov    0x2e0a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <read_proc+0xd8>
mov    -0x84(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x88(%rbp)
jne    1200 <read_proc+0xc0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <read_proc+0xd8>
movslq -0x88(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11ab <read_proc+0x6b>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x63,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1197 <read_proc+0x57>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
test   %ebx,%ebx
je     11ab <read_proc+0x6b>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1060 <atoi@plt>
mov    %eax,%r14d
jmp    11ab <read_proc+0x6b>
mov    0x2e3a(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,(%r15)
mov    %r14d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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