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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ isspace(char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int read(int, char *, int);
char *strerror(int);
int /*<<< orphan*/ strlen(char const *);
char *strstr(char *, char const *);
long strtol(char *, char **, int /*<<< orphan*/);
long file_read_ulong(char *file, const char *tag) {
  int fd;
  char buf[2048];
  int len, readerr;
  char *p, *q;
  long val;
  fd = open(file, O_RDONLY);
  if (fd < 0) {
    printf("Couldn't open %s: %s\n", file, strerror(errno));
    return -1;
  }
  len = read(fd, buf, sizeof(buf));
  readerr = errno;
  close(fd);
  if (len < 0) {
    printf("Error reading %s: %s\n", file, strerror(readerr));
    return -1;
  }
  if (len == sizeof(buf)) {
    printf("%s is too large\n", file);
    return -1;
  }
  buf[len] = '\0';
  if (tag) {
    p = strstr(buf, tag);
    if (!p)
      return -1;
    p += strlen(tag);
  } else
    p = buf;
  val = strtol(p, &q, 0);
  if (!isspace(*q)) {
    printf("Couldn't parse %s value\n", file);
    return -1;
  }
  return val;
}


x86 assembly (clang, -O0):
<file_read_ulong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1200 <file_read_ulong+0x70>
mov    -0x10(%rbp),%rax
mov    %rax,-0x848(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x848(%rbp),%rsi
mov    %rax,%rdx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1387 <file_read_ulong+0x1f7>
mov    -0x1c(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
call   1070 <read@plt>
mov    %eax,-0x824(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x828(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x824(%rbp)
jge    1279 <file_read_ulong+0xe9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x850(%rbp)
mov    -0x828(%rbp),%edi
call   10a0 <strerror@plt>
mov    -0x850(%rbp),%rsi
mov    %rax,%rdx
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1387 <file_read_ulong+0x1f7>
movslq -0x824(%rbp),%rax
cmp    $0x800,%rax
jne    12ab <file_read_ulong+0x11b>
mov    -0x10(%rbp),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1387 <file_read_ulong+0x1f7>
movslq -0x824(%rbp),%rax
movb   $0x0,-0x820(%rbp,%rax,1)
cmpq   $0x0,-0x18(%rbp)
je     131b <file_read_ulong+0x18b>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x830(%rbp)
cmpq   $0x0,-0x830(%rbp)
jne    12f7 <file_read_ulong+0x167>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1387 <file_read_ulong+0x1f7>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x830(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x830(%rbp)
jmp    1329 <file_read_ulong+0x199>
lea    -0x820(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rdi
lea    -0x838(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %rax,-0x840(%rbp)
mov    -0x838(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
cmp    $0x0,%eax
jne    137c <file_read_ulong+0x1ec>
mov    -0x10(%rbp),%rsi
lea    0xcd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1387 <file_read_ulong+0x1f7>
mov    -0x840(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read_ulong>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     11fa <file_read_ulong+0x6a>
mov    %eax,%ebp
lea    0x10(%rsp),%r12
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x800,%edx
call   1070 <read@plt>
mov    %eax,%ebx
mov    0x2e00(%rip),%rax
mov    (%rax),%r13d
mov    %ebp,%edi
call   1050 <close@plt>
test   %ebx,%ebx
js     1211 <file_read_ulong+0x81>
cmp    $0x800,%ebx
jne    122f <file_read_ulong+0x9f>
lea    0xe37(%rip),%rdi
jmp    1287 <file_read_ulong+0xf7>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdf1(%rip),%rdi
jmp    1220 <file_read_ulong+0x90>
mov    %r13d,%edi
call   10a0 <strerror@plt>
lea    0xdf6(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1291 <file_read_ulong+0x101>
mov    %ebx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
test   %r14,%r14
je     125d <file_read_ulong+0xcd>
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
je     1291 <file_read_ulong+0x101>
mov    %rax,%r12
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%r12
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
test   %eax,%eax
jne    1298 <file_read_ulong+0x108>
lea    0xdb6(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [22 x i8] c"Couldn't open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"Error reading %s: %s\0A\00"
@.str.2 = [17 x i8] c"%s is too large\0A\00"
@.str.3 = [25 x i8] c"Couldn't parse %s value\0A\00"
define i64 @file_read_ulong(i8* %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [2048 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %21)
store i64 -1, i64* %3
br label %77
23: ; preds = %2
%24 = load i32, i32* %6
%25 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 2048)
store i32 %26, i32* %8
%27 = load i32, i32* @errno
store i32 %27, i32* %9
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %23
%33 = load i8*, i8** %4
%34 = load i32, i32* %9
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* %35)
store i64 -1, i64* %3
br label %77
37: ; preds = %23
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = icmp eq i64 %39, 2048
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i8*, i8** %4
%43 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %42)
store i64 -1, i64* %3
br label %77
44: ; preds = %37
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %5
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %63
50: ; preds = %44
%51 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%52 = load i8*, i8** %5
%53 = call i8* @strstr(i8* %51, i8* %52)
store i8* %53, i8** %10
%54 = load i8*, i8** %10
%55 = icmp ne i8* %54, null
br i1 %55, label %57, label %56
56: ; preds = %50
store i64 -1, i64* %3
br label %77
57: ; preds = %50
%58 = load i8*, i8** %5
%59 = call i32 @strlen(i8* %58)
%60 = load i8*, i8** %10
%61 = sext i32 %59 to i64
%62 = getelementptr inbounds i8, i8* %60, i64 %61
store i8* %62, i8** %10
br label %65
63: ; preds = %44
%64 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
store i8* %64, i8** %10
br label %65
65: ; preds = %63, %57
%66 = load i8*, i8** %10
%67 = call i64 @strtol(i8* %66, i8** %11, i32 0)
store i64 %67, i64* %12
%68 = load i8*, i8** %11
%69 = load i8, i8* %68
%70 = call i32 @isspace(i8 signext %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %75, label %72
72: ; preds = %65
%73 = load i8*, i8** %4
%74 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %73)
store i64 -1, i64* %3
br label %77
75: ; preds = %65
%76 = load i64, i64* %12
store i64 %76, i64* %3
br label %77
77: ; preds = %75, %72, %56, %41, %32, %18
%78 = load i64, i64* %3
ret i64 %78
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [22 x i8] c"Couldn't open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"Error reading %s: %s\0A\00"
@.str.2 = [17 x i8] c"%s is too large\0A\00"
@.str.3 = [25 x i8] c"Couldn't parse %s value\0A\00"
define i64 @file_read_ulong(i8* %0, i8* %1) {
%3 = alloca [2048 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%6 = bitcast i8** %4 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %12)
br label %46
14: ; preds = %2
%15 = call i32 @read(i32 %8, i8* %5, i32 2048)
%16 = load i32, i32* @errno
%17 = tail call i32 @close(i32 %8)
%18 = icmp slt i32 %15, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i8* @strerror(i32 %16)
%21 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %20)
br label %46
22: ; preds = %14
%23 = icmp eq i32 %15, 2048
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %46
26: ; preds = %22
%27 = zext i32 %15 to i64
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %27
store i8 0, i8* %28
%29 = icmp eq i8* %1, null
br i1 %29, label %37, label %30
30: ; preds = %26
%31 = call i8* @strstr(i8* %5, i8* %1)
%32 = icmp eq i8* %31, null
br i1 %32, label %46, label %33
33: ; preds = %30
%34 = call i32 @strlen(i8* %1)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %31, i64 %35
br label %37
37: ; preds = %26, %33
%38 = phi i8* [ %36, %33 ], [ %5, %26 ]
%39 = call i64 @strtol(i8* %38, i8** %4, i32 0)
%40 = load i8*, i8** %4
%41 = load i8, i8* %40
%42 = call i32 @isspace(i8 signext %41)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %37
%45 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %46
46: ; preds = %37, %30, %44, %24, %19, %10
%47 = phi i64 [ -1, %10 ], [ -1, %19 ], [ -1, %24 ], [ -1, %44 ], [ -1, %30 ], [ %39, %37 ]
ret i64 %47
}