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

/* Variables and functions */
int EBADMSG;
int ENODATA;
int ERANGE;
int read_from_fd(int, char *, int);
unsigned long strtoul(char *, char **, int);
int read_unsigned_int_from_fd(int fd) {
  char buffer[16];
  char *endptr;
  unsigned long value;
  int rv;
  rv = read_from_fd(fd, buffer, sizeof(buffer) - 1);
  if (rv < 0)
    return rv;
  if (rv == 0)
    return -ENODATA;
  buffer[rv] = '\0';
  value = strtoul(buffer, &endptr, 10);
  if (endptr == buffer)
    return -EBADMSG;
  if (value == (~0UL) || value > ((int)(~0U >> 1)))
    return -ERANGE;
  return (int)value;
}


x86 assembly (clang, -O0):
<read_unsigned_int_from_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xf,%edx
call   1030 <read_from_fd@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1154 <read_unsigned_int_from_fd+0x34>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <read_unsigned_int_from_fd+0xc7>
cmpl   $0x0,-0x34(%rbp)
jne    1171 <read_unsigned_int_from_fd+0x51>
xor    %eax,%eax
mov    0x2e71(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <read_unsigned_int_from_fd+0xc7>
movslq -0x34(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
lea    -0x20(%rbp),%rcx
cmp    %rcx,%rax
jne    11b4 <read_unsigned_int_from_fd+0x94>
xor    %eax,%eax
mov    0x2e3e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <read_unsigned_int_from_fd+0xc7>
cmpq   $0xffffffffffffffff,-0x30(%rbp)
je     11cd <read_unsigned_int_from_fd+0xad>
cmpq   $0x7fffffff,-0x30(%rbp)
7f
jbe    11e0 <read_unsigned_int_from_fd+0xc0>
xor    %eax,%eax
mov    0x2e0a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <read_unsigned_int_from_fd+0xc7>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_unsigned_int_from_fd>:
push   %rbx
sub    $0x20,%rsp
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xf,%edx
call   1030 <read_from_fd@plt>
test   %eax,%eax
js     1182 <read_unsigned_int_from_fd+0x62>
je     116e <read_unsigned_int_from_fd+0x4e>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1040 <strtoul@plt>
cmp    %rbx,0x8(%rsp)
je     1177 <read_unsigned_int_from_fd+0x57>
test   $0xffffffff80000000,%rax
je     1182 <read_unsigned_int_from_fd+0x62>
mov    0x2e74(%rip),%rcx
jmp    117e <read_unsigned_int_from_fd+0x5e>
mov    0x2e63(%rip),%rcx
jmp    117e <read_unsigned_int_from_fd+0x5e>
mov    0x2e6a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@ENODATA = global i32 0
@EBADMSG = global i32 0
@ERANGE = global i32 0
define i32 @read_unsigned_int_from_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%10 = call i32 @read_from_fd(i32 %8, i8* %9, i32 15)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* %7
store i32 %14, i32* %2
br label %45
15: ; preds = %1
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @ENODATA
%20 = sub nsw i32 0, %19
store i32 %20, i32* %2
br label %45
21: ; preds = %15
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%26 = call i64 @strtoul(i8* %25, i8** %5, i32 10)
store i64 %26, i64* %6
%27 = load i8*, i8** %5
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i32, i32* @EBADMSG
%32 = sub nsw i32 0, %31
store i32 %32, i32* %2
br label %45
33: ; preds = %21
%34 = load i64, i64* %6
%35 = icmp eq i64 %34, -1
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i64, i64* %6
%38 = icmp ugt i64 %37, 2147483647
br i1 %38, label %39, label %42
39: ; preds = %36, %33
%40 = load i32, i32* @ERANGE
%41 = sub nsw i32 0, %40
store i32 %41, i32* %2
br label %45
42: ; preds = %36
%43 = load i64, i64* %6
%44 = trunc i64 %43 to i32
store i32 %44, i32* %2
br label %45
45: ; preds = %42, %39, %30, %18, %13
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@ENODATA = global i32 0
@EBADMSG = global i32 0
@ERANGE = global i32 0
define i32 @read_unsigned_int_from_fd(i32 %0) {
%2 = alloca [16 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
%6 = call i32 @read_from_fd(i32 %0, i8* %4, i32 15)
%7 = icmp slt i32 %6, 0
br i1 %7, label %29, label %8
8: ; preds = %1
%9 = icmp eq i32 %6, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = load i32, i32* @ENODATA
%12 = sub nsw i32 0, %11
br label %29
13: ; preds = %8
%14 = zext i32 %6 to i64
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i64 @strtoul(i8* %4, i8** %3, i32 10)
%17 = load i8*, i8** %3
%18 = icmp eq i8* %17, %4
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @EBADMSG
%21 = sub nsw i32 0, %20
br label %29
22: ; preds = %13
%23 = icmp ugt i64 %16, 2147483647
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = load i32, i32* @ERANGE
%26 = sub nsw i32 0, %25
br label %29
27: ; preds = %22
%28 = trunc i64 %16 to i32
br label %29
29: ; preds = %1, %27, %24, %19, %10
%30 = phi i32 [ %12, %10 ], [ %21, %19 ], [ %26, %24 ], [ %28, %27 ], [ %6, %1 ]
ret i32 %30
}