#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 /*<<< orphan*/ free(char *);
char *quickcat(char const *, char *);
int rand();
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strdup(char const *);
int strlen(char const *);
char *randdir(const char *root) {
  char *slashdir, name[16], *dir;
  if (!root)
    return NULL;
  if (strlen(root) > 0 && root[strlen(root) - 1] != '/')
    slashdir = quickcat(root, "/");
  else
    slashdir = strdup(root);
  sprintf(name, "dir%05d", rand());
  dir = quickcat(slashdir, name);
  free(slashdir);
  return dir;
}


x86 assembly (clang, -O0):
<randdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <randdir+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1229 <randdir+0xc9>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    11d9 <randdir+0x79>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11d9 <randdir+0x79>
mov    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1050 <quickcat@plt>
mov    %rax,-0x18(%rbp)
jmp    11e6 <randdir+0x86>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1080 <rand@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xe00(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <quickcat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randdir>:
push   %r14
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     1193 <randdir+0x33>
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %eax,%eax
jle    1189 <randdir+0x29>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    1197 <randdir+0x37>
mov    %rbx,%rdi
call   1070 <strdup@plt>
jmp    11a6 <randdir+0x46>
xor    %ebx,%ebx
jmp    11da <randdir+0x7a>
lea    0xe62(%rip),%rsi
mov    %rbx,%rdi
call   1050 <quickcat@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1080 <rand@plt>
lea    0xe4b(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <quickcat@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [8 x i8] c"dir%05d\00"
define i8* @randdir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [16 x i8]
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i8* null, i8** %2
br label %40
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %15, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 47
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = load i8*, i8** %3
%26 = call i8* @quickcat(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %26, i8** %4
br label %30
27: ; preds = %14, %10
%28 = load i8*, i8** %3
%29 = call i8* @strdup(i8* %28)
store i8* %29, i8** %4
br label %30
30: ; preds = %27, %24
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%32 = call i32 (...) @rand()
%33 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i8*, i8** %4
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%36 = call i8* @quickcat(i8* %34, i8* %35)
store i8* %36, i8** %6
%37 = load i8*, i8** %4
%38 = call i32 @free(i8* %37)
%39 = load i8*, i8** %6
store i8* %39, i8** %2
br label %40
40: ; preds = %30, %9
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [8 x i8] c"dir%05d\00"
define i8* @randdir(i8* %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %25, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %5
%9 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, -1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 47
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = tail call i8* @quickcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %19
17: ; preds = %8, %5
%18 = tail call i8* @strdup(i8* %0)
br label %19
19: ; preds = %17, %15
%20 = phi i8* [ %16, %15 ], [ %18, %17 ]
%21 = tail call i32 (...) @rand()
%22 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i8* @quickcat(i8* %20, i8* %3)
%24 = call i32 @free(i8* %20)
br label %25
25: ; preds = %1, %19
%26 = phi i8* [ %23, %19 ], [ null, %1 ]
ret i8* %26
}