#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_TRUNC;
char *alloca(unsigned int);
unsigned int atoi(char *);
scalar_t__ chdir(char *);
scalar_t__ creat(char *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned int);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  unsigned int depth;
  unsigned int len;
  unsigned int i;
  char *root;
  char *dirname;
  char *filename;
  if (argc == 5) {
    root = argv[1];
    filename = argv[2];
    depth = atoi(argv[3]);
    len = atoi(argv[4]);
  } else {
    fprintf(stderr, "Usage: %s dirname filename depth dirlen\n", argv[0]);
    return 1;
  }
  dirname = alloca(len + 2);
  if (!dirname) {
    fprintf(stderr, "No stack space!\n");
    return 2;
  }
  memset(dirname, '0', len);
  dirname[len] = 0;
  if (chdir(root) < 0) {
    fprintf(stderr, "Failed to chdir to %s (%d)!\n", root, errno);
    return 3;
  }
  for (i = 0; i < depth; i++) {
    if (mkdir(dirname, 0700) < 0) {
      fprintf(stderr, "Failed to mkdir n.%u (%d)!\n", i, errno);
      return 4;
    }
    if (chdir(dirname) < 0) {
      fprintf(stderr, "Failed to chdir n.%u (%d)!\n", i, errno);
      return 5;
    }
  }
  if (creat(filename, O_CREAT | O_TRUNC) < 0) {
    fprintf(stderr, "Failed to create file (%d)!\n", errno);
    return 10 + errno;
  }
  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)
