#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;
scalar_t__ EINVAL;
scalar_t__ errno;
long getpid();
scalar_t__ mkdir(char *, int);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
scalar_t__ time(int /*<<< orphan*/ *);
char *mkdtemp(char *tmpl) {
  static const char LETTERS[] =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  static long value = 0;
  long v;
  int len;
  int i, j;
  len = strlen(tmpl);
  if (len < 6 || strcmp(&tmpl[len - 6], "XXXXXX") != 0) {
    errno = EINVAL;
    return NULL;
  }
  value += ((long)time(NULL)) ^ getpid();
  for (i = 0; i < 7; ++i, value += 7777) {
    for (j = 0, v = value; j < 6; ++j)
      tmpl[(len - 6) + j] = LETTERS[v % 62];
    v /= 62;
    if (mkdir(tmpl, 0700) == 0)
      return tmpl;
    else if (errno != EEXIST)
      return NULL;
  }
  errno = EEXIST;
  return NULL;
}


x86 assembly (clang, -O0):
<mkdtemp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x6,-0x1c(%rbp)
jl     1197 <mkdtemp+0x47>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%eax
sub    $0x6,%eax
cltq
add    %rax,%rdi
lea    0xeb7(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11b8 <mkdtemp+0x68>
mov    0x2e32(%rip),%rax
mov    (%rax),%rcx
mov    0x2e30(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12f0 <mkdtemp+0x1a0>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
xor    %rcx,%rax
add    0x2e73(%rip),%rax
mov    %rax,0x2e6c(%rip)
movl   $0x0,-0x20(%rbp)
cmpl   $0x7,-0x20(%rbp)
jge    12d4 <mkdtemp+0x184>
movl   $0x0,-0x24(%rbp)
mov    0x2e4d(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x6,-0x24(%rbp)
jge    124a <mkdtemp+0xfa>
mov    -0x18(%rbp),%rax
mov    $0x3e,%ecx
cqto
idiv   %rcx
lea    0xdda(%rip),%rax
mov    (%rax,%rdx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x6,%ecx
add    -0x24(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1207 <mkdtemp+0xb7>
mov    -0x18(%rbp),%rax
mov    $0x3e,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jne    1281 <mkdtemp+0x131>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f0 <mkdtemp+0x1a0>
mov    0x2d50(%rip),%rax
mov    (%rax),%rax
mov    0x2d4e(%rip),%rcx
cmp    (%rcx),%rax
je     12a8 <mkdtemp+0x158>
movq   $0x0,-0x8(%rbp)
jmp    12f0 <mkdtemp+0x1a0>
jmp    12ad <mkdtemp+0x15d>
jmp    12b2 <mkdtemp+0x162>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    0x2d8e(%rip),%rax
add    $0x1e61,%rax
mov    %rax,0x2d81(%rip)
jmp    11eb <mkdtemp+0x9b>
mov    0x2d05(%rip),%rax
mov    (%rax),%rcx
mov    0x2cf3(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdtemp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1050 <strlen@plt>
cmp    $0x6,%eax
jl     1184 <mkdtemp+0x34>
mov    %eax,%r14d
add    $0xfffffffa,%r14d
lea    (%r12,%r14,1),%rdi
lea    0xec5(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11ad <mkdtemp+0x5d>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
mov    %rax,(%rcx)
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1070 <time@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rcx
xor    %r15,%rcx
add    0x2e85(%rip),%rcx
mov    %rcx,0x2e7e(%rip)
mov    %r14d,%ebx
mov    $0x7,%ebp
lea    0xe1f(%rip),%r15
mov    0x2df0(%rip),%r13
mov    0x2df1(%rip),%r14
nop
mov    %rcx,%rax
movabs $0x8421084210842109,%rdx
08 21 84
imul   %rdx
add    %rcx,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x5,%rdx
add    %rax,%rdx
mov    %rdx,%rax
shl    $0x6,%rax
mov    %rdx,%rsi
sub    %rax,%rsi
add    %rdx,%rsi
add    %rcx,%rsi
movzbl (%r15,%rsi,1),%eax
mov    %al,(%r12,%rbx,1)
mov    %al,0x1(%r12,%rbx,1)
mov    %al,0x2(%r12,%rbx,1)
mov    %al,0x3(%r12,%rbx,1)
mov    %al,0x4(%r12,%rbx,1)
mov    %al,0x5(%r12,%rbx,1)
mov    %r12,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     119b <mkdtemp+0x4b>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
jne    1198 <mkdtemp+0x48>
mov    0x2de0(%rip),%rcx
mov    $0x1e61,%eax
add    %rax,%rcx
mov    %rcx,0x2dd1(%rip)
add    $0xffffffff,%ebp
jne    11f0 <mkdtemp+0xa0>
jmp    1198 <mkdtemp+0x48>

LLVM IR (-O0):
@mkdtemp.LETTERS = internal constant [63 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\00"
@mkdtemp.value = internal global i64 0
@.str = [7 x i8] c"XXXXXX\00"
@EINVAL = global i64 0
@errno = global i64 0
@EEXIST = global i64 0
define i8* @mkdtemp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 6
br i1 %11, label %20, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = load i32, i32* %5
%15 = sub nsw i32 %14, 6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12, %1
%21 = load i64, i64* @EINVAL
store i64 %21, i64* @errno
store i8* null, i8** %2
br label %73
22: ; preds = %12
%23 = call i64 @time(i32* null)
%24 = call i64 (...) @getpid()
%25 = xor i64 %23, %24
%26 = load i64, i64* @mkdtemp.value
%27 = add nsw i64 %26, %25
store i64 %27, i64* @mkdtemp.value
store i32 0, i32* %6
br label %28
28: ; preds = %66, %22
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 7
br i1 %30, label %31, label %71
31: ; preds = %28
store i32 0, i32* %7
%32 = load i64, i64* @mkdtemp.value
store i64 %32, i64* %4
br label %33
33: ; preds = %48, %31
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 6
br i1 %35, label %36, label %51
36: ; preds = %33
%37 = load i64, i64* %4
%38 = srem i64 %37, 62
%39 = getelementptr inbounds [63 x i8], [63 x i8]* @mkdtemp.LETTERS, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = load i8*, i8** %3
%42 = load i32, i32* %5
%43 = sub nsw i32 %42, 6
%44 = load i32, i32* %7
%45 = add nsw i32 %43, %44
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %41, i64 %46
store i8 %40, i8* %47
br label %48
48: ; preds = %36
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %33
51: ; preds = %33
%52 = load i64, i64* %4
%53 = sdiv i64 %52, 62
store i64 %53, i64* %4
%54 = load i8*, i8** %3
%55 = call i64 @mkdir(i8* %54, i32 448)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = load i8*, i8** %3
store i8* %58, i8** %2
br label %73
59: ; preds = %51
%60 = load i64, i64* @errno
%61 = load i64, i64* @EEXIST
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %64
63: ; preds = %59
store i8* null, i8** %2
br label %73
64: ; preds = %59
br label %65
65: ; preds = %64
br label %66
66: ; preds = %65
%67 = load i32, i32* %6
%68 = add nsw i32 %67, 1
store i32 %68, i32* %6
%69 = load i64, i64* @mkdtemp.value
%70 = add nsw i64 %69, 7777
store i64 %70, i64* @mkdtemp.value
br label %28
71: ; preds = %28
%72 = load i64, i64* @EEXIST
store i64 %72, i64* @errno
store i8* null, i8** %2
br label %73
73: ; preds = %71, %63, %57, %20
%74 = load i8*, i8** %2
ret i8* %74
}

LLVM IR (-O2):
@mkdtemp.LETTERS = internal unnamed_addr constant [63 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\00"
@mkdtemp.value = internal unnamed_addr global i64 0
@.str = [7 x i8] c"XXXXXX\00"
@EINVAL = global i64 0
@errno = global i64 0
@EEXIST = global i64 0
define i8* @mkdtemp(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 6
br i1 %3, label %10, label %4
4: ; preds = %1
%5 = add nsw i32 %2, -6
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = tail call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %4, %1
%11 = load i64, i64* @EINVAL
store i64 %11, i64* @errno
br label %47
12: ; preds = %4
%13 = tail call i64 @time(i32* null)
%14 = tail call i64 (...) @getpid()
%15 = xor i64 %14, %13
%16 = load i64, i64* @mkdtemp.value
%17 = add nsw i64 %16, %15
store i64 %17, i64* @mkdtemp.value
%18 = zext i32 %5 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = add nuw nsw i64 %18, 1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = add nuw nsw i64 %18, 2
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = add nuw nsw i64 %18, 3
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = add nuw nsw i64 %18, 4
%27 = getelementptr inbounds i8, i8* %0, i64 %26
%28 = add nuw nsw i64 %18, 5
%29 = getelementptr inbounds i8, i8* %0, i64 %28
br label %30
30: ; preds = %12, %42
%31 = phi i32 [ 0, %12 ], [ %43, %42 ]
%32 = phi i64 [ %17, %12 ], [ %45, %42 ]
%33 = srem i64 %32, 62
%34 = getelementptr inbounds [63 x i8], [63 x i8]* @mkdtemp.LETTERS, i64 0, i64 %33
%35 = load i8, i8* %34
store i8 %35, i8* %19
store i8 %35, i8* %21
store i8 %35, i8* %23
store i8 %35, i8* %25
store i8 %35, i8* %27
store i8 %35, i8* %29
%36 = tail call i64 @mkdir(i8* %0, i32 448)
%37 = icmp eq i64 %36, 0
br i1 %37, label %47, label %38
38: ; preds = %30
%39 = load i64, i64* @errno
%40 = load i64, i64* @EEXIST
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = add nuw nsw i32 %31, 1
%44 = load i64, i64* @mkdtemp.value
%45 = add nsw i64 %44, 7777
store i64 %45, i64* @mkdtemp.value
%46 = icmp eq i32 %43, 7
br i1 %46, label %47, label %30
47: ; preds = %38, %30, %42, %10
%48 = phi i8* [ null, %10 ], [ null, %42 ], [ null, %38 ], [ %0, %30 ]
ret i8* %48
}