#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 EINVAL;
scalar_t__ errno;
int /*<<< orphan*/ isspace(char);
int read_str_safe(int, char *, int);
long strtoul(char *, char **, int);
int read_str_ulong(int fd, unsigned long *value, int base) {
  char buf[32], *end;
  long v;
  int err;
  *value = 0;
  err = read_str_safe(fd, buf, sizeof(buf));
  if (err < 0)
    return err;
  errno = 0;
  v = strtoul(buf, &end, base);
  if (end == buf || !isspace(*end))
    return -EINVAL;
  *value = v;
  return 0;
}


x86 assembly (clang, -O0):
<read_str_ulong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1040 <read_str_safe@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1176 <read_str_ulong+0x46>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e3 <read_str_ulong+0xb3>
mov    0x2e63(%rip),%rax
movq   $0x0,(%rax)
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x48(%rbp),%rsi
call   1050 <strtoul@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
lea    -0x40(%rbp),%rcx
cmp    %rcx,%rax
je     11be <read_str_ulong+0x8e>
mov    -0x48(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%eax
jne    11d1 <read_str_ulong+0xa1>
xor    %eax,%eax
mov    0x2e11(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e3 <read_str_ulong+0xb3>
mov    -0x50(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_str_ulong>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
movq   $0x0,(%rsi)
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x20,%edx
call   1040 <read_str_safe@plt>
test   %eax,%eax
js     11a2 <read_str_ulong+0x72>
mov    0x2e7f(%rip),%rax
movq   $0x0,(%rax)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <strtoul@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%rax
cmp    %rbx,%rax
je     1197 <read_str_ulong+0x67>
movsbl (%rax),%edi
call   1030 <isspace@plt>
test   %eax,%eax
je     1197 <read_str_ulong+0x67>
mov    %rbp,(%r14)
xor    %eax,%eax
jmp    11a2 <read_str_ulong+0x72>
mov    0x2e3a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i32 0
define i32 @read_str_ulong(i32 %0, i64* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %5
store i64* %1, i64** %6
store i32 %2, i32* %7
%12 = load i64*, i64** %6
store i64 0, i64* %12
%13 = load i32, i32* %5
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%15 = call i32 @read_str_safe(i32 %13, i8* %14, i32 32)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i32, i32* %11
store i32 %19, i32* %4
br label %38
20: ; preds = %3
store i64 0, i64* @errno
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%22 = load i32, i32* %7
%23 = call i64 @strtoul(i8* %21, i8** %9, i32 %22)
store i64 %23, i64* %10
%24 = load i8*, i8** %9
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%26 = icmp eq i8* %24, %25
br i1 %26, label %32, label %27
27: ; preds = %20
%28 = load i8*, i8** %9
%29 = load i8, i8* %28
%30 = call i32 @isspace(i8 signext %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %27, %20
%33 = load i32, i32* @EINVAL
%34 = sub nsw i32 0, %33
store i32 %34, i32* %4
br label %38
35: ; preds = %27
%36 = load i64, i64* %10
%37 = load i64*, i64** %6
store i64 %36, i64* %37
store i32 0, i32* %4
br label %38
38: ; preds = %35, %32, %18
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i32 0
define i32 @read_str_ulong(i32 %0, i64* nocapture writeonly %1, i32 %2) {
%4 = alloca [32 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
store i64 0, i64* %1
%8 = call i32 @read_str_safe(i32 %0, i8* %6, i32 32)
%9 = icmp slt i32 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %3
store i64 0, i64* @errno
%11 = call i64 @strtoul(i8* %6, i8** %5, i32 %2)
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, %6
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i8, i8* %12
%16 = call i32 @isspace(i8 signext %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14, %10
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
br label %22
21: ; preds = %14
store i64 %11, i64* %1
br label %22
22: ; preds = %3, %21, %18
%23 = phi i32 [ %20, %18 ], [ 0, %21 ], [ %8, %3 ]
ret i32 %23
}