#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 */
scalar_t__ EEXIST;
int atoi(char *);
int chdir(char *);
int /*<<< orphan*/ close(int);
int creat(char *, int);
scalar_t__ errno;
int /*<<< orphan*/ gen_random_filename(char *);
int mkdir(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  char filename[256];
  char *dirpath = argv[1];
  int ret = 0, i = 0, cnt = 0;
  if (argc < 3) {
    printf("Usage: %s dirpath fcount\n", argv[0]);
    return -1;
  }
  cnt = atoi(argv[2]);
  ret = mkdir(dirpath, 0777);
  if (ret && errno != EEXIST) {
    printf("mkdir %s failed: %m\n", dirpath);
    return -1;
  }
  ret = chdir(dirpath);
  if (ret) {
    printf("chdir %s failed: %m\n", dirpath);
    return -1;
  }
  srand(time(NULL));
  for (i = 1; i <= cnt; i++) {
    gen_random_filename(filename);
    ret = creat(filename, 0777);
    if (ret < 0)
      printf("creat %s failed: %m\n", filename);
    else
      close(ret);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x118(%rbp)
movl   $0x0,-0x11c(%rbp)
movl   $0x0,-0x120(%rbp)
movl   $0x0,-0x124(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ff <main+0x6f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134c <main+0x1bc>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x124(%rbp)
mov    -0x118(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0x0,-0x11c(%rbp)
je     126f <main+0xdf>
mov    0x2d9b(%rip),%rax
mov    (%rax),%rax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
je     126f <main+0xdf>
mov    -0x118(%rbp),%rsi
lea    0xdbc(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134c <main+0x1bc>
mov    -0x118(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0x0,-0x11c(%rbp)
je     12ad <main+0x11d>
mov    -0x118(%rbp),%rsi
lea    0xd93(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134c <main+0x1bc>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
movl   $0x1,-0x120(%rbp)
mov    -0x120(%rbp),%eax
cmp    -0x124(%rbp),%eax
jg     1345 <main+0x1b5>
lea    -0x110(%rbp),%rdi
call   1090 <gen_random_filename@plt>
lea    -0x110(%rbp),%rdi
mov    $0x1ff,%esi
call   10b0 <creat@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0x0,-0x11c(%rbp)
jge    1321 <main+0x191>
lea    -0x110(%rbp),%rsi
lea    0xd2d(%rip),%rdi
call   1050 <printf@plt>
jmp    132c <main+0x19c>
mov    -0x11c(%rbp),%edi
call   1060 <close@plt>
jmp    1331 <main+0x1a1>
mov    -0x120(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x120(%rbp)
jmp    12c7 <main+0x137>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
cmp    $0x2,%edi
jg     11af <main+0x1f>
mov    (%rsi),%rsi
lea    0xe53(%rip),%rdi
jmp    1204 <main+0x74>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %eax,%eax
je     11e5 <main+0x55>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    11fa <main+0x6a>
mov    %r14,%rdi
call   1040 <chdir@plt>
test   %eax,%eax
je     1221 <main+0x91>
lea    0xe37(%rip),%rdi
jmp    1201 <main+0x71>
lea    0xe19(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %r14d,%r14d
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
test   %ebx,%ebx
jle    120f <main+0x7f>
mov    %rsp,%r15
lea    0xe04(%rip),%r12
jmp    125c <main+0xcc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%edi
call   1060 <close@plt>
add    $0xffffffff,%ebx
je     120f <main+0x7f>
mov    %r15,%rdi
call   1090 <gen_random_filename@plt>
mov    %r15,%rdi
mov    $0x1ff,%esi
call   10b0 <creat@plt>
test   %eax,%eax
jns    1250 <main+0xc0>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
add    $0xffffffff,%ebx
jne    125c <main+0xcc>
jmp    120f <main+0x7f>

LLVM IR (-O0):
@.str = [26 x i8] c"Usage: %s dirpath fcount\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [21 x i8] c"mkdir %s failed: %m\0A\00"
@.str.2 = [21 x i8] c"chdir %s failed: %m\0A\00"
@.str.3 = [21 x i8] c"creat %s failed: %m\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
store i8* %13, i8** %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 -1, i32* %3
br label %70
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %10
%26 = load i8*, i8** %7
%27 = call i32 @mkdir(i8* %26, i32 511)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %21
%31 = load i64, i64* @errno
%32 = load i64, i64* @EEXIST
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i8*, i8** %7
%36 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %35)
store i32 -1, i32* %3
br label %70
37: ; preds = %30, %21
%38 = load i8*, i8** %7
%39 = call i32 @chdir(i8* %38)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i8*, i8** %7
%44 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %43)
store i32 -1, i32* %3
br label %70
45: ; preds = %37
%46 = call i32 @time(i32* null)
%47 = call i32 @srand(i32 %46)
store i32 1, i32* %9
br label %48
48: ; preds = %66, %45
%49 = load i32, i32* %9
%50 = load i32, i32* %10
%51 = icmp sle i32 %49, %50
br i1 %51, label %52, label %69
52: ; preds = %48
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%54 = call i32 @gen_random_filename(i8* %53)
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%56 = call i32 @creat(i8* %55, i32 511)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%61 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %60)
br label %65
62: ; preds = %52
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %59
br label %66
66: ; preds = %65
%67 = load i32, i32* %9
%68 = add nsw i32 %67, 1
store i32 %68, i32* %9
br label %48
69: ; preds = %48
store i32 0, i32* %3
br label %70
70: ; preds = %69, %42, %34, %16
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str = [26 x i8] c"Usage: %s dirpath fcount\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [21 x i8] c"mkdir %s failed: %m\0A\00"
@.str.2 = [21 x i8] c"chdir %s failed: %m\0A\00"
@.str.3 = [21 x i8] c"creat %s failed: %m\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %44
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = tail call i32 @mkdir(i8* %6, i32 511)
%16 = icmp eq i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EEXIST
%20 = icmp eq i64 %18, %19
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %44
23: ; preds = %17, %11
%24 = tail call i32 @chdir(i8* %6)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %44
28: ; preds = %23
%29 = tail call i32 @time(i32* null)
%30 = tail call i32 @srand(i32 %29)
%31 = icmp slt i32 %14, 1
br i1 %31, label %44, label %32
32: ; preds = %28, %41
%33 = phi i32 [ %42, %41 ], [ 1, %28 ]
%34 = call i32 @gen_random_filename(i8* %4)
%35 = call i32 @creat(i8* %4, i32 511)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %41
39: ; preds = %32
%40 = call i32 @close(i32 %35)
br label %41
41: ; preds = %37, %39
%42 = add nuw i32 %33, 1
%43 = icmp eq i32 %33, %14
br i1 %43, label %44, label %32
44: ; preds = %41, %28, %26, %21, %8
%45 = phi i32 [ -1, %8 ], [ -1, %21 ], [ -1, %26 ], [ 0, %28 ], [ 0, %41 ]
ret i32 %45
}