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

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
unsigned long strtoul(char *, char **, int /*<<< orphan*/);
int util_read(char const *, char *, int);
ssize_t util_strtoul(const char *name, unsigned long int *val) {
  ssize_t ret;
  char data[256], *endptr;
  unsigned long int tmp;
  memset(data, 0, sizeof(data));
  if ((ret = util_read(name, data, sizeof(data))) < 0) {
    return ret;
  }
  tmp = strtoul(data, &endptr, 0);
  if (*endptr != '\n') {
    return -1;
  }
  *val = tmp;
  return ret;
}


x86 assembly (clang, -O0):
<util_strtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1040 <util_read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1182 <util_strtoul+0x52>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <util_strtoul+0xa1>
lea    -0x120(%rbp),%rdi
lea    -0x128(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x130(%rbp)
mov    -0x128(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     11bd <util_strtoul+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <util_strtoul+0xa1>
mov    -0x130(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_strtoul>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1040 <util_read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1191 <util_strtoul+0x61>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,%rcx
mov    0x8(%rsp),%rdx
mov    $0xffffffff,%eax
cmpb   $0xa,(%rdx)
jne    1193 <util_strtoul+0x63>
mov    %rcx,(%r14)
mov    %ebp,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @util_strtoul(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 0, i32 256)
%12 = load i8*, i8** %4
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%14 = call i32 @util_read(i8* %12, i8* %13, i32 256)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %6
store i32 %17, i32* %3
br label %30
18: ; preds = %2
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%20 = call i64 @strtoul(i8* %19, i8** %8, i32 0)
store i64 %20, i64* %9
%21 = load i8*, i8** %8
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 10
br i1 %24, label %25, label %26
25: ; preds = %18
store i32 -1, i32* %3
br label %30
26: ; preds = %18
%27 = load i64, i64* %9
%28 = load i64*, i64** %5
store i64 %27, i64* %28
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %30
30: ; preds = %26, %25, %16
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
define i32 @util_strtoul(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast i8** %4 to i8*
%7 = call i32 @memset(i8* %5, i32 0, i32 256)
%8 = call i32 @util_read(i8* %0, i8* %5, i32 256)
%9 = icmp slt i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = call i64 @strtoul(i8* %5, i8** %4, i32 0)
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 10
br i1 %14, label %15, label %16
15: ; preds = %10
store i64 %11, i64* %1
br label %16
16: ; preds = %10, %2, %15
%17 = phi i32 [ %8, %15 ], [ %8, %2 ], [ -1, %10 ]
ret i32 %17
}