#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 AT_FDCWD;
int /*<<< orphan*/ O_DIRECTORY;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int openat(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print(int);
int main() {
  int fd = open("requirements.txt", O_RDONLY);
  if (fd < 0)
    perror("open readme");
  print(fd);
  close(fd);
  chdir("examples/files");
  fd = open("openat.c", O_RDONLY);
  if (fd < 0)
    perror("open chdired");
  print(fd);
  close(fd);
  int fdd = open("/etc", O_DIRECTORY);
  if (fdd < 0) {
    perror("opendir");
  }
  fd = openat(fdd, "passwd", O_RDONLY);
  if (fd < 0)
    perror("open passwd");
  print(fd);
  close(fd);
  fd = openat(fdd, "/proc/meminfo", O_RDONLY);
  if (fd < 0)
    perror("open passwd");
  print(fd);
  close(fd);
  fd = openat(AT_FDCWD, "../Makefile", O_RDONLY);
  if (fd < 0)
    perror("open passwd");
  print(fd);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119d <main+0x3d>
lea    0xe79(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <print@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    0xe69(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e7 <main+0x87>
lea    0xe53(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <print@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1225 <main+0xc5>
lea    0xe27(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
lea    0xe1c(%rip),%rsi
call   1030 <openat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1256 <main+0xf6>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <print@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
lea    0xde2(%rip),%rsi
call   1030 <openat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1297 <main+0x137>
lea    0xdbd(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <print@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
lea    0xda9(%rip),%rsi
call   1030 <openat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    12de <main+0x17e>
lea    0xd76(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <print@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e7d(%rip),%r14
mov    (%r14),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1269 <main+0x109>
mov    %ebx,%edi
call   1060 <print@plt>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe84(%rip),%rdi
call   1040 <chdir@plt>
mov    (%r14),%esi
lea    0xe84(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     127a <main+0x11a>
mov    %ebx,%edi
call   1060 <print@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     128b <main+0x12b>
mov    (%r14),%edx
lea    0xe66(%rip),%rsi
mov    %ebx,%edi
call   1030 <openat@plt>
mov    %eax,%ebp
test   %eax,%eax
js     129c <main+0x13c>
mov    %ebp,%edi
call   1060 <print@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%r14),%edx
lea    0xe44(%rip),%rsi
mov    %ebx,%edi
call   1030 <openat@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12ad <main+0x14d>
mov    %ebx,%edi
call   1060 <print@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%edx
lea    0xe20(%rip),%rsi
call   1030 <openat@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12be <main+0x15e>
mov    %ebx,%edi
call   1060 <print@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xda1(%rip),%rdi
call   1080 <perror@plt>
jmp    1184 <main+0x24>
lea    0xdb4(%rip),%rdi
call   1080 <perror@plt>
jmp    11b7 <main+0x57>
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
jmp    11e4 <main+0x84>
lea    0xdac(%rip),%rdi
call   1080 <perror@plt>
jmp    11ff <main+0x9f>
lea    0xd9b(%rip),%rdi
call   1080 <perror@plt>
jmp    1228 <main+0xc8>
lea    0xd8a(%rip),%rdi
call   1080 <perror@plt>
jmp    1254 <main+0xf4>

LLVM IR (-O0):
@.str = [17 x i8] c"requirements.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open readme\00"
@.str.2 = [15 x i8] c"examples/files\00"
@.str.3 = [9 x i8] c"openat.c\00"
@.str.4 = [13 x i8] c"open chdired\00"
@.str.5 = [5 x i8] c"/etc\00"
@O_DIRECTORY = global i32 0
@.str.6 = [8 x i8] c"opendir\00"
@.str.7 = [7 x i8] c"passwd\00"
@.str.8 = [12 x i8] c"open passwd\00"
@.str.9 = [14 x i8] c"/proc/meminfo\00"
@AT_FDCWD = global i32 0
@.str.10 = [12 x i8] c"../Makefile\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* %2
%12 = call i32 @print(i32 %11)
%13 = load i32, i32* %2
%14 = call i32 @close(i32 %13)
%15 = call i32 @chdir(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %10
%23 = load i32, i32* %2
%24 = call i32 @print(i32 %23)
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @O_DIRECTORY
%28 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %27)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %33
33: ; preds = %31, %22
%34 = load i32, i32* %3
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @openat(i32 %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %35)
store i32 %36, i32* %2
%37 = load i32, i32* %2
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
%42 = load i32, i32* %2
%43 = call i32 @print(i32 %42)
%44 = load i32, i32* %2
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %3
%47 = load i32, i32* @O_RDONLY
%48 = call i32 @openat(i32 %46, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i32 %47)
store i32 %48, i32* %2
%49 = load i32, i32* %2
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %41
%52 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %53
53: ; preds = %51, %41
%54 = load i32, i32* %2
%55 = call i32 @print(i32 %54)
%56 = load i32, i32* %2
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* @AT_FDCWD
%59 = load i32, i32* @O_RDONLY
%60 = call i32 @openat(i32 %58, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i32 %59)
store i32 %60, i32* %2
%61 = load i32, i32* %2
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %53
%64 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %65
65: ; preds = %63, %53
%66 = load i32, i32* %2
%67 = call i32 @print(i32 %66)
%68 = load i32, i32* %2
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
@.str = [17 x i8] c"requirements.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open readme\00"
@.str.2 = [15 x i8] c"examples/files\00"
@.str.3 = [9 x i8] c"openat.c\00"
@.str.4 = [13 x i8] c"open chdired\00"
@.str.5 = [5 x i8] c"/etc\00"
@O_DIRECTORY = global i32 0
@.str.6 = [8 x i8] c"opendir\00"
@.str.7 = [7 x i8] c"passwd\00"
@.str.8 = [12 x i8] c"open passwd\00"
@.str.9 = [14 x i8] c"/proc/meminfo\00"
@AT_FDCWD = global i32 0
@.str.10 = [12 x i8] c"../Makefile\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = tail call i32 @print(i32 %2)
%8 = tail call i32 @close(i32 %2)
%9 = tail call i32 @chdir(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %15
15: ; preds = %13, %6
%16 = tail call i32 @print(i32 %11)
%17 = tail call i32 @close(i32 %11)
%18 = load i32, i32* @O_DIRECTORY
%19 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = load i32, i32* @O_RDONLY
%25 = tail call i32 @openat(i32 %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = tail call i32 @print(i32 %25)
%31 = tail call i32 @close(i32 %25)
%32 = load i32, i32* @O_RDONLY
%33 = tail call i32 @openat(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = tail call i32 @print(i32 %33)
%39 = tail call i32 @close(i32 %33)
%40 = load i32, i32* @AT_FDCWD
%41 = load i32, i32* @O_RDONLY
%42 = tail call i32 @openat(i32 %40, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %37
%45 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %46
46: ; preds = %44, %37
%47 = tail call i32 @print(i32 %42)
%48 = tail call i32 @close(i32 %42)
ret i32 0
}