#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 const AT_FDCWD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ create_file(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ openat(int const, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int unlinkat(int const, char *, int /*<<< orphan*/);
int /*<<< orphan*/ verify_fd(int /*<<< orphan*/);
int main(void) {
  verify_fd(openat(AT_FDCWD, "x", O_RDONLY));
  const int parfd = open("..", O_RDONLY);
  verify_fd(openat(parfd, "atfuncs/x", O_RDONLY));
  create_file("scratch");
  if (-1 == unlinkat(AT_FDCWD, "scratch", 0)) {
    perror("unlinkat (AT_FDCWD, \"scratch\")");
    return -1;
  }
  create_file("scratch2");
  if (-1 == unlinkat(parfd, "atfuncs/scratch2", 0)) {
    perror("unlinkat (AT_FDCWD, \"scratch\")");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
lea    0xe87(%rip),%rsi
call   1040 <openat@plt>
mov    %eax,%edi
call   1050 <verify_fd@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe60(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
lea    0xe48(%rip),%rsi
call   1040 <openat@plt>
mov    %eax,%edi
call   1050 <verify_fd@plt>
lea    0xe3f(%rip),%rdi
call   1080 <create_file@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
xor    %edx,%edx
call   1030 <unlinkat@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1215 <main+0xb5>
lea    0xe15(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <main+0x100>
lea    0xe24(%rip),%rdi
call   1080 <create_file@plt>
mov    -0x8(%rbp),%edi
lea    0xe0d(%rip),%rsi
xor    %edx,%edx
call   1030 <unlinkat@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1259 <main+0xf9>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <main+0x100>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e7d(%rip),%rbx
mov    (%rbx),%edx
lea    0xe9b(%rip),%rsi
xor    %r14d,%r14d
xor    %edi,%edi
call   1040 <openat@plt>
mov    %eax,%edi
call   1050 <verify_fd@plt>
mov    (%rbx),%esi
lea    0xe76(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
mov    (%rbx),%edx
lea    0xe69(%rip),%rsi
mov    %eax,%edi
call   1040 <openat@plt>
mov    %eax,%edi
call   1050 <verify_fd@plt>
lea    0xe5e(%rip),%rbx
mov    %rbx,%rdi
call   1080 <create_file@plt>
xor    %edi,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <unlinkat@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0x95>
lea    0xe6d(%rip),%rdi
call   1080 <create_file@plt>
lea    0xe59(%rip),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <unlinkat@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0x95>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11ed <main+0x8d>

LLVM IR (-O0):
@AT_FDCWD = constant i32 0
@.str = [2 x i8] c"x\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [10 x i8] c"atfuncs/x\00"
@.str.3 = [8 x i8] c"scratch\00"
@.str.4 = [31 x i8] c"unlinkat (AT_FDCWD, \22scratch\22)\00"
@.str.5 = [9 x i8] c"scratch2\00"
@.str.6 = [17 x i8] c"atfuncs/scratch2\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @AT_FDCWD
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @openat(i32 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @verify_fd(i32 %5)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @openat(i32 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = call i32 @verify_fd(i32 %11)
%13 = call i32 @create_file(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%14 = load i32, i32* @AT_FDCWD
%15 = call i32 @unlinkat(i32 %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 0)
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
19: ; preds = %0
%20 = call i32 @create_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%21 = load i32, i32* %2
%22 = call i32 @unlinkat(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 0)
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
26: ; preds = %19
store i32 0, i32* %1
br label %27
27: ; preds = %26, %24, %17
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@AT_FDCWD = constant i32 0
@.str = [2 x i8] c"x\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [10 x i8] c"atfuncs/x\00"
@.str.3 = [8 x i8] c"scratch\00"
@.str.4 = [31 x i8] c"unlinkat (AT_FDCWD, \22scratch\22)\00"
@.str.5 = [9 x i8] c"scratch2\00"
@.str.6 = [17 x i8] c"atfuncs/scratch2\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @openat(i32 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 @verify_fd(i32 %2)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @openat(i32 %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%8 = tail call i32 @verify_fd(i32 %7)
%9 = tail call i32 @create_file(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%10 = tail call i32 @unlinkat(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %16, label %12
12: ; preds = %0
%13 = tail call i32 @create_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%14 = tail call i32 @unlinkat(i32 %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12, %0
%17 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = phi i32 [ 0, %12 ], [ -1, %16 ]
ret i32 %19
}