#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int openat(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
void test_openat_read() {
  int dir = open("/dev", O_RDONLY);
  int fd = openat(dir, "zero", O_RDONLY);
  if (fd == -1) {
    puts("Can't open /dev/zero with openat()");
  };
  close(fd);
  close(dir);
}


x86 assembly (clang, -O0):
<test_openat_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e7e(%rip),%rax
mov    (%rax),%edx
lea    0xe92(%rip),%rsi
call   1040 <openat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1191 <test_openat_read+0x51>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_openat_read>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xeac(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%edx
lea    0xea0(%rip),%rsi
mov    %eax,%edi
call   1040 <openat@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    117f <test_openat_read+0x3f>
lea    0xe90(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"/dev\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"zero\00"
@.str.2 = [35 x i8] c"Can't open /dev/zero with openat()\00"
define void @test_openat_read() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @openat(i32 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* %2
%14 = call i32 @close(i32 %13)
%15 = load i32, i32* %1
%16 = call i32 @close(i32 %15)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"/dev\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"zero\00"
@.str.2 = [35 x i8] c"Can't open /dev/zero with openat()\00"
define void @test_openat_read() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @openat(i32 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = tail call i32 @close(i32 %4)
%10 = tail call i32 @close(i32 %2)
ret void
}