#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ _PC_NAME_MAX;
int /*<<< orphan*/ _PC_PATH_MAX;
int chdir(char *);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ get_random_name(char *, int);
scalar_t__ malloc(int);
int mkdir(char *, int);
int open(char *, int, int);
int pathconf(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  char const *path = "/";
  int name_max = pathconf(path, _PC_NAME_MAX);
  int path_max = pathconf(path, _PC_PATH_MAX);
  printf("NAME_MAX = %d, PATH_MAX = %d\n", name_max, path_max);
  name_max = 25;
  int name_len = 0;
  name_len = name_max;
  char *name = (char *)malloc(name_len + 1);
  if (name == 0)
    return -1;
  get_random_name(name, name_len);
  int fd = open(name, O_RDWR | O_CREAT, 0644);
  if (fd == -1)
    printf("open %s failed, errno = %d\n", name, errno);
  else {
    printf("open %s OK.\n", name);
    close(fd);
  }
  int ret = 0, level = 0;
  do {
    level++;
    get_random_name(name, name_len);
    ret = mkdir(name, 0777);
    if (ret == -1) {
      printf("mkdir %s failed, errno = %d, level = %d\n", name, errno, level);
      break;
    }
    ret = chdir(name);
    if (ret == -1) {
      printf("chdir %s failed, errno = %d, level = %d\n", name, errno, level);
      break;
    }
    printf("mkdir %s @ %d\n", name, level);
  } while (1);
  free(name);
  return 0;
}


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)
lea    0xe53(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1050 <pathconf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1050 <pathconf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x19,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1229 <main+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <main+0x1da>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   10b0 <get_random_name@plt>
mov    -0x30(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    0x2daf(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1282 <main+0xf2>
mov    -0x30(%rbp),%rsi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    129c <main+0x10c>
mov    -0x30(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   10b0 <get_random_name@plt>
mov    -0x30(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    12fd <main+0x16d>
mov    -0x30(%rbp),%rsi
mov    0x2ce3(%rip),%rax
mov    (%rax),%edx
mov    -0x3c(%rbp),%ecx
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    135a <main+0x1ca>
mov    -0x30(%rbp),%rdi
call   1060 <chdir@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1336 <main+0x1a6>
mov    -0x30(%rbp),%rsi
mov    0x2caa(%rip),%rax
mov    (%rax),%edx
mov    -0x3c(%rbp),%ecx
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    135a <main+0x1ca>
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%al
test   $0x1,%al
jne    12aa <main+0x11a>
jmp    135a <main+0x1ca>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rbx
mov    %rbx,%rdi
call   1050 <pathconf@plt>
mov    %eax,%ebp
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <pathconf@plt>
lea    0xe3c(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1a,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1230 <main+0xa0>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x19,%esi
call   10b0 <get_random_name@plt>
mov    0x2de9(%rip),%rax
mov    0x2dfa(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     123a <main+0xaa>
mov    %eax,%ebp
lea    0xe1f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1254 <main+0xc4>
mov    $0xffffffff,%eax
jmp    12f6 <main+0x166>
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
lea    0xdd6(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    $0x19,%esi
call   10b0 <get_random_name@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
je     12be <main+0x12e>
lea    0xe1c(%rip),%r14
nop
mov    %rbx,%rdi
call   1060 <chdir@plt>
cmp    $0xffffffff,%eax
je     12d0 <main+0x140>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x19,%esi
call   10b0 <get_random_name@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    1280 <main+0xf0>
mov    0x2d03(%rip),%rax
mov    (%rax),%edx
lea    0xd7b(%rip),%rdi
jmp    12e0 <main+0x150>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edx
lea    0xd92(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@_PC_NAME_MAX = global i32 0
@_PC_PATH_MAX = global i32 0
@.str.1 = [30 x i8] c"NAME_MAX = %d, PATH_MAX = %d\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [28 x i8] c"open %s failed, errno = %d\0A\00"
@errno = global i32 0
@.str.3 = [13 x i8] c"open %s OK.\0A\00"
@.str.4 = [41 x i8] c"mkdir %s failed, errno = %d, level = %d\0A\00"
@.str.5 = [41 x i8] c"chdir %s failed, errno = %d, level = %d\0A\00"
@.str.6 = [15 x i8] c"mkdir %s @ %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6
%14 = load i8*, i8** %6
%15 = load i32, i32* @_PC_NAME_MAX
%16 = call i32 @pathconf(i8* %14, i32 %15)
store i32 %16, i32* %7
%17 = load i8*, i8** %6
%18 = load i32, i32* @_PC_PATH_MAX
%19 = call i32 @pathconf(i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21)
store i32 25, i32* %7
store i32 0, i32* %9
%23 = load i32, i32* %7
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %10
%28 = load i8*, i8** %10
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %2
store i32 -1, i32* %3
br label %85
31: ; preds = %2
%32 = load i8*, i8** %10
%33 = load i32, i32* %9
%34 = call i32 @get_random_name(i8* %32, i32 %33)
%35 = load i8*, i8** %10
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = call i32 @open(i8* %35, i32 %38, i32 420)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %31
%43 = load i8*, i8** %10
%44 = load i32, i32* @errno
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %43, i32 %44)
br label %51
46: ; preds = %31
%47 = load i8*, i8** %10
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = load i32, i32* %11
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %46, %42
store i32 0, i32* %12
store i32 0, i32* %13
br label %52
52: ; preds = %81, %51
%53 = load i32, i32* %13
%54 = add nsw i32 %53, 1
store i32 %54, i32* %13
%55 = load i8*, i8** %10
%56 = load i32, i32* %9
%57 = call i32 @get_random_name(i8* %55, i32 %56)
%58 = load i8*, i8** %10
%59 = call i32 @mkdir(i8* %58, i32 511)
store i32 %59, i32* %12
%60 = load i32, i32* %12
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %67
62: ; preds = %52
%63 = load i8*, i8** %10
%64 = load i32, i32* @errno
%65 = load i32, i32* %13
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %63, i32 %64, i32 %65)
br label %82
67: ; preds = %52
%68 = load i8*, i8** %10
%69 = call i32 @chdir(i8* %68)
store i32 %69, i32* %12
%70 = load i32, i32* %12
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %77
72: ; preds = %67
%73 = load i8*, i8** %10
%74 = load i32, i32* @errno
%75 = load i32, i32* %13
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %73, i32 %74, i32 %75)
br label %82
77: ; preds = %67
%78 = load i8*, i8** %10
%79 = load i32, i32* %13
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %78, i32 %79)
br label %81
81: ; preds = %77
br i1 true, label %52, label %82
82: ; preds = %81, %72, %62
%83 = load i8*, i8** %10
%84 = call i32 @free(i8* %83)
store i32 0, i32* %3
br label %85
85: ; preds = %82, %30
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@_PC_NAME_MAX = global i32 0
@_PC_PATH_MAX = global i32 0
@.str.1 = [30 x i8] c"NAME_MAX = %d, PATH_MAX = %d\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [28 x i8] c"open %s failed, errno = %d\0A\00"
@errno = global i32 0
@.str.3 = [13 x i8] c"open %s OK.\0A\00"
@.str.4 = [41 x i8] c"mkdir %s failed, errno = %d, level = %d\0A\00"
@.str.5 = [41 x i8] c"chdir %s failed, errno = %d, level = %d\0A\00"
@.str.6 = [15 x i8] c"mkdir %s @ %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @_PC_NAME_MAX
%4 = tail call i32 @pathconf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @_PC_PATH_MAX
%6 = tail call i32 @pathconf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %6)
%8 = tail call i64 @malloc(i32 26)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %47, label %11
11: ; preds = %2
%12 = tail call i32 @get_random_name(i8* %9, i32 25)
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* %9, i32 %15, i32 420)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* @errno
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %9, i32 %19)
br label %24
21: ; preds = %11
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%23 = tail call i32 @close(i32 %16)
br label %24
24: ; preds = %21, %18
%25 = tail call i32 @get_random_name(i8* %9, i32 25)
%26 = tail call i32 @mkdir(i8* %9, i32 511)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %39, %24
%29 = phi i32 [ 1, %24 ], [ %41, %39 ]
%30 = load i32, i32* @errno
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %9, i32 %30, i32 %29)
br label %45
32: ; preds = %24, %39
%33 = phi i32 [ %41, %39 ], [ 1, %24 ]
%34 = tail call i32 @chdir(i8* %9)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* @errno
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %9, i32 %37, i32 %33)
br label %45
39: ; preds = %32
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %9, i32 %33)
%41 = add nuw nsw i32 %33, 1
%42 = tail call i32 @get_random_name(i8* %9, i32 25)
%43 = tail call i32 @mkdir(i8* %9, i32 511)
%44 = icmp eq i32 %43, -1
br i1 %44, label %28, label %32
45: ; preds = %36, %28
%46 = tail call i32 @free(i8* %9)
br label %47
47: ; preds = %2, %45
%48 = phi i32 [ 0, %45 ], [ -1, %2 ]
ret i32 %48
}