#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int get_attr(int, long long *, int);
int /*<<< orphan*/ printf(char const *, long long);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void read_val(const char *prompt, int fd) {
  long long v;
  int ret = get_attr(fd, &v, 10);
  if (ret < 0) {
    fprintf(stderr, "failed to get attr: %s\n", strerror(errno));
    return;
  }
  printf(prompt, v);
}


x86 assembly (clang, -O0):
<read_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1050 <get_attr@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119e <read_val+0x5e>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe6c(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11ab <read_val+0x6b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_val>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%eax
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1050 <get_attr@plt>
test   %eax,%eax
js     116e <read_val+0x2e>
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <printf@plt>
jmp    1196 <read_val+0x56>
mov    0x2e6b(%rip),%rax
mov    (%rax),%ebx
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe74(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"failed to get attr: %s\0A\00"
@errno = global i32 0
define void @read_val(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = call i32 @get_attr(i32 %7, i64* %5, i32 10)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %20
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = load i64, i64* %5
%19 = call i32 @printf(i8* %17, i64 %18)
br label %20
20: ; preds = %16, %11
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"failed to get attr: %s\0A\00"
@errno = global i32 0
define void @read_val(i8* %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = call i32 @get_attr(i32 %1, i64* %3, i32 10)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %15
12: ; preds = %2
%13 = load i64, i64* %3
%14 = call i32 @printf(i8* %0, i64 %13)
br label %15
15: ; preds = %12, %7
ret void
}