#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ EEXIST;
int R_OK;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int W_OK;
int X_OK;
scalar_t__ access(unsigned char const *, int);
scalar_t__ errno;
int /*<<< orphan*/ error(char *, unsigned char const *, ...);
int getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
scalar_t__ mkdir(unsigned char *, int);
int /*<<< orphan*/ snprintf(unsigned char *, size_t, char *,
                            unsigned char const *, unsigned char const *, int);
scalar_t__ stat(unsigned char const *, struct stat *);
char *strerror(scalar_t__);
int umask(int);
int create_arch_dir(unsigned char *buf, size_t bufsize,
                    const unsigned char *work_dir,
                    const unsigned char *prefix) {
  int serial = 0;
  struct timeval tv;
  int pid = getpid();
  enum { MAX_ATTEMPTS = 20 };
  struct stat stb;
  int seed, um;
  if (stat(work_dir, &stb) < 0) {
    error("working directory %s does not exist", work_dir);
    return -1;
  }
  if (!S_ISDIR(stb.st_mode)) {
    error("working directory %s is not a directory", work_dir);
    return -1;
  }
  if (access(work_dir, R_OK | W_OK | X_OK) < 0) {
    error("working directory %s has invalid permissions", work_dir);
    return -1;
  }
  while (serial < MAX_ATTEMPTS) {
    gettimeofday(&tv, 0);
    seed = ++serial ^ (tv.tv_sec & 0xffff) ^ ((tv.tv_sec >> 16) & 0xffff) ^
           (tv.tv_usec & 0xffff) ^ ((tv.tv_usec >> 16) & 0xffff) ^
           (pid & 0xffff) ^ ((pid >> 16) & 0xffff);
    snprintf(buf, bufsize, "%s/%s%d", work_dir, prefix, seed);
    um = umask(0);
    if (mkdir(buf, 0700) >= 0) {
      umask(um);
      break;
    }
    if (errno != EEXIST) {
      error("cannot create directory %s: %s", buf, strerror(errno));
      umask(um);
      return -1;
    }
    umask(um);
  }
  if (serial >= MAX_ATTEMPTS) {
    error("cannot create directory in %s: too many attempts", work_dir);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_arch_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    11fe <create_arch_dir+0x5e>
mov    -0x20(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   10a0 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cf <create_arch_dir+0x22f>
mov    -0x40(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    122d <create_arch_dir+0x8d>
mov    -0x20(%rbp),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   10a0 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cf <create_arch_dir+0x22f>
mov    -0x20(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2da7(%rip),%rax
or     (%rax),%esi
mov    0x2d96(%rip),%rax
or     (%rax),%esi
call   10b0 <access@plt>
cmp    $0x0,%rax
jge    1279 <create_arch_dir+0xd9>
mov    -0x20(%rbp),%rsi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   10a0 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cf <create_arch_dir+0x22f>
jmp    127e <create_arch_dir+0xde>
cmpl   $0x14,-0x2c(%rbp)
jge    13a0 <create_arch_dir+0x200>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%ecx
and    $0xffff,%ecx
xor    %ecx,%eax
mov    -0x38(%rbp),%ecx
sar    $0x10,%ecx
and    $0xffff,%ecx
xor    %ecx,%eax
mov    -0x34(%rbp),%ecx
and    $0xffff,%ecx
xor    %ecx,%eax
mov    -0x34(%rbp),%ecx
sar    $0x10,%ecx
and    $0xffff,%ecx
xor    %ecx,%eax
mov    -0x3c(%rbp),%ecx
and    $0xffff,%ecx
xor    %ecx,%eax
mov    -0x3c(%rbp),%ecx
sar    $0x10,%ecx
and    $0xffff,%ecx
xor    %ecx,%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x44(%rbp),%r9d
lea    0xd74(%rip),%rdx
call   1060 <snprintf@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jl     1339 <create_arch_dir+0x199>
mov    -0x48(%rbp),%edi
call   1080 <umask@plt>
jmp    13a0 <create_arch_dir+0x200>
mov    0x2c88(%rip),%rax
mov    (%rax),%rax
mov    0x2c86(%rip),%rcx
cmp    (%rcx),%rax
je     1393 <create_arch_dir+0x1f3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c66(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   10a0 <error@plt>
mov    -0x48(%rbp),%edi
call   1080 <umask@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cf <create_arch_dir+0x22f>
mov    -0x48(%rbp),%edi
call   1080 <umask@plt>
jmp    127e <create_arch_dir+0xde>
cmpl   $0x14,-0x2c(%rbp)
jl     13c8 <create_arch_dir+0x228>
mov    -0x20(%rbp),%rsi
lea    0xceb(%rip),%rdi
mov    $0x0,%al
call   10a0 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cf <create_arch_dir+0x22f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_arch_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
mov    %rdx,%r14
mov    %rsi,0x18(%rsp)
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1090 <stat@plt>
test   %rax,%rax
js     12ca <create_arch_dir+0x12a>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     12d3 <create_arch_dir+0x133>
mov    0x2dcb(%rip),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
call   10b0 <access@plt>
test   %rax,%rax
js     12dc <create_arch_dir+0x13c>
movzwl %bp,%eax
shr    $0x10,%ebp
xor    %eax,%ebp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
lea    0x1(%r15),%r12d
mov    0x10(%rsp),%eax
mov    0x14(%rsp),%ecx
movzwl %ax,%edx
shr    $0x10,%eax
movzwl %cx,%esi
xor    %eax,%esi
shr    $0x10,%ecx
mov    %ebp,%r9d
xor    %r12d,%r9d
xor    %edx,%r9d
xor    %esi,%r9d
xor    %ecx,%r9d
mov    %rbx,%rdi
mov    0x18(%rsp),%rsi
lea    0xe05(%rip),%rdx
mov    %r14,%rcx
mov    0x20(%rsp),%r8
call   1060 <snprintf@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
jns    12e5 <create_arch_dir+0x145>
mov    0x2d24(%rip),%rax
mov    (%rax),%rdi
mov    0x2d22(%rip),%rax
cmp    (%rax),%rdi
jne    131a <create_arch_dir+0x17a>
mov    %r13d,%edi
call   1080 <umask@plt>
mov    %r12d,%r15d
cmp    $0x14,%r12d
jne    1230 <create_arch_dir+0x90>
jmp    12f5 <create_arch_dir+0x155>
lea    0xd2f(%rip),%rdi
jmp    12fc <create_arch_dir+0x15c>
lea    0xd4a(%rip),%rdi
jmp    12fc <create_arch_dir+0x15c>
lea    0xd69(%rip),%rdi
jmp    12fc <create_arch_dir+0x15c>
mov    %r13d,%edi
call   1080 <umask@plt>
xor    %eax,%eax
cmp    $0x13,%r15d
jb     130b <create_arch_dir+0x16b>
lea    0xda4(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10a0 <error@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
call   10c0 <strerror@plt>
lea    0xd5b(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <error@plt>
mov    %r13d,%edi
call   1080 <umask@plt>
jmp    1306 <create_arch_dir+0x166>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.stat = type { i32 }
@.str = [36 x i8] c"working directory %s does not exist\00"
@.str.1 = [40 x i8] c"working directory %s is not a directory\00"
@R_OK = global i32 0
@W_OK = global i32 0
@X_OK = global i32 0
@.str.2 = [45 x i8] c"working directory %s has invalid permissions\00"
@.str.3 = [8 x i8] c"%s/%s%d\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.4 = [31 x i8] c"cannot create directory %s: %s\00"
@.str.5 = [49 x i8] c"cannot create directory in %s: too many attempts\00"
define i32 @create_arch_dir(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca %struct.stat
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 0, i32* %10
%16 = call i32 (...) @getpid()
store i32 %16, i32* %12
%17 = load i8*, i8** %8
%18 = call i64 @stat(i8* %17, %struct.stat* %13)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %4
%21 = load i8*, i8** %8
%22 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %5
br label %110
23: ; preds = %4
%24 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @S_ISDIR(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i8*, i8** %8
%30 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 -1, i32* %5
br label %110
31: ; preds = %23
%32 = load i8*, i8** %8
%33 = load i32, i32* @R_OK
%34 = load i32, i32* @W_OK
%35 = or i32 %33, %34
%36 = load i32, i32* @X_OK
%37 = or i32 %35, %36
%38 = call i64 @access(i8* %32, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = load i8*, i8** %8
%42 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %41)
store i32 -1, i32* %5
br label %110
43: ; preds = %31
br label %44
44: ; preds = %100, %43
%45 = load i32, i32* %10
%46 = icmp slt i32 %45, 20
br i1 %46, label %47, label %103
47: ; preds = %44
%48 = call i32 @gettimeofday(%struct.timeval* %11, i32 0)
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 1
store i32 %50, i32* %10
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%52 = load i32, i32* %51
%53 = and i32 %52, 65535
%54 = xor i32 %50, %53
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%56 = load i32, i32* %55
%57 = ashr i32 %56, 16
%58 = and i32 %57, 65535
%59 = xor i32 %54, %58
%60 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%61 = load i32, i32* %60
%62 = and i32 %61, 65535
%63 = xor i32 %59, %62
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%65 = load i32, i32* %64
%66 = ashr i32 %65, 16
%67 = and i32 %66, 65535
%68 = xor i32 %63, %67
%69 = load i32, i32* %12
%70 = and i32 %69, 65535
%71 = xor i32 %68, %70
%72 = load i32, i32* %12
%73 = ashr i32 %72, 16
%74 = and i32 %73, 65535
%75 = xor i32 %71, %74
store i32 %75, i32* %14
%76 = load i8*, i8** %6
%77 = load i64, i64* %7
%78 = load i8*, i8** %8
%79 = load i8*, i8** %9
%80 = load i32, i32* %14
%81 = call i32 @snprintf(i8* %76, i64 %77, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %78, i8* %79, i32 %80)
%82 = call i32 @umask(i32 0)
store i32 %82, i32* %15
%83 = load i8*, i8** %6
%84 = call i64 @mkdir(i8* %83, i32 448)
%85 = icmp sge i64 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %47
%87 = load i32, i32* %15
%88 = call i32 @umask(i32 %87)
br label %103
89: ; preds = %47
%90 = load i64, i64* @errno
%91 = load i64, i64* @EEXIST
%92 = icmp ne i64 %90, %91
br i1 %92, label %93, label %100
93: ; preds = %89
%94 = load i8*, i8** %6
%95 = load i64, i64* @errno
%96 = call i8* @strerror(i64 %95)
%97 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %94, i8* %96)
%98 = load i32, i32* %15
%99 = call i32 @umask(i32 %98)
store i32 -1, i32* %5
br label %110
100: ; preds = %89
%101 = load i32, i32* %15
%102 = call i32 @umask(i32 %101)
br label %44
103: ; preds = %86, %44
%104 = load i32, i32* %10
%105 = icmp sge i32 %104, 20
br i1 %105, label %106, label %109
106: ; preds = %103
%107 = load i8*, i8** %8
%108 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i8* %107)
store i32 -1, i32* %5
br label %110
109: ; preds = %103
store i32 0, i32* %5
br label %110
110: ; preds = %109, %106, %93, %40, %28, %20
%111 = load i32, i32* %5
ret i32 %111
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.stat = type { i32 }
@.str = [36 x i8] c"working directory %s does not exist\00"
@.str.1 = [40 x i8] c"working directory %s is not a directory\00"
@R_OK = global i32 0
@W_OK = global i32 0
@X_OK = global i32 0
@.str.2 = [45 x i8] c"working directory %s has invalid permissions\00"
@.str.3 = [8 x i8] c"%s/%s%d\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.4 = [31 x i8] c"cannot create directory %s: %s\00"
@.str.5 = [49 x i8] c"cannot create directory in %s: too many attempts\00"
define i32 @create_arch_dir(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca %struct.timeval
%6 = alloca %struct.stat
%7 = bitcast %struct.timeval* %5 to i8*
%8 = tail call i32 (...) @getpid()
%9 = bitcast %struct.stat* %6 to i8*
%10 = call i64 @stat(i8* %2, %struct.stat* %6)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %72
14: ; preds = %4
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @S_ISDIR(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %72
21: ; preds = %14
%22 = load i32, i32* @R_OK
%23 = load i32, i32* @W_OK
%24 = or i32 %23, %22
%25 = load i32, i32* @X_OK
%26 = or i32 %24, %25
%27 = call i64 @access(i8* %2, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %21
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%32 = and i32 %8, 65535
%33 = lshr i32 %8, 16
%34 = xor i32 %33, %32
br label %37
35: ; preds = %21
%36 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %72
37: ; preds = %29, %64
%38 = phi i32 [ 0, %29 ], [ %40, %64 ]
%39 = call i32 @gettimeofday(%struct.timeval* %5, i32 0)
%40 = add nuw nsw i32 %38, 1
%41 = load i32, i32* %30
%42 = and i32 %41, 65535
%43 = lshr i32 %41, 16
%44 = load i32, i32* %31
%45 = and i32 %44, 65535
%46 = lshr i32 %44, 16
%47 = xor i32 %34, %40
%48 = xor i32 %47, %42
%49 = xor i32 %48, %43
%50 = xor i32 %49, %45
%51 = xor i32 %50, %46
%52 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %2, i8* %3, i32 %51)
%53 = call i32 @umask(i32 0)
%54 = call i64 @mkdir(i8* %0, i32 448)
%55 = icmp sgt i64 %54, -1
br i1 %55, label %67, label %56
56: ; preds = %37
%57 = load i64, i64* @errno
%58 = load i64, i64* @EEXIST
%59 = icmp eq i64 %57, %58
br i1 %59, label %64, label %60
60: ; preds = %56
%61 = call i8* @strerror(i64 %57)
%62 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %61)
%63 = call i32 @umask(i32 %53)
br label %72
64: ; preds = %56
%65 = call i32 @umask(i32 %53)
%66 = icmp eq i32 %40, 20
br i1 %66, label %70, label %37
67: ; preds = %37
%68 = call i32 @umask(i32 %53)
%69 = icmp ugt i32 %38, 18
br i1 %69, label %70, label %72
70: ; preds = %64, %67
%71 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i8* %2)
br label %72
72: ; preds = %67, %70, %60, %35, %19, %12
%73 = phi i32 [ -1, %12 ], [ -1, %35 ], [ -1, %70 ], [ -1, %60 ], [ -1, %19 ], [ 0, %67 ]
ret i32 %73
}