#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 /*<<< orphan*/ cwd;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mkdir(char *, int);
int open(char *, int);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int k_tree_gen(int lvl, int node_nr) {
  int cnt;
  char dir[8192], cwd[8192], leaf[8192];
  if (lvl <= 0)
    return 0;
  for (cnt = 0; cnt < node_nr; cnt++) {
    int fd = 0, fd2;
    sprintf(dir, "%d-d", cnt);
    sprintf(leaf, "%d-f", cnt);
    fd = open(leaf, O_CREAT | O_RDWR);
    fd2 = dup(fd);
    close(fd);
    fd = fd2;
    write(fd, leaf, 3);
    lseek(fd, SEEK_SET, 0);
    read(fd, leaf, 3);
    close(fd);
    mkdir(dir, 0777);
    getcwd(cwd, sizeof(cwd));
    chdir(dir);
    k_tree_gen(lvl - 1, node_nr);
    chdir(cwd);
  }
  return 1;
}


x86 assembly (clang, -O0):
<k_tree_gen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x6020,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     11d7 <k_tree_gen+0x27>
movl   $0x0,-0x4(%rbp)
jmp    1318 <k_tree_gen+0x168>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1311 <k_tree_gen+0x161>
movl   $0x0,-0x6014(%rbp)
lea    -0x2010(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xdfb(%rip),%rsi
call   10d0 <sprintf@plt>
lea    -0x6010(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xdea(%rip),%rsi
call   10d0 <sprintf@plt>
lea    -0x6010(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x6014(%rbp)
mov    -0x6014(%rbp),%edi
call   1060 <dup@plt>
mov    %eax,-0x6018(%rbp)
mov    -0x6014(%rbp),%edi
call   1090 <close@plt>
mov    -0x6018(%rbp),%eax
mov    %eax,-0x6014(%rbp)
mov    -0x6014(%rbp),%edi
lea    -0x6010(%rbp),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
mov    -0x6014(%rbp),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <lseek@plt>
mov    -0x6014(%rbp),%edi
lea    -0x6010(%rbp),%rsi
mov    $0x3,%edx
call   10a0 <read@plt>
mov    -0x6014(%rbp),%edi
call   1090 <close@plt>
lea    -0x2010(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
lea    -0x4010(%rbp),%rdi
mov    $0x2000,%esi
call   1080 <getcwd@plt>
lea    -0x2010(%rbp),%rdi
call   1050 <chdir@plt>
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
mov    -0xc(%rbp),%esi
call   10c0 <k_tree_gen@plt>
lea    -0x4010(%rbp),%rdi
call   1050 <chdir@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11de <k_tree_gen+0x2e>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x6020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<k_tree_gen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x6008,%rsp
test   %edi,%edi
jle    12cc <k_tree_gen+0x11c>
mov    %esi,%r14d
mov    $0x1,%eax
test   %esi,%esi
jle    12ce <k_tree_gen+0x11e>
mov    %edi,%ebp
add    $0xffffffff,%ebp
xor    %ebx,%ebx
mov    %rsp,%rdi
lea    0xe16(%rip),%rsi
mov    %ebx,%edx
call   10d0 <sprintf@plt>
lea    0x2000(%rsp),%r13
mov    %r13,%rdi
lea    0xe02(%rip),%rsi
mov    %ebx,%edx
call   10d0 <sprintf@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
or     (%rax),%esi
mov    %r13,%rdi
call   10b0 <open@plt>
mov    %r14d,%r12d
mov    %eax,%r14d
mov    %eax,%edi
call   1060 <dup@plt>
mov    %eax,%r15d
mov    %r14d,%edi
mov    %r12d,%r14d
call   1090 <close@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x3,%edx
call   1040 <write@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1070 <lseek@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x3,%edx
call   10a0 <read@plt>
mov    %r15d,%edi
call   1090 <close@plt>
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
lea    0x4000(%rsp),%r15
mov    %r15,%rdi
mov    $0x2000,%esi
call   1080 <getcwd@plt>
mov    %r12,%rdi
call   1050 <chdir@plt>
mov    %ebp,%edi
mov    %r14d,%esi
call   10c0 <k_tree_gen@plt>
mov    %r15,%rdi
call   1050 <chdir@plt>
add    $0x1,%ebx
cmp    %ebx,%r14d
jne    11e0 <k_tree_gen+0x30>
mov    $0x1,%eax
jmp    12ce <k_tree_gen+0x11e>
xor    %eax,%eax
add    $0x6008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%d-d\00"
@.str.1 = [5 x i8] c"%d-f\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
define i32 @k_tree_gen(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [8192 x i8]
%8 = alloca [8192 x i8]
%9 = alloca [8192 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%12 = load i32, i32* %4
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %64
15: ; preds = %2
store i32 0, i32* %6
br label %16
16: ; preds = %60, %15
%17 = load i32, i32* %6
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %63
20: ; preds = %16
store i32 0, i32* %10
%21 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%22 = load i32, i32* %6
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%25 = load i32, i32* %6
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%28 = load i32, i32* @O_CREAT
%29 = load i32, i32* @O_RDWR
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %27, i32 %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = call i32 @dup(i32 %32)
store i32 %33, i32* %11
%34 = load i32, i32* %10
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %11
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%39 = call i32 @write(i32 %37, i8* %38, i32 3)
%40 = load i32, i32* %10
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @lseek(i32 %40, i32 %41, i32 0)
%43 = load i32, i32* %10
%44 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 3)
%46 = load i32, i32* %10
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%49 = call i32 @mkdir(i8* %48, i32 511)
%50 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%51 = call i32 @getcwd(i8* %50, i32 8192)
%52 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%53 = call i32 @chdir(i8* %52)
%54 = load i32, i32* %4
%55 = sub nsw i32 %54, 1
%56 = load i32, i32* %5
%57 = call i32 @k_tree_gen(i32 %55, i32 %56)
%58 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%59 = call i32 @chdir(i8* %58)
br label %60
60: ; preds = %20
%61 = load i32, i32* %6
%62 = add nsw i32 %61, 1
store i32 %62, i32* %6
br label %16
63: ; preds = %16
store i32 1, i32* %3
br label %64
64: ; preds = %63, %14
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@.str = [5 x i8] c"%d-d\00"
@.str.1 = [5 x i8] c"%d-f\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
define i32 @k_tree_gen(i32 %0, i32 %1) {
%3 = alloca [8192 x i8]
%4 = alloca [8192 x i8]
%5 = alloca [8192 x i8]
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 1
br i1 %9, label %36, label %10
10: ; preds = %2
%11 = icmp sgt i32 %1, 0
br i1 %11, label %12, label %36
12: ; preds = %10
%13 = add nsw i32 %0, -1
br label %14
14: ; preds = %12, %14
%15 = phi i32 [ 0, %12 ], [ %34, %14 ]
%16 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_RDWR
%20 = or i32 %19, %18
%21 = call i32 @open(i8* %8, i32 %20)
%22 = call i32 @dup(i32 %21)
%23 = call i32 @close(i32 %21)
%24 = call i32 @write(i32 %22, i8* %8, i32 3)
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @lseek(i32 %22, i32 %25, i32 0)
%27 = call i32 @read(i32 %22, i8* %8, i32 3)
%28 = call i32 @close(i32 %22)
%29 = call i32 @mkdir(i8* %6, i32 511)
%30 = call i32 @getcwd(i8* %7, i32 8192)
%31 = call i32 @chdir(i8* %6)
%32 = call i32 @k_tree_gen(i32 %13, i32 %1)
%33 = call i32 @chdir(i8* %7)
%34 = add nuw nsw i32 %15, 1
%35 = icmp eq i32 %34, %1
br i1 %35, label %36, label %14
36: ; preds = %14, %10, %2
%37 = phi i32 [ 0, %2 ], [ 1, %10 ], [ 1, %14 ]
ret i32 %37
}