#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 fileaccess {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int ETXTBSY;
scalar_t__ errno;
int file_read_buf(struct fileaccess *, char *, int);
long strtol(char *, char **, int);
int file_read_int(struct fileaccess *fa, int *value, int base) {
  char buf[64];
  int count;
  long val;
  char *tail;
  count = file_read_buf(fa, buf, sizeof(buf) - 1);
  if (count < 0)
    return count;
  buf[count] = '\0';
  errno = 0;
  val = strtol(buf, &tail, base);
  if (errno || (*tail != '\0' && *tail != '\n'))
    return -ETXTBSY;
  *value = val;
  return 0;
}


x86 assembly (clang, -O0):
<file_read_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x3f,%edx
call   1030 <file_read_buf@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    1160 <file_read_int+0x40>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e2 <file_read_int+0xc2>
movslq -0x64(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
mov    0x2e68(%rip),%rax
movq   $0x0,(%rax)
lea    -0x60(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    -0x78(%rbp),%rsi
call   1040 <strtol@plt>
mov    %rax,-0x70(%rbp)
mov    0x2e46(%rip),%rax
cmpq   $0x0,(%rax)
jne    11bc <file_read_int+0x9c>
mov    -0x78(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11cf <file_read_int+0xaf>
mov    -0x78(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     11cf <file_read_int+0xaf>
xor    %eax,%eax
mov    0x2e1b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e2 <file_read_int+0xc2>
mov    -0x70(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read_int>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
lea    0x10(%rsp),%rsi
mov    $0x3f,%edx
call   1030 <file_read_buf@plt>
test   %eax,%eax
js     118e <file_read_int+0x6e>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x2e8a(%rip),%rbx
movq   $0x0,(%rbx)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %ebp,%edx
call   1040 <strtol@plt>
cmpq   $0x0,(%rbx)
je     1179 <file_read_int+0x59>
mov    0x2e6d(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    118e <file_read_int+0x6e>
mov    0x8(%rsp),%rcx
mov    (%rcx),%cl
test   %cl,%cl
je     1189 <file_read_int+0x69>
cmp    $0xa,%cl
jne    116c <file_read_int+0x4c>
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.fileaccess = type { i32 }
@errno = global i64 0
@ETXTBSY = global i32 0
define i32 @file_read_int(%struct.fileaccess* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.fileaccess*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [64 x i8]
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
store %struct.fileaccess* %0, %struct.fileaccess** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%12 = load %struct.fileaccess*, %struct.fileaccess** %5
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%14 = call i32 @file_read_buf(%struct.fileaccess* %12, i8* %13, i32 63)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* %9
store i32 %18, i32* %4
br label %45
19: ; preds = %3
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 %21
store i8 0, i8* %22
store i64 0, i64* @errno
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%24 = load i32, i32* %7
%25 = call i64 @strtol(i8* %23, i8** %11, i32 %24)
store i64 %25, i64* %10
%26 = load i64, i64* @errno
%27 = icmp ne i64 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %19
%29 = load i8*, i8** %11
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %28
%34 = load i8*, i8** %11
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 10
br i1 %37, label %38, label %41
38: ; preds = %33, %19
%39 = load i32, i32* @ETXTBSY
%40 = sub nsw i32 0, %39
store i32 %40, i32* %4
br label %45
41: ; preds = %33, %28
%42 = load i64, i64* %10
%43 = trunc i64 %42 to i32
%44 = load i32*, i32** %6
store i32 %43, i32* %44
store i32 0, i32* %4
br label %45
45: ; preds = %41, %38, %17
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.fileaccess = type { i32 }
@errno = global i64 0
@ETXTBSY = global i32 0
define i32 @file_read_int(%struct.fileaccess* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca [64 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
%8 = call i32 @file_read_buf(%struct.fileaccess* %0, i8* %6, i32 63)
%9 = icmp slt i32 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %3
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %11
store i8 0, i8* %12
store i64 0, i64* @errno
%13 = call i64 @strtol(i8* %6, i8** %5, i32 %2)
%14 = load i64, i64* @errno
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
switch i8 %18, label %19 [
i8 0, label %22
i8 10, label %22
]
19: ; preds = %16, %10
%20 = load i32, i32* @ETXTBSY
%21 = sub nsw i32 0, %20
br label %24
22: ; preds = %16, %16
%23 = trunc i64 %13 to i32
store i32 %23, i32* %1
br label %24
24: ; preds = %3, %22, %19
%25 = phi i32 [ %21, %19 ], [ 0, %22 ], [ %8, %3 ]
ret i32 %25
}