cmpl   $0x5,-0x8(%rbp)
jne    11cd <main+0x5d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    11f7 <main+0x87>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a2 <main+0x232>
mov    -0x18(%rbp),%edi
add    $0x2,%edi
call   1070 <alloca@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1234 <main+0xc4>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a2 <main+0x232>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x30,%esi
call   1050 <memset@plt>
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1293 <main+0x123>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    0x2d51(%rip),%rax
mov    (%rax),%ecx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    13a2 <main+0x232>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1342 <main+0x1d2>
mov    -0x30(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    12ed <main+0x17d>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    0x2cf7(%rip),%rax
mov    (%rax),%ecx
lea    0xd7d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x4,-0x4(%rbp)
jmp    13a2 <main+0x232>
mov    -0x30(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    132f <main+0x1bf>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    0x2cb5(%rip),%rax
mov    (%rax),%ecx
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x5,-0x4(%rbp)
jmp    13a2 <main+0x232>
jmp    1334 <main+0x1c4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    129a <main+0x12a>
mov    -0x38(%rbp),%rdi
mov    0x2cab(%rip),%rax
mov    (%rax),%esi
mov    0x2c9a(%rip),%rax
or     (%rax),%esi
call   1090 <creat@plt>
cmp    $0x0,%rax
jge    139b <main+0x22b>
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
mov    0x2c51(%rip),%rax
mov    (%rax),%edx
lea    0xd0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c3a(%rip),%rax
mov    (%rax),%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    13a2 <main+0x232>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x5,%edi
jne    1271 <main+0x101>
mov    0x8(%rbx),%r15
mov    0x10(%rbx),%r14
mov    0x18(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%r12d
mov    0x20(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
lea    0x2(%rbp),%edi
call   1070 <alloca@plt>
test   %rax,%rax
je     1295 <main+0x125>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x30,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r15,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     12b3 <main+0x143>
test   %r12d,%r12d
je     121f <main+0xaf>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
js     12dd <main+0x16d>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     1306 <main+0x196>
add    $0x1,%ebp
cmp    %ebp,%r12d
jne    11f0 <main+0x80>
mov    0x2dd2(%rip),%rax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   1090 <creat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    132d <main+0x1bd>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    0x2d71(%rip),%rbx
mov    (%rbx),%edx
lea    0xe2f(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%eax
add    $0xa,%eax
jmp    132d <main+0x1bd>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    132d <main+0x1bd>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%eax
jmp    132d <main+0x1bd>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%ecx
lea    0xd6e(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x3,%eax
jmp    132d <main+0x1bd>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%ecx
lea    0xd61(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x4,%eax
jmp    132d <main+0x1bd>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
mov    0x2cb2(%rip),%rax
mov    (%rax),%ecx
lea    0xd54(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x5,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"Usage: %s dirname filename depth dirlen\0A\00"
@.str.1 = [17 x i8] c"No stack space!\0A\00"
@.str.2 = [29 x i8] c"Failed to chdir to %s (%d)!\0A\00"
@errno = global i32 0
@.str.3 = [28 x i8] c"Failed to mkdir n.%u (%d)!\0A\00"
@.str.4 = [28 x i8] c"Failed to chdir n.%u (%d)!\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.5 = [29 x i8] c"Failed to create file (%d)!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 5
br i1 %13, label %14, label %29
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %9
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
store i8* %20, i8** %11
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 3
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %6
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 4
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %7
br label %35
29: ; preds = %2
%30 = load i32, i32* @stderr
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %33)
store i32 1, i32* %3
br label %101
35: ; preds = %14
%36 = load i32, i32* %7
%37 = add i32 %36, 2
%38 = call i8* @alloca(i32 %37)
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %44, label %41
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %101
44: ; preds = %35
%45 = load i8*, i8** %10
%46 = load i32, i32* %7
%47 = call i32 @memset(i8* %45, i32 48, i32 %46)
%48 = load i8*, i8** %10
%49 = load i32, i32* %7
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %9
%53 = call i64 @chdir(i8* %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %44
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %9
%58 = load i32, i32* @errno
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %57, i32 %58)
store i32 3, i32* %3
br label %101
60: ; preds = %44
store i32 0, i32* %8
br label %61
61: ; preds = %84, %60
%62 = load i32, i32* %8
%63 = load i32, i32* %6
%64 = icmp ult i32 %62, %63
br i1 %64, label %65, label %87
65: ; preds = %61
%66 = load i8*, i8** %10
%67 = call i64 @mkdir(i8* %66, i32 448)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %65
%70 = load i32, i32* @stderr
%71 = load i32, i32* %8
%72 = load i32, i32* @errno
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %71, i32 %72)
store i32 4, i32* %3
br label %101
74: ; preds = %65
%75 = load i8*, i8** %10
%76 = call i64 @chdir(i8* %75)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %74
%79 = load i32, i32* @stderr
%80 = load i32, i32* %8
%81 = load i32, i32* @errno
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %80, i32 %81)
store i32 5, i32* %3
br label %101
83: ; preds = %74
br label %84
84: ; preds = %83
%85 = load i32, i32* %8
%86 = add i32 %85, 1
store i32 %86, i32* %8
br label %61
87: ; preds = %61
%88 = load i8*, i8** %11
%89 = load i32, i32* @O_CREAT
%90 = load i32, i32* @O_TRUNC
%91 = or i32 %89, %90
%92 = call i64 @creat(i8* %88, i32 %91)
%93 = icmp slt i64 %92, 0
br i1 %93, label %94, label %100
94: ; preds = %87
%95 = load i32, i32* @stderr
%96 = load i32, i32* @errno
%97 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %96)
%98 = load i32, i32* @errno
%99 = add nsw i32 10, %98
store i32 %99, i32* %3
br label %101
100: ; preds = %87
store i32 0, i32* %3
br label %101
101: ; preds = %100, %94, %78, %69, %55, %41, %29
%102 = load i32, i32* %3
ret i32 %102
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"Usage: %s dirname filename depth dirlen\0A\00"
@.str.1 = [17 x i8] c"No stack space!\0A\00"
@.str.2 = [29 x i8] c"Failed to chdir to %s (%d)!\0A\00"
@errno = global i32 0
@.str.3 = [28 x i8] c"Failed to mkdir n.%u (%d)!\0A\00"
@.str.4 = [28 x i8] c"Failed to chdir n.%u (%d)!\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.5 = [29 x i8] c"Failed to create file (%d)!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 5
br i1 %3, label %4, label %18
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = getelementptr inbounds i8*, i8** %1, i64 3
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 4
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = add i32 %14, 2
%16 = tail call i8* @alloca(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %22, label %25
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %1
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %67
22: ; preds = %4
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %67
25: ; preds = %4
%26 = tail call i32 @memset(i8* %16, i32 48, i32 %14)
%27 = zext i32 %14 to i64
%28 = getelementptr inbounds i8, i8* %16, i64 %27
store i8 0, i8* %28
%29 = tail call i64 @chdir(i8* %6)
%30 = icmp slt i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = icmp eq i32 %11, 0
br i1 %32, label %55, label %37
33: ; preds = %25
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %6, i32 %35)
br label %67
37: ; preds = %31, %52
%38 = phi i32 [ %53, %52 ], [ 0, %31 ]
%39 = tail call i64 @mkdir(i8* %16, i32 448)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %38, i32 %43)
br label %67
45: ; preds = %37
%46 = tail call i64 @chdir(i8* %16)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = load i32, i32* @errno
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %38, i32 %50)
br label %67
52: ; preds = %45
%53 = add nuw i32 %38, 1
%54 = icmp eq i32 %53, %11
br i1 %54, label %55, label %37
55: ; preds = %52, %31
%56 = load i32, i32* @O_CREAT
%57 = load i32, i32* @O_TRUNC
%58 = or i32 %57, %56
%59 = tail call i64 @creat(i8* %8, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %55
%62 = load i32, i32* @stderr
%63 = load i32, i32* @errno
%64 = tail call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %63)
%65 = load i32, i32* @errno
%66 = add nsw i32 %65, 10
br label %67
67: ; preds = %55, %61, %48, %41, %33, %22, %18
%68 = phi i32 [ 3, %33 ], [ 4, %41 ], [ 5, %48 ], [ %66, %61 ], [ 2, %22 ], [ 1, %18 ], [ 0, %55 ]
ret i32 %68
}