#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;
int /*<<< orphan*/ __randname(char *);
scalar_t__ errno;
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ mkdir(char *, int);
size_t strlen(char *);
char *mkdtemp(char *template) {
  size_t l = strlen(template);
  int retries = 100;
  if (l < 6 || memcmp(template + l - 6, "XXXXXX", 6)) {
    errno = EINVAL;
    return 0;
  }
  do {
    __randname(template + l - 6);
    if (!mkdir(template, 0700))
      return template;
  } while (--retries && errno == EEXIST);
  memcpy(template + l - 6, "XXXXXX", 6);
  return 0;
}


x86 assembly (clang, -O0):
<mkdtemp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x64,-0x1c(%rbp)
cmpq   $0x6,-0x18(%rbp)
jb     11a2 <mkdtemp+0x52>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0xfffffffffffffffa,%rdi
lea    0xe72(%rip),%rsi
mov    $0x6,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     11c3 <mkdtemp+0x73>
mov    0x2e27(%rip),%rax
mov    (%rax),%rcx
mov    0x2e25(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1263 <mkdtemp+0x113>
jmp    11c8 <mkdtemp+0x78>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0xfffffffffffffffa,%rdi
call   1070 <__randname@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%eax
jne    11fd <mkdtemp+0xad>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1263 <mkdtemp+0x113>
jmp    1202 <mkdtemp+0xb2>
mov    -0x1c(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
je     1233 <mkdtemp+0xe3>
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11c8 <mkdtemp+0x78>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0xfffffffffffffffa,%rdi
lea    0xdaf(%rip),%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdtemp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x6,%rax
jb     1187 <mkdtemp+0x37>
lea    (%rbx,%rax,1),%r14
add    $0xfffffffffffffffa,%r14
lea    0xe8b(%rip),%rsi
mov    %r14,%rdi
mov    $0x6,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     11a9 <mkdtemp+0x59>
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
mov    %rax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffff9d,%ebp
mov    0x2e23(%rip),%r15
mov    0x2e24(%rip),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
call   1070 <__randname@plt>
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
test   %eax,%eax
je     119d <mkdtemp+0x4d>
test   %ebp,%ebp
je     11e9 <mkdtemp+0x99>
add    $0x1,%ebp
mov    (%r12),%rax
cmp    %rax,(%r15)
je     11c0 <mkdtemp+0x70>
lea    0xe10(%rip),%rsi
mov    %r14,%rdi
mov    $0x6,%edx
call   1060 <memcpy@plt>
jmp    119b <mkdtemp+0x4b>

LLVM IR (-O0):
@.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
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %4
store i32 100, i32* %5
%8 = load i64, i64* %4
%9 = icmp ult i64 %8, 6
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i64, i64* %4
%13 = getelementptr inbounds i8, i8* %11, i64 %12
%14 = getelementptr inbounds i8, i8* %13, i64 -6
%15 = call i64 @memcmp(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10, %1
%18 = load i64, i64* @EINVAL
store i64 %18, i64* @errno
store i8* null, i8** %2
br label %48
19: ; preds = %10
br label %20
20: ; preds = %40, %19
%21 = load i8*, i8** %3
%22 = load i64, i64* %4
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = getelementptr inbounds i8, i8* %23, i64 -6
%25 = call i32 @__randname(i8* %24)
%26 = load i8*, i8** %3
%27 = call i32 @mkdir(i8* %26, i32 448)
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %20
%30 = load i8*, i8** %3
store i8* %30, i8** %2
br label %48
31: ; preds = %20
br label %32
32: ; preds = %31
%33 = load i32, i32* %5
%34 = add nsw i32 %33, -1
store i32 %34, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = load i64, i64* @EEXIST
%39 = icmp eq i64 %37, %38
br label %40
40: ; preds = %36, %32
%41 = phi i1 [ false, %32 ], [ %39, %36 ]
br i1 %41, label %20, label %42
42: ; preds = %40
%43 = load i8*, i8** %3
%44 = load i64, i64* %4
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = getelementptr inbounds i8, i8* %45, i64 -6
%47 = call i32 @memcpy(i8* %46, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
store i8* null, i8** %2
br label %48
48: ; preds = %42, %29, %17
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
@.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 i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp ult i64 %2, 6
br i1 %3, label %9, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 %2
%6 = getelementptr inbounds i8, i8* %5, i64 -6
%7 = tail call i64 @memcmp(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %4, %1
%10 = load i64, i64* @EINVAL
store i64 %10, i64* @errno
br label %25
11: ; preds = %4, %16
%12 = phi i32 [ %17, %16 ], [ 100, %4 ]
%13 = tail call i32 @__randname(i8* %6)
%14 = tail call i32 @mkdir(i8* %0, i32 448)
%15 = icmp eq i32 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %11
%17 = add nsw i32 %12, -1
%18 = icmp ne i32 %17, 0
%19 = load i64, i64* @errno
%20 = load i64, i64* @EEXIST
%21 = icmp eq i64 %19, %20
%22 = select i1 %18, i1 %21, i1 false
br i1 %22, label %11, label %23
23: ; preds = %16
%24 = tail call i32 @memcpy(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
br label %25
25: ; preds = %11, %23, %9
%26 = phi i8* [ null, %9 ], [ null, %23 ], [ %0, %11 ]
ret i8* %26
}