#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 PATH_MAX;
int /*<<< orphan*/ S_IRWXU;
scalar_t__ chdir(char *);
int /*<<< orphan*/ chmod(char *, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ mkdir(char *, int /*<<< orphan*/);
char *mkdtemp(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rmdir(char *);
scalar_t__ strlen(char *);
int main(int argc, char **argv) {
  char template[] = "/tmp/wd_test_XXXXXX";
  char *tmpdir = mkdtemp(template);
  if (tmpdir == NULL) {
    perror("Couldn't mkdtemp");
    exit(-1);
  }
  if (chdir(tmpdir) != 0) {
    perror("Couldn't chdir into tmpdir");
    exit(-1);
  }
  int dirslen = PATH_MAX;
  while (dirslen > 0) {
    mkdir("subdir", S_IRWXU);
    chdir("subdir");
    dirslen -= strlen("subdir");
  }
  chmod(tmpdir, 0);
  rmdir("../subdir");
  execlp("echo", "echo", "Hello", "World", (char *)NULL);
  perror("Couldn't execlp");
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe53(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe50(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe4e(%rip),%eax
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <mkdtemp@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f3 <main+0x63>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
je     121c <main+0x8c>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    1271 <main+0xe1>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0xe01(%rip),%rdi
call   1030 <mkdir@plt>
lea    0xdf5(%rip),%rdi
call   1060 <chdir@plt>
lea    0xde9(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x3c(%rbp)
jmp    1228 <main+0x98>
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1080 <chmod@plt>
lea    0xdbd(%rip),%rdi
call   1040 <rmdir@plt>
lea    0xdbb(%rip),%rsi
lea    0xdb9(%rip),%rdx
lea    0xdb8(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10b0 <execlp@plt>
lea    0xdaa(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movaps 0xe6f(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x585858,0x10(%rsp)
mov    %rsp,%rdi
call   1060 <mkdtemp@plt>
test   %rax,%rax
je     124d <main+0xcd>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    1256 <main+0xd6>
mov    0x2e27(%rip),%rax
mov    (%rax),%ebp
test   %ebp,%ebp
jle    11fb <main+0x7b>
add    $0x6,%ebp
mov    0x2e07(%rip),%r15
lea    0xe63(%rip),%rbx
mov    (%r15),%esi
mov    %rbx,%rdi
call   1030 <mkdir@plt>
mov    %rbx,%rdi
call   1050 <chdir@plt>
add    $0xfffffffa,%ebp
cmp    $0x6,%ebp
ja     11e0 <main+0x60>
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <chmod@plt>
lea    0xe34(%rip),%rdi
call   1040 <rmdir@plt>
lea    0xe32(%rip),%rdi
lea    0xe30(%rip),%rdx
lea    0xe2f(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
call   10a0 <execlp@plt>
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
jmp    125d <main+0xdd>
lea    0xdc8(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.main.template = [20 x i8] c"/tmp/wd_test_XXXXXX\00"
@.str = [17 x i8] c"Couldn't mkdtemp\00"
@.str.1 = [27 x i8] c"Couldn't chdir into tmpdir\00"
@PATH_MAX = global i32 0
@.str.2 = [7 x i8] c"subdir\00"
@S_IRWXU = global i32 0
@.str.3 = [10 x i8] c"../subdir\00"
@.str.4 = [5 x i8] c"echo\00"
@.str.5 = [6 x i8] c"Hello\00"
@.str.6 = [6 x i8] c"World\00"
@.str.7 = [16 x i8] c"Couldn't execlp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [20 x i8]
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [20 x i8]* %6 to i8*
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%11 = call i8* @mkdtemp(i8* %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %7
%19 = call i64 @chdir(i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %17
%25 = load i32, i32* @PATH_MAX
store i32 %25, i32* %8
br label %26
26: ; preds = %29, %24
%27 = load i32, i32* %8
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i32, i32* @S_IRWXU
%31 = call i32 @mkdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = call i64 @chdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%33 = call i64 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = sub nsw i64 %35, %33
%37 = trunc i64 %36 to i32
store i32 %37, i32* %8
br label %26
38: ; preds = %26
%39 = load i8*, i8** %7
%40 = call i32 @chmod(i8* %39, i32 0)
%41 = call i32 @rmdir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* null)
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
ret i32 -1
}

LLVM IR (-O2):
@__const.main.template = [20 x i8] c"/tmp/wd_test_XXXXXX\00"
@.str = [17 x i8] c"Couldn't mkdtemp\00"
@.str.1 = [27 x i8] c"Couldn't chdir into tmpdir\00"
@PATH_MAX = global i32 0
@.str.2 = [7 x i8] c"subdir\00"
@S_IRWXU = global i32 0
@.str.3 = [10 x i8] c"../subdir\00"
@.str.4 = [5 x i8] c"echo\00"
@.str.5 = [6 x i8] c"Hello\00"
@.str.6 = [6 x i8] c"World\00"
@.str.7 = [16 x i8] c"Couldn't execlp\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = call i8* @mkdtemp(i8* %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = call i64 @chdir(i8* %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %10
%17 = load i32, i32* @PATH_MAX
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %16, %19
%20 = phi i32 [ %24, %19 ], [ %17, %16 ]
%21 = load i32, i32* @S_IRWXU
%22 = call i32 @mkdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i64 @chdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = add nsw i32 %20, -6
%25 = icmp ugt i32 %20, 6
br i1 %25, label %19, label %26
26: ; preds = %19, %16
%27 = call i32 @chmod(i8* %5, i32 0)
%28 = call i32 @rmdir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* null)
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
ret i32 -1
}