#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 */
double RAND_MAX;
int first;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(char *, char, int);
double rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strcspn(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *getdir(char *path) {
  static char dir[100];
  int n, r, not_found = 1, pathlen;
  char *_begin;
  static first = 1;
  _begin = path;
  pathlen = strlen(path);
  memset(dir, '\0', 100);
  if (first)
    srand(getpid());
  first = 0;
  while (not_found) {
    r = (int)((double)strlen(path) * rand() / (RAND_MAX + 1.0));
    path += r;
    if (r != 0) {
      path += strcspn(path, ":");
      path++;
    }
    if (path - _begin >= pathlen) {
      path = _begin;
      continue;
    }
    not_found = 0;
    n = strcspn(path, ":");
    strcpy(dir, "");
    strncat(dir, path, n);
    strcat(dir, "");
  };
  return dir;
}


x86 assembly (clang, -O0):
<getdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
lea    0x2ec2(%rip),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
cmpl   $0x0,0x2e97(%rip)
je     11e5 <getdir+0x55>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
call   1090 <srand@plt>
movl   $0x0,0x2e79(%rip)
cmpl   $0x0,-0x14(%rbp)
je     1300 <getdir+0x170>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    $0x0,%al
call   10b0 <rand@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xdda(%rip),%xmm1
mov    0x2dbb(%rip),%rax
addsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1283 <getdir+0xf3>
mov    -0x8(%rbp),%rdi
lea    0xda6(%rip),%rsi
call   1080 <strcspn@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
movslq -0x18(%rbp),%rcx
cmp    %rcx,%rax
jl     12a8 <getdir+0x118>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ef <getdir+0x5f>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xd4e(%rip),%rsi
call   1080 <strcspn@plt>
mov    %eax,-0xc(%rbp)
lea    0x2db7(%rip),%rdi
lea    0xd39(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0x2d9d(%rip),%rdi
call   1070 <strncat@plt>
lea    0x2d91(%rip),%rdi
lea    0xd13(%rip),%rsi
call   10a0 <strcat@plt>
jmp    11ef <getdir+0x5f>
lea    0x2d79(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0x2ed3(%rip),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
cmpb   $0x0,0x2f24(%rip)
jne    11d0 <getdir+0x40>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1090 <srand@plt>
movb   $0x1,0x2f0d(%rip)
movslq %ebx,%rbp
mov    0x2e07(%rip),%r12
lea    0xe20(%rip),%r14
jmp    11fb <getdir+0x6b>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rax
sub    %r15,%rax
cmp    %rbp,%rax
jl     1253 <getdir+0xc3>
mov    %r15,%rdi
call   1050 <strlen@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   10b0 <rand@plt>
mulsd  0x8(%rsp),%xmm0
movsd  (%r12),%xmm1
addsd  0xdd5(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
cltq
lea    (%r15,%rax,1),%rbx
test   %eax,%eax
je     11f0 <getdir+0x60>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcspn@plt>
cltq
add    %rax,%rbx
add    $0x1,%rbx
jmp    11f0 <getdir+0x60>
lea    0xdae(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcspn@plt>
mov    %eax,%r14d
lea    0x2e14(%rip),%rbp
lea    0xd96(%rip),%r15
mov    %rbp,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1070 <strncat@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10a0 <strcat@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@getdir.dir = internal global [100 x i8] zeroinitializer
@getdir.first = internal global i32 1
@RAND_MAX = global double 0.000000e+00
@.str = [2 x i8] c":\00"
@.str.1 = [1 x i8] zeroinitializer
@first = global i32 0
define i8* @getdir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %2
store i32 1, i32* %5
%8 = load i8*, i8** %2
store i8* %8, i8** %7
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %6
%11 = call i32 @memset(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0), i8 signext 0, i32 100)
%12 = load i32, i32* @getdir.first
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 (...) @getpid()
%16 = call i32 @srand(i32 %15)
br label %17
17: ; preds = %14, %1
store i32 0, i32* @getdir.first
br label %18
18: ; preds = %56, %54, %17
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %64
21: ; preds = %18
%22 = load i8*, i8** %2
%23 = call i32 @strlen(i8* %22)
%24 = sitofp i32 %23 to double
%25 = call double (...) @rand()
%26 = fmul double %24, %25
%27 = load double, double* @RAND_MAX
%28 = fadd double %27, 1.000000e+00
%29 = fdiv double %26, %28
%30 = fptosi double %29 to i32
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = load i8*, i8** %2
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %2
%35 = load i32, i32* %4
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %21
%38 = load i8*, i8** %2
%39 = call i32 @strcspn(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%40 = load i8*, i8** %2
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %2
%43 = load i8*, i8** %2
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %2
br label %45
45: ; preds = %37, %21
%46 = load i8*, i8** %2
%47 = load i8*, i8** %7
%48 = ptrtoint i8* %46 to i64
%49 = ptrtoint i8* %47 to i64
%50 = sub i64 %48, %49
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = icmp sge i64 %50, %52
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = load i8*, i8** %7
store i8* %55, i8** %2
br label %18
56: ; preds = %45
store i32 0, i32* %5
%57 = load i8*, i8** %2
%58 = call i32 @strcspn(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %58, i32* %3
%59 = call i32 @strcpy(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%60 = load i8*, i8** %2
%61 = load i32, i32* %3
%62 = call i32 @strncat(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0), i8* %60, i32 %61)
%63 = call i32 @strcat(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %18
64: ; preds = %18
ret i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0)
}

LLVM IR (-O2):
@getdir.dir = internal global [100 x i8] zeroinitializer
@getdir.first = internal unnamed_addr global i1 false
@RAND_MAX = global double 0.000000e+00
@.str = [2 x i8] c":\00"
@.str.1 = [1 x i8] zeroinitializer
@first = global i32 0
define i8* @getdir(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i32 @memset(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0), i8 signext 0, i32 100)
%4 = load i1, i1* @getdir.first
br i1 %4, label %8, label %5
5: ; preds = %1
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 @srand(i32 %6)
br label %8
8: ; preds = %5, %1
store i1 true, i1* @getdir.first
%9 = ptrtoint i8* %0 to i64
%10 = sext i32 %2 to i64
br label %11
11: ; preds = %28, %8
%12 = tail call i32 @strlen(i8* %0)
%13 = sitofp i32 %12 to double
%14 = tail call double (...) @rand()
%15 = fmul double %14, %13
%16 = load double, double* @RAND_MAX
%17 = fadd double %16, 1.000000e+00
%18 = fdiv double %15, %17
%19 = fptosi double %18 to i32
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = icmp eq i32 %19, 0
br i1 %22, label %28, label %23
23: ; preds = %11
%24 = tail call i32 @strcspn(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %21, i64 1
%27 = getelementptr inbounds i8, i8* %26, i64 %25
br label %28
28: ; preds = %23, %11
%29 = phi i8* [ %27, %23 ], [ %21, %11 ]
%30 = ptrtoint i8* %29 to i64
%31 = sub i64 %30, %9
%32 = icmp slt i64 %31, %10
br i1 %32, label %33, label %11
33: ; preds = %28
%34 = tail call i32 @strcspn(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%35 = tail call i32 @strcpy(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%36 = tail call i32 @strncat(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0), i8* %29, i32 %34)
%37 = tail call i32 @strcat(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
ret i8* getelementptr inbounds ([100 x i8], [100 x i8]* @getdir.dir, i64 0, i64 0)
}