#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 */
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ isdigit(char);
int open(char const *, int /*<<< orphan*/);
size_t read(int, char *, int);
long int readfile(const char *path) {
  int fd;
  char buf[64];
  ssize_t i, n;
  long int value = 0;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return -1;
  while ((n = read(fd, buf, sizeof(buf))) > 0) {
    for (i = 0; i < n; i++) {
      if (isdigit(buf[i]))
        value = value * 10 + (buf[i] - '0');
      else if (buf[i] != '\n')
        goto out;
    }
  }
out:
  close(fd);
  return n ? -1 : value;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x78(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1183 <readfile+0x43>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1264 <readfile+0x124>
jmp    1188 <readfile+0x48>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %rax,-0x70(%rbp)
cmp    $0x0,%rax
jbe    122c <readfile+0xec>
movq   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmp    -0x70(%rbp),%rax
jae    1227 <readfile+0xe7>
mov    -0x68(%rbp),%rax
movsbl -0x60(%rbp,%rax,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     11f5 <readfile+0xb5>
imul   $0xa,-0x78(%rbp),%rax
mov    -0x68(%rbp),%rcx
movsbl -0x60(%rbp,%rcx,1),%ecx
sub    $0x30,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x78(%rbp)
jmp    1211 <readfile+0xd1>
mov    -0x68(%rbp),%rax
movsbl -0x60(%rbp,%rax,1),%eax
cmp    $0xa,%eax
je     120c <readfile+0xcc>
jmp    1231 <readfile+0xf1>
jmp    1211 <readfile+0xd1>
jmp    1216 <readfile+0xd6>
mov    -0x68(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x68(%rbp)
jmp    11af <readfile+0x6f>
jmp    1188 <readfile+0x48>
jmp    1231 <readfile+0xf1>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x70(%rbp)
je     1254 <readfile+0x114>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x80(%rbp)
jmp    125c <readfile+0x11c>
mov    -0x78(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11e6 <readfile+0xa6>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x40,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11ef <readfile+0xaf>
mov    %rax,%r13
xor    %r12d,%r12d
mov    %rsp,%r14
jmp    11a8 <readfile+0x68>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %rax,%r13
test   %rax,%rax
je     11f2 <readfile+0xb2>
xor    %ebp,%ebp
jmp    11c5 <readfile+0x85>
nopl   0x0(%rax)
lea    (%r12,%r12,4),%rax
lea    (%rbx,%rax,2),%r12
add    $0xffffffffffffffd0,%r12
add    $0x1,%rbp
cmp    %rbp,%r13
je     1190 <readfile+0x50>
movsbl (%rsp,%rbp,1),%eax
movslq %eax,%rbx
mov    %ebx,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    11b0 <readfile+0x70>
cmp    $0xa,%bl
je     11bc <readfile+0x7c>
mov    $0xffffffffffffffff,%r12
jmp    11f2 <readfile+0xb2>
mov    $0xffffffffffffffff,%r12
jmp    11fa <readfile+0xba>
xor    %r12d,%r12d
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r12,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i64 @readfile(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* %8
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i64 -1, i64* %2
br label %66
15: ; preds = %1
br label %16
16: ; preds = %54, %15
%17 = load i32, i32* %4
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%19 = call i64 @read(i32 %17, i8* %18, i32 64)
store i64 %19, i64* %7
%20 = icmp ugt i64 %19, 0
br i1 %20, label %21, label %55
21: ; preds = %16
store i64 0, i64* %6
br label %22
22: ; preds = %51, %21
%23 = load i64, i64* %6
%24 = load i64, i64* %7
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %54
26: ; preds = %22
%27 = load i64, i64* %6
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = call i64 @isdigit(i8 signext %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %26
%33 = load i64, i64* %8
%34 = mul nsw i64 %33, 10
%35 = load i64, i64* %6
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = sub nsw i32 %38, 48
%40 = sext i32 %39 to i64
%41 = add nsw i64 %34, %40
store i64 %41, i64* %8
br label %50
42: ; preds = %26
%43 = load i64, i64* %6
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 10
br i1 %47, label %48, label %49
48: ; preds = %42
br label %56
49: ; preds = %42
br label %50
50: ; preds = %49, %32
br label %51
51: ; preds = %50
%52 = load i64, i64* %6
%53 = add i64 %52, 1
store i64 %53, i64* %6
br label %22
54: ; preds = %22
br label %16
55: ; preds = %16
br label %56
56: ; preds = %55, %48
%57 = load i32, i32* %4
%58 = call i32 @close(i32 %57)
%59 = load i64, i64* %7
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %56
br label %64
62: ; preds = %56
%63 = load i64, i64* %8
br label %64
64: ; preds = %62, %61
%65 = phi i64 [ -1, %61 ], [ %63, %62 ]
store i64 %65, i64* %2
br label %66
66: ; preds = %64, %14
%67 = load i64, i64* %2
ret i64 %67
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i64 @readfile(i8* nocapture %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 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 %37, label %7
7: ; preds = %1
%8 = call i64 @read(i32 %5, i8* %3, i32 64)
%9 = icmp eq i64 %8, 0
br i1 %9, label %34, label %13
10: ; preds = %30
%11 = call i64 @read(i32 %5, i8* %3, i32 64)
%12 = icmp eq i64 %11, 0
br i1 %12, label %34, label %13
13: ; preds = %7, %10
%14 = phi i64 [ %11, %10 ], [ %8, %7 ]
%15 = phi i64 [ %31, %10 ], [ 0, %7 ]
br label %16
16: ; preds = %13, %30
%17 = phi i64 [ %15, %13 ], [ %31, %30 ]
%18 = phi i64 [ 0, %13 ], [ %32, %30 ]
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = tail call i64 @isdigit(i8 signext %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %16
%24 = mul nsw i64 %17, 10
%25 = sext i8 %20 to i64
%26 = add i64 %24, -48
%27 = add i64 %26, %25
br label %30
28: ; preds = %16
%29 = icmp eq i8 %20, 10
br i1 %29, label %30, label %34
30: ; preds = %23, %28
%31 = phi i64 [ %27, %23 ], [ %17, %28 ]
%32 = add nuw i64 %18, 1
%33 = icmp eq i64 %32, %14
br i1 %33, label %10, label %16
34: ; preds = %10, %28, %7
%35 = phi i64 [ 0, %7 ], [ -1, %28 ], [ %31, %10 ]
%36 = tail call i32 @close(i32 %5)
br label %37
37: ; preds = %1, %34
%38 = phi i64 [ %35, %34 ], [ -1, %1 ]
ret i64 %38
}