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

/* Variables and functions */
scalar_t__ EINTR;
int O_CREAT;
int O_WRONLY;
int _gen_err(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ htobe64(int /*<<< orphan*/);
int openat(int, char const *, int, int);
int write(int, int /*<<< orphan*/ *, int);
int set_idx(int dirfd, const char *path, uint64_t val) {
  int status = 0;
  int rc;
  int fd = openat(dirfd, path, O_WRONLY | O_CREAT, 0644);
  if (fd < 0)
    return _gen_err(-2);
  val = htobe64(val);
  while ((rc = write(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);
  close(fd);
  return status;
}


x86 assembly (clang, -O0):
<set_idx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    0x2e78(%rip),%rax
or     (%rax),%edx
mov    $0x1a4,%ecx
call   1040 <openat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11ab <set_idx+0x5b>
mov    $0xfffffffe,%edi
call   1070 <_gen_err@plt>
mov    %eax,-0x4(%rbp)
jmp    1241 <set_idx+0xf1>
mov    -0x14(%rbp),%edi
call   1060 <htobe64@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11f7 <set_idx+0xa7>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
je     11f2 <set_idx+0xa2>
jmp    11f7 <set_idx+0xa7>
jmp    11b6 <set_idx+0x66>
cmpl   $0x0,-0x1c(%rbp)
jge    1213 <set_idx+0xc3>
mov    $0xfffffffe,%edi
call   1070 <_gen_err@plt>
mov    %eax,-0x18(%rbp)
jmp    1233 <set_idx+0xe3>
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
je     122e <set_idx+0xde>
mov    $0xfffffffd,%edi
call   1070 <_gen_err@plt>
mov    %eax,-0x18(%rbp)
jmp    1233 <set_idx+0xe3>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_idx>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    0x2e60(%rip),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x1a4,%ecx
call   1040 <openat@plt>
test   %eax,%eax
js     11c3 <set_idx+0x73>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <htobe64@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%r14
mov    0x2e46(%rip),%r15
mov    0x2e37(%rip),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11d7 <set_idx+0x87>
mov    (%r15),%rax
cmp    0x0(%rbp),%rax
je     11a0 <set_idx+0x50>
mov    $0xfffffffe,%edi
jmp    11e3 <set_idx+0x93>
mov    $0xfffffffe,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <_gen_err@plt>
mov    $0xfffffffd,%edi
xor    %ebp,%ebp
cmp    $0x4,%eax
je     11ea <set_idx+0x9a>
call   1070 <_gen_err@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @set_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_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @openat(i32 %11, i8* %12, i32 %15, i32 420)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @_gen_err(i32 -2)
store i32 %20, i32* %4
br label %50
21: ; preds = %3
%22 = load i32, i32* %7
%23 = call i32 @htobe64(i32 %22)
store i32 %23, i32* %7
br label %24
24: ; preds = %33, %21
%25 = load i32, i32* %10
%26 = call i32 @write(i32 %25, i32* %7, i32 4)
store i32 %26, i32* %9
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %34
33: ; preds = %28
br label %24
34: ; preds = %32, %24
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @_gen_err(i32 -2)
store i32 %38, i32* %8
br label %46
39: ; preds = %34
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = icmp ne i64 %41, 4
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @_gen_err(i32 -3)
store i32 %44, i32* %8
br label %45
45: ; preds = %43, %39
br label %46
46: ; preds = %45, %37
%47 = load i32, i32* %10
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %8
store i32 %49, i32* %4
br label %50
50: ; preds = %46, %19
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @set_idx(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = tail call i32 @openat(i32 %0, i8* %1, i32 %7, i32 420)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @_gen_err(i32 -2)
br label %29
12: ; preds = %3
%13 = tail call i32 @htobe64(i32 %2)
store i32 %13, i32* %4
br label %14
14: ; preds = %17, %12
%15 = call i32 @write(i32 %8, i32* %4, i32 4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
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 %23
21: ; preds = %14
%22 = icmp eq i32 %15, 4
br i1 %22, label %26, label %23
23: ; preds = %17, %21
%24 = phi i32 [ -3, %21 ], [ -2, %17 ]
%25 = tail call i32 @_gen_err(i32 %24)
br label %26
26: ; preds = %23, %21
%27 = phi i32 [ 0, %21 ], [ %25, %23 ]
%28 = tail call i32 @close(i32 %8)
br label %29
29: ; preds = %26, %10
%30 = phi i32 [ %11, %10 ], [ %27, %26 ]
ret i32 %30
}