#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 stat {
  int dummy;
};

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ EINVAL;
scalar_t__ ENOENT;
int /*<<< orphan*/ __randname(char *);
scalar_t__ errno;
scalar_t__ memcmp(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
size_t strlen(char *);
char *mktemp(char *template) {
  size_t l = strlen(template);
  int retries = 100;
  struct stat st;
  if (l < 6 || memcmp(template + l - 6, "XXXXXX", 6)) {
    errno = EINVAL;
    *template = 0;
    return template;
  }
  do {
    __randname(template + l - 6);
    if (stat(template, &st)) {
      if (errno != ENOENT)
        *template = 0;
      return template;
    }
  } while (--retries);
  *template = 0;
  errno = EEXIST;
  return template;
}


x86 assembly (clang, -O0):
<mktemp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x64,-0x1c(%rbp)
cmpq   $0x6,-0x18(%rbp)
jb     1192 <mktemp+0x52>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0xfffffffffffffffa,%rdi
lea    0xe82(%rip),%rsi
mov    $0x6,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     11ba <mktemp+0x7a>
mov    0x2e37(%rip),%rax
mov    (%rax),%rcx
mov    0x2e35(%rip),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124f <mktemp+0x10f>
jmp    11bf <mktemp+0x7f>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0xfffffffffffffffa,%rdi
call   1060 <__randname@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1215 <mktemp+0xd5>
mov    0x2dea(%rip),%rax
mov    (%rax),%rax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
je     1208 <mktemp+0xc8>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124f <mktemp+0x10f>
jmp    121a <mktemp+0xda>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11bf <mktemp+0x7f>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2da6(%rip),%rax
mov    (%rax),%rcx
mov    0x2d94(%rip),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mktemp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0x6,%rax
jb     1176 <mktemp+0x36>
lea    (%rbx,%rax,1),%r14
add    $0xfffffffffffffffa,%r14
lea    0xe9c(%rip),%rsi
mov    %r14,%rdi
mov    $0x6,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     119b <mktemp+0x5b>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
mov    %rax,(%rcx)
movb   $0x0,(%rbx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffff9c,%ebp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1060 <__randname@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    11e5 <mktemp+0xa5>
inc    %ebp
jne    11b0 <mktemp+0x70>
movb   $0x0,(%rbx)
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    0x2df8(%rip),%rcx
mov    %rax,(%rcx)
jmp    118d <mktemp+0x4d>
mov    0x2dec(%rip),%rax
mov    (%rax),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <mktemp+0x4a>
jmp    118d <mktemp+0x4d>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"XXXXXX\00"
@EINVAL = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
@EEXIST = global i64 0
define i8* @mktemp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %4
store i32 100, i32* %5
%9 = load i64, i64* %4
%10 = icmp ult i64 %9, 6
br i1 %10, label %18, label %11
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i64, i64* %4
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 -6
%16 = call i64 @memcmp(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %11, %1
%19 = load i64, i64* @EINVAL
store i64 %19, i64* @errno
%20 = load i8*, i8** %3
store i8 0, i8* %20
%21 = load i8*, i8** %3
store i8* %21, i8** %2
br label %49
22: ; preds = %11
br label %23
23: ; preds = %41, %22
%24 = load i8*, i8** %3
%25 = load i64, i64* %4
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = getelementptr inbounds i8, i8* %26, i64 -6
%28 = call i32 @__randname(i8* %27)
%29 = load i8*, i8** %3
%30 = call i64 @stat(i8* %29, %struct.stat* %6)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %23
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOENT
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i8*, i8** %3
store i8 0, i8* %37
br label %38
38: ; preds = %36, %32
%39 = load i8*, i8** %3
store i8* %39, i8** %2
br label %49
40: ; preds = %23
br label %41
41: ; preds = %40
%42 = load i32, i32* %5
%43 = add nsw i32 %42, -1
store i32 %43, i32* %5
%44 = icmp ne i32 %43, 0
br i1 %44, label %23, label %45
45: ; preds = %41
%46 = load i8*, i8** %3
store i8 0, i8* %46
%47 = load i64, i64* @EEXIST
store i64 %47, i64* @errno
%48 = load i8*, i8** %3
store i8* %48, i8** %2
br label %49
49: ; preds = %45, %38, %18
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"XXXXXX\00"
@EINVAL = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
@EEXIST = global i64 0
define i8* @mktemp(i8* returned %0) {
%2 = alloca %struct.stat
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = bitcast %struct.stat* %2 to i8*
%5 = icmp ult i64 %3, 6
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = getelementptr inbounds i8, i8* %0, i64 %3
%8 = getelementptr inbounds i8, i8* %7, i64 -6
%9 = tail call i64 @memcmp(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %6, %1
%12 = load i64, i64* @EINVAL
store i64 %12, i64* @errno
store i8 0, i8* %0
br label %28
13: ; preds = %6, %23
%14 = phi i32 [ %24, %23 ], [ 100, %6 ]
%15 = call i32 @__randname(i8* %8)
%16 = call i64 @stat(i8* %0, %struct.stat* %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOENT
%21 = icmp eq i64 %19, %20
br i1 %21, label %28, label %22
22: ; preds = %18
store i8 0, i8* %0
br label %28
23: ; preds = %13
%24 = add nsw i32 %14, -1
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %13
26: ; preds = %23
store i8 0, i8* %0
%27 = load i64, i64* @EEXIST
store i64 %27, i64* @errno
br label %28
28: ; preds = %18, %22, %26, %11
ret i8* %0
}