#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 mode_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchdir(int);
int /*<<< orphan*/ mkdir(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ rmdir(char *);
int main() {
  int fd;
  mkdir("foobar", 0765);
  mkdir((char *)-1, 0765);
  mkdir("foobar2", (mode_t)-1);
  chdir("foobar");
  chdir("..");
  chdir((char *)-1);
  fd = open("foobar", O_RDONLY);
  fchdir(fd);
  fchdir(-1);
  chdir("..");
  close(fd);
  rmdir("foobar");
  rmdir((char *)-1);
  fd = open(".", O_RDONLY);
  close(fd);
  rmdir("xyzzy");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x1f5,%esi
call   1030 <mkdir@plt>
mov    $0xffffffffffffffff,%rdi
mov    $0x1f5,%esi
call   1030 <mkdir@plt>
lea    0xe6f(%rip),%rdi
mov    $0xffffffff,%esi
call   1030 <mkdir@plt>
lea    0xe57(%rip),%rdi
call   1050 <chdir@plt>
lea    0xe5a(%rip),%rdi
call   1050 <chdir@plt>
mov    $0xffffffffffffffff,%rdi
call   1050 <chdir@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <fchdir@plt>
mov    $0xffffffff,%edi
call   1070 <fchdir@plt>
lea    0xe18(%rip),%rdi
call   1050 <chdir@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xdf5(%rip),%rdi
call   1040 <rmdir@plt>
mov    $0xffffffffffffffff,%rdi
call   1040 <rmdir@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xde4(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xdcf(%rip),%rdi
call   1040 <rmdir@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe92(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1f5,%esi
call   1030 <mkdir@plt>
mov    $0xffffffffffffffff,%rdi
mov    $0x1f5,%esi
call   1030 <mkdir@plt>
lea    0xe74(%rip),%rdi
mov    $0xffffffff,%esi
call   1030 <mkdir@plt>
mov    %rbx,%rdi
call   1050 <chdir@plt>
lea    0xe63(%rip),%r14
mov    %r14,%rdi
call   1050 <chdir@plt>
mov    $0xffffffffffffffff,%rdi
call   1050 <chdir@plt>
mov    0x2e21(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <fchdir@plt>
mov    $0xffffffff,%edi
call   1070 <fchdir@plt>
mov    %r14,%rdi
call   1050 <chdir@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1040 <rmdir@plt>
mov    $0xffffffffffffffff,%rdi
call   1040 <rmdir@plt>
mov    (%r15),%esi
lea    0xdfe(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%edi
call   1060 <close@plt>
lea    0xded(%rip),%rdi
call   1040 <rmdir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"foobar\00"
@.str.1 = [8 x i8] c"foobar2\00"
@.str.2 = [3 x i8] c"..\00"
@O_RDONLY = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [6 x i8] c"xyzzy\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @mkdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 501)
%4 = call i32 @mkdir(i8* inttoptr (i64 -1 to i8*), i32 501)
%5 = call i32 @mkdir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 -1)
%6 = call i32 @chdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @chdir(i8* inttoptr (i64 -1 to i8*))
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = call i32 @fchdir(i32 %11)
%13 = call i32 @fchdir(i32 -1)
%14 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* %2
%16 = call i32 @close(i32 %15)
%17 = call i32 @rmdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @rmdir(i8* inttoptr (i64 -1 to i8*))
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %19)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
%23 = call i32 @rmdir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"foobar\00"
@.str.1 = [8 x i8] c"foobar2\00"
@.str.2 = [3 x i8] c"..\00"
@O_RDONLY = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [6 x i8] c"xyzzy\00"
define i32 @main() {
%1 = tail call i32 @mkdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 501)
%2 = tail call i32 @mkdir(i8* inttoptr (i64 -1 to i8*), i32 501)
%3 = tail call i32 @mkdir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 -1)
%4 = tail call i32 @chdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i32 @chdir(i8* inttoptr (i64 -1 to i8*))
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @fchdir(i32 %8)
%10 = tail call i32 @fchdir(i32 -1)
%11 = tail call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @close(i32 %8)
%13 = tail call i32 @rmdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @rmdir(i8* inttoptr (i64 -1 to i8*))
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = tail call i32 @close(i32 %16)
%18 = tail call i32 @rmdir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}