#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;
scalar_t__ close(int);
scalar_t__ errno;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
long strtol(char *, int /*<<< orphan*/ *, int);
int get_long_from_file(long *value, const char *filename) {
  int fd = open(filename, O_RDONLY);
  if (fd < 0)
    return -1;
  char buf[16];
  if (read(fd, buf, 16) < 1) {
    close(fd);
    return -1;
  }
  if (close(fd) < 0)
    return -1;
  errno = 0;
  *value = strtol(buf, NULL, 10);
  if (errno)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<get_long_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117b <get_long_from_file+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <get_long_from_file+0xd5>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jge    11a9 <get_long_from_file+0x69>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <get_long_from_file+0xd5>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    11c7 <get_long_from_file+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <get_long_from_file+0xd5>
mov    0x2e0a(%rip),%rax
movq   $0x0,(%rax)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2de0(%rip),%rax
cmpq   $0x0,(%rax)
je     120e <get_long_from_file+0xce>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <get_long_from_file+0xd5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_long_from_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e93(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11b7 <get_long_from_file+0x77>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %ebx,%ebx
jle    11bc <get_long_from_file+0x7c>
test   %rcx,%rcx
js     11bc <get_long_from_file+0x7c>
mov    0x2e43(%rip),%rbx
movq   $0x0,(%rbx)
xor    %ebp,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,(%r14)
cmp    (%rbx),%rbp
sbb    %eax,%eax
jmp    11bc <get_long_from_file+0x7c>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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