#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 */
scalar_t__ errno;
int pread(int, char *, int, int /*<<< orphan*/);
long long strtoll(char *, int /*<<< orphan*/ *, int);
int get_attr(int fd, long long *val, int base) {
  char data[24];
  ssize_t r = pread(fd, data, sizeof(data), 0);
  if (r < 1) {
    *val = -1;
    return -1;
  }
  errno = 0;
  *val = strtoll(data, NULL, base);
  if (errno) {
    *val = -2;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
xor    %ecx,%ecx
call   1040 <pread@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jge    1169 <get_attr+0x49>
mov    -0x10(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <get_attr+0xa0>
mov    0x2e70(%rip),%rax
movq   $0x0,(%rax)
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <strtoll@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e48(%rip),%rax
cmpq   $0x0,(%rax)
je     11b9 <get_attr+0x99>
mov    -0x10(%rbp),%rax
movq   $0xfffffffffffffffe,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <get_attr+0xa0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_attr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rsp,%rsi
mov    $0x18,%edx
xor    %ecx,%ecx
call   1040 <pread@plt>
test   %eax,%eax
jle    1178 <get_attr+0x58>
mov    0x2e99(%rip),%r14
movq   $0x0,(%r14)
mov    %rsp,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <strtoll@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmpq   $0x0,(%r14)
setne  %al
mov    $0xfffffffffffffffe,%rdx
cmove  %rcx,%rdx
mov    %rdx,(%rbx)
neg    %eax
jmp    1184 <get_attr+0x64>
movq   $0xffffffffffffffff,(%rbx)
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
define i32 @get_attr(i32 %0, i64* nocapture writeonly %1, i32 %2) {
%4 = alloca [24 x i8]
%5 = getelementptr inbounds [24 x i8], [24 x i8]* %4, i64 0, i64 0
%6 = call i32 @pread(i32 %0, i8* %5, i32 24, i32 0)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %9
8: ; preds = %3
store i64 -1, i64* %1
br label %16
9: ; preds = %3
store i64 0, i64* @errno
%10 = call i64 @strtoll(i8* nocapture %5, i32* null, i32 %2)
%11 = load i64, i64* @errno
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i64 %10, i64 -2
store i64 %13, i64* %1
%14 = xor i1 %12, true
%15 = sext i1 %14 to i32
br label %16
16: ; preds = %9, %8
%17 = phi i32 [ -1, %8 ], [ %15, %9 ]
ret i32 %17
}