#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
void read_unsigned_int(const char *filename, unsigned *val) {
  int fd;
  char buf[16];
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    return;
  if (read(fd, buf, 15) > 0 && *buf) {
    unsigned long tmp;
    errno = 0;
    tmp = strtoul(buf, NULL, 10);
    if (!errno && tmp <= (unsigned)-1)
      *val = tmp;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<read_unsigned_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1174 <read_unsigned_int+0x34>
jmp    11f9 <read_unsigned_int+0xb9>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xf,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jle    11f1 <read_unsigned_int+0xb1>
movsbl -0x30(%rbp),%eax
cmp    $0x0,%eax
je     11f1 <read_unsigned_int+0xb1>
mov    0x2e35(%rip),%rax
movq   $0x0,(%rax)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %rax,-0x38(%rbp)
mov    0x2e11(%rip),%rax
cmpq   $0x0,(%rax)
jne    11ec <read_unsigned_int+0xac>
mov    $0xffffffff,%eax
cmp    %rax,-0x38(%rbp)
ja     11ec <read_unsigned_int+0xac>
mov    -0x38(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11f1 <read_unsigned_int+0xb1>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_unsigned_int>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11b6 <read_unsigned_int+0x76>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xf,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11a7 <read_unsigned_int+0x67>
cmpb   $0x0,(%rsp)
je     11a7 <read_unsigned_int+0x67>
mov    0x2e58(%rip),%r14
movq   $0x0,(%r14)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtoul@plt>
cmpq   $0x0,(%r14)
jne    11a7 <read_unsigned_int+0x67>
mov    %rax,%rcx
shr    $0x20,%rcx
jne    11a7 <read_unsigned_int+0x67>
mov    %eax,(%rbx)
mov    %ebp,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
define void @read_unsigned_int(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [16 x i8]
%7 = alloca i64
store i8* %0, i8** %3
store i32* %1, i32** %4
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
br label %40
14: ; preds = %2
%15 = load i32, i32* %5
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%17 = call i64 @read(i32 %15, i8* %16, i32 15)
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %14
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %37
24: ; preds = %19
store i64 0, i64* @errno
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%26 = call i64 @strtoul(i8* %25, i32* null, i32 10)
store i64 %26, i64* %7
%27 = load i64, i64* @errno
%28 = icmp ne i64 %27, 0
br i1 %28, label %36, label %29
29: ; preds = %24
%30 = load i64, i64* %7
%31 = icmp ule i64 %30, 4294967295
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i64, i64* %7
%34 = trunc i64 %33 to i32
%35 = load i32*, i32** %4
store i32 %34, i32* %35
br label %36
36: ; preds = %32, %29, %24
br label %37
37: ; preds = %36, %19, %14
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %37, %13
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
define void @read_unsigned_int(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %2
%9 = call i64 @read(i32 %6, i8* %4, i32 15)
%10 = icmp sgt i64 %9, 0
%11 = load i8, i8* %4
%12 = icmp ne i8 %11, 0
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %14, label %22
14: ; preds = %8
store i64 0, i64* @errno
%15 = call i64 @strtoul(i8* nocapture %4, i32* null, i32 10)
%16 = load i64, i64* @errno
%17 = icmp eq i64 %16, 0
%18 = icmp ult i64 %15, 4294967296
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = trunc i64 %15 to i32
store i32 %21, i32* %1
br label %22
22: ; preds = %14, %20, %8
%23 = tail call i32 @close(i32 %6)
br label %24
24: ; preds = %2, %22
ret void
}