#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 timespec {
  uintptr_t tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ EEXIST;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned long);
scalar_t__ strcmp(char *, char *);
size_t strlen(char *);
char *__mktemp(char *template) {
  struct timespec ts;
  size_t l = strlen(template);
  int retries = 10000;
  unsigned long r;
  if (l < 6 || strcmp(template + l - 6, "XXXXXX")) {
    errno = EINVAL;
    return 0;
  }
  clock_gettime(CLOCK_REALTIME, &ts);
  r = ts.tv_nsec + (uintptr_t)&ts / 16 + (uintptr_t) template;
  while (retries--) {
    snprintf(template + l - 6, 7, "%06lX", r & 0xffffff);
    if (access(template, F_OK) < 0)
      return template;
    r = r * 1103515245 + 12345;
  }
  *template = 0;
  errno = EEXIST;
  return template;
}


x86 assembly (clang, -O0):
<__mktemp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x2710,-0x24(%rbp)
cmpq   $0x6,-0x20(%rbp)
jb     119d <__mktemp+0x4d>
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0xfffffffffffffffa,%rdi
lea    0xe72(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11bc <__mktemp+0x6c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2b(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1285 <__mktemp+0x135>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%rax
lea    -0x18(%rbp),%rcx
shr    $0x4,%rcx
add    %rcx,%rax
mov    -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
cmp    $0x0,%eax
je     1264 <__mktemp+0x114>
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0xfffffffffffffffa,%rdi
mov    -0x30(%rbp),%rcx
and    $0xffffff,%rcx
mov    $0x7,%esi
lea    0xde8(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jge    124d <__mktemp+0xfd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1285 <__mktemp+0x135>
imul   $0x41c64e6d,-0x30(%rbp),%rax
41
add    $0x3039,%rax
mov    %rax,-0x30(%rbp)
jmp    11e8 <__mktemp+0x98>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2d6e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d5d(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mktemp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    0x2e66(%rip),%rbx
cmp    $0x6,%rax
jb     118c <__mktemp+0x3c>
lea    (%r15,%rax,1),%r14
add    $0xfffffffffffffffa,%r14
lea    0xe81(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11ac <__mktemp+0x5c>
xor    %r15d,%r15d
mov    (%rbx),%eax
mov    0x2e40(%rip),%rcx
mov    %eax,(%rcx)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
shr    $0x4,%rbx
add    %r15,%rbx
add    (%rsp),%rbx
mov    $0xffffd8f0,%ebp
lea    0xe30(%rip),%r12
mov    0x2dda(%rip),%r13
xchg   %ax,%ax
mov    %ebx,%ecx
and    $0xffffff,%ecx
mov    %r14,%rdi
mov    $0x7,%esi
mov    %r12,%rdx
call   1050 <snprintf@plt>
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1070 <access@plt>
test   %rax,%rax
js     119a <__mktemp+0x4a>
imul   $0x41c64e6d,%rbx,%rbx
add    $0x3039,%rbx
inc    %ebp
jne    11e0 <__mktemp+0x90>
movb   $0x0,(%r15)
mov    0x2dba(%rip),%rbx
jmp    118f <__mktemp+0x3f>

LLVM IR (-O0):
%struct.timespec = type { i64 }
@.str = [7 x i8] c"XXXXXX\00"
@EINVAL = global i32 0
@errno = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.1 = [6 x i8] c"%06lX\00"
@F_OK = global i32 0
@EEXIST = global i32 0
define i8* @__mktemp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.timespec
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %5
store i32 10000, i32* %6
%10 = load i64, i64* %5
%11 = icmp ult i64 %10, 6
br i1 %11, label %19, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = load i64, i64* %5
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = getelementptr inbounds i8, i8* %15, i64 -6
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12, %1
%20 = load i32, i32* @EINVAL
store i32 %20, i32* @errno
store i8* null, i8** %2
br label %58
21: ; preds = %12
%22 = load i32, i32* @CLOCK_REALTIME
%23 = call i32 @clock_gettime(i32 %22, %struct.timespec* %4)
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%25 = load i64, i64* %24
%26 = ptrtoint %struct.timespec* %4 to i64
%27 = udiv i64 %26, 16
%28 = add i64 %25, %27
%29 = load i8*, i8** %3
%30 = ptrtoint i8* %29 to i64
%31 = add i64 %28, %30
store i64 %31, i64* %7
br label %32
32: ; preds = %50, %21
%33 = load i32, i32* %6
%34 = add nsw i32 %33, -1
store i32 %34, i32* %6
%35 = icmp ne i32 %33, 0
br i1 %35, label %36, label %54
36: ; preds = %32
%37 = load i8*, i8** %3
%38 = load i64, i64* %5
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 -6
%41 = load i64, i64* %7
%42 = and i64 %41, 16777215
%43 = call i32 @snprintf(i8* %40, i32 7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %42)
%44 = load i8*, i8** %3
%45 = load i32, i32* @F_OK
%46 = call i64 @access(i8* %44, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %36
%49 = load i8*, i8** %3
store i8* %49, i8** %2
br label %58
50: ; preds = %36
%51 = load i64, i64* %7
%52 = mul i64 %51, 1103515245
%53 = add i64 %52, 12345
store i64 %53, i64* %7
br label %32
54: ; preds = %32
%55 = load i8*, i8** %3
store i8 0, i8* %55
%56 = load i32, i32* @EEXIST
store i32 %56, i32* @errno
%57 = load i8*, i8** %3
store i8* %57, i8** %2
br label %58
58: ; preds = %54, %48, %19
%59 = load i8*, i8** %2
ret i8* %59
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
@.str = [7 x i8] c"XXXXXX\00"
@EINVAL = global i32 0
@errno = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.1 = [6 x i8] c"%06lX\00"
@F_OK = global i32 0
@EEXIST = global i32 0
define i8* @__mktemp(i8* %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = icmp ult i64 %4, 6
br i1 %5, label %35, label %6
6: ; preds = %1
%7 = getelementptr inbounds i8, i8* %0, i64 %4
%8 = getelementptr inbounds i8, i8* %7, i64 -6
%9 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %35
11: ; preds = %6
%12 = load i32, i32* @CLOCK_REALTIME
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %2)
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%15 = load i64, i64* %14
%16 = ptrtoint %struct.timespec* %2 to i64
%17 = lshr i64 %16, 4
%18 = ptrtoint i8* %0 to i64
%19 = add i64 %17, %18
%20 = add i64 %19, %15
br label %21
21: ; preds = %11, %29
%22 = phi i32 [ 9999, %11 ], [ %32, %29 ]
%23 = phi i64 [ %20, %11 ], [ %31, %29 ]
%24 = and i64 %23, 16777215
%25 = call i32 @snprintf(i8* %8, i32 7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %24)
%26 = load i32, i32* @F_OK
%27 = call i64 @access(i8* %0, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %39, label %29
29: ; preds = %21
%30 = mul i64 %23, 1103515245
%31 = add i64 %30, 12345
%32 = add nsw i32 %22, -1
%33 = icmp eq i32 %22, 0
br i1 %33, label %34, label %21
34: ; preds = %29
store i8 0, i8* %0
br label %35
35: ; preds = %1, %6, %34
%36 = phi i32* [ @EEXIST, %34 ], [ @EINVAL, %6 ], [ @EINVAL, %1 ]
%37 = phi i8* [ %0, %34 ], [ null, %6 ], [ null, %1 ]
%38 = load i32, i32* %36
store i32 %38, i32* @errno
br label %39
39: ; preds = %21, %35
%40 = phi i8* [ %37, %35 ], [ %0, %21 ]
ret i8* %40
}