#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*/ uint64_t;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int _gen_err(int);
int /*<<< orphan*/ be64toh(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int openat(int, char const *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
int get_idx(int dirfd, const char *path, uint64_t *val) {
  int status = 0;
  int fd = openat(dirfd, path, O_RDONLY);
  if (fd < 0) {
    if (errno == ENOENT) {
      *val = 0;
      return 0;
    }
    return _gen_err(-2);
  }
  ssize_t rc;
  while ((rc = read(fd, val, sizeof(*val))) < 0)
    if (errno != EINTR)
      break;
  if (rc < 0)
    status = _gen_err(-2);
  else if (rc != sizeof(*val))
    status = _gen_err(-3);
  else
    *val = be64toh(*val);
  close(fd);
  return status;
}


x86 assembly (clang, -O0):
<get_idx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
call   1030 <openat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11ce <get_idx+0x7e>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11bc <get_idx+0x6c>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1276 <get_idx+0x126>
mov    $0xfffffffe,%edi
call   1070 <_gen_err@plt>
mov    %eax,-0x4(%rbp)
jmp    1276 <get_idx+0x126>
jmp    11d3 <get_idx+0x83>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1214 <get_idx+0xc4>
mov    0x2de1(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
je     120f <get_idx+0xbf>
jmp    1214 <get_idx+0xc4>
jmp    11d3 <get_idx+0x83>
cmpl   $0x0,-0x24(%rbp)
jge    1230 <get_idx+0xe0>
mov    $0xfffffffe,%edi
call   1070 <_gen_err@plt>
mov    %eax,-0x1c(%rbp)
jmp    1268 <get_idx+0x118>
movslq -0x24(%rbp),%rax
cmp    $0x4,%rax
je     1250 <get_idx+0x100>
mov    $0xfffffffd,%edi
call   1070 <_gen_err@plt>
mov    %eax,-0x1c(%rbp)
jmp    1263 <get_idx+0x113>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <be64toh@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1268 <get_idx+0x118>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_idx>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
call   1030 <openat@plt>
test   %eax,%eax
js     11a3 <get_idx+0x53>
mov    %eax,%ebx
mov    0x2e63(%rip),%r15
mov    0x2e54(%rip),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11c4 <get_idx+0x74>
mov    (%r15),%rax
cmp    0x0(%rbp),%rax
je     1180 <get_idx+0x30>
mov    $0xfffffffe,%edi
jmp    11f1 <get_idx+0xa1>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <get_idx+0x88>
movl   $0x0,(%r14)
xor    %ebp,%ebp
jmp    11ff <get_idx+0xaf>
cmp    $0x4,%eax
jne    11ec <get_idx+0x9c>
mov    (%r14),%edi
call   1060 <be64toh@plt>
mov    %eax,(%r14)
xor    %ebp,%ebp
jmp    11f8 <get_idx+0xa8>
mov    $0xfffffffe,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <_gen_err@plt>
mov    $0xfffffffd,%edi
call   1070 <_gen_err@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EINTR = global i64 0
define i32 @get_idx(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 0, i32* %8
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @openat(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %3
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32*, i32** %7
store i32 0, i32* %22
store i32 0, i32* %4
br label %58
23: ; preds = %17
%24 = call i32 @_gen_err(i32 -2)
store i32 %24, i32* %4
br label %58
25: ; preds = %3
br label %26
26: ; preds = %36, %25
%27 = load i32, i32* %9
%28 = load i32*, i32** %7
%29 = call i32 @read(i32 %27, i32* %28, i32 4)
store i32 %29, i32* %10
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
br label %37
36: ; preds = %31
br label %26
37: ; preds = %35, %26
%38 = load i32, i32* %10
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @_gen_err(i32 -2)
store i32 %41, i32* %8
br label %54
42: ; preds = %37
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = icmp ne i64 %44, 4
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @_gen_err(i32 -3)
store i32 %47, i32* %8
br label %53
48: ; preds = %42
%49 = load i32*, i32** %7
%50 = load i32, i32* %49
%51 = call i32 @be64toh(i32 %50)
%52 = load i32*, i32** %7
store i32 %51, i32* %52
br label %53
53: ; preds = %48, %46
br label %54
54: ; preds = %53, %40
%55 = load i32, i32* %9
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %58
58: ; preds = %54, %23, %21
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EINTR = global i64 0
define i32 @get_idx(i32 %0, i8* %1, i32* nocapture %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @openat(i32 %0, i8* %1, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %3
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 0, i32* %2
br label %33
12: ; preds = %7
%13 = tail call i32 @_gen_err(i32 -2)
br label %33
14: ; preds = %3, %17
%15 = tail call i32 @read(i32 %5, i32* %2, i32 4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %14, label %21
21: ; preds = %17
%22 = tail call i32 @_gen_err(i32 -2)
br label %30
23: ; preds = %14
%24 = icmp eq i32 %15, 4
br i1 %24, label %27, label %25
25: ; preds = %23
%26 = tail call i32 @_gen_err(i32 -3)
br label %30
27: ; preds = %23
%28 = load i32, i32* %2
%29 = tail call i32 @be64toh(i32 %28)
store i32 %29, i32* %2
br label %30
30: ; preds = %25, %27, %21
%31 = phi i32 [ %22, %21 ], [ %26, %25 ], [ 0, %27 ]
%32 = tail call i32 @close(i32 %5)
br label %33
33: ; preds = %30, %12, %11
%34 = phi i32 [ 0, %11 ], [ %13, %12 ], [ %31, %30 ]
ret i32 %34
}