#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__ ENOENT;
int O_CREAT;
int O_EXCL;
int O_RDWR;
scalar_t__ RAND_MAX;
scalar_t__ errno;
int open(char *, int, int);
float rand();
int strlen(char const *);
int mkstemp(char *template) {
  static const char alphanumerics[] =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  register char *a, *b;
  int fd, count, alphanumericsLen = strlen(alphanumerics);
  a = template + strlen(template);
  while (a > template && *(a - 1) == 'X') {
    a--;
  }
  if (a == template) {
    errno = ENOENT;
    return -1;
  }
  count = 10;
  do {
    for (b = a; *b; b++) {
      float r = rand() / ((float)RAND_MAX);
      *b = alphanumerics[(int)(r * alphanumericsLen)];
    }
    fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
  } while (fd == -1 && errno == EEXIST && --count > 0);
  return fd;
}


x86 assembly (clang, -O0):
<mkstemp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xebd(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x39(%rbp)
jbe    1190 <mkstemp+0x60>
mov    -0x18(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x58,%eax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11a0 <mkstemp+0x70>
jmp    11b1 <mkstemp+0x81>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    116c <mkstemp+0x3c>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11df <mkstemp+0xaf>
mov    0x2de2(%rip),%rax
mov    (%rax),%rcx
mov    0x2de8(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <mkstemp+0x19a>
movl   $0xa,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     124d <mkstemp+0x11d>
mov    $0x0,%al
call   1050 <rand@plt>
mov    0x2dcf(%rip),%rax
cvtsi2ssq (%rax),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x30(%rbp)
movss  -0x30(%rbp),%xmm0
cvtsi2ssl -0x2c(%rbp),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
movslq %eax,%rcx
lea    0xdcd(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11ee <mkstemp+0xbe>
mov    -0x10(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
or     (%rax),%esi
mov    0x2d56(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1040 <open@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x24(%rbp)
mov    %al,-0x3a(%rbp)
jne    12b9 <mkstemp+0x189>
mov    0x2d29(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2d2d(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x3a(%rbp)
jne    12b9 <mkstemp+0x189>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x3a(%rbp)
mov    -0x3a(%rbp),%al
test   $0x1,%al
jne    11e6 <mkstemp+0xb6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkstemp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0xeb8(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
movslq %eax,%rcx
lea    (%r14,%rcx,1),%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rdx,%rbp
mov    %rcx,%rax
cmp    %r14,%rdx
jbe    1189 <mkstemp+0x59>
lea    -0x1(%rbp),%rdx
lea    -0x1(%rax),%rcx
cmpb   $0x58,-0x1(%rbp)
je     1170 <mkstemp+0x40>
test   %rax,%rax
je     1261 <mkstemp+0x131>
cvtsi2ss %r15d,%xmm0
lea    0x1(%rbp),%rax
mov    %rax,0x10(%rsp)
mov    $0xa,%r13d
mov    0x2e2b(%rip),%r12
movss  %xmm0,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x0,0x0(%rbp)
je     1206 <mkstemp+0xd6>
mov    0x10(%rsp),%r15
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2ssq (%r12),%xmm1
divss  %xmm1,%xmm0
movss  0xc(%rsp),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
cltq
movzbl (%rax,%rbx,1),%eax
mov    %al,-0x1(%r15)
cmpb   $0x0,(%r15)
lea    0x1(%r15),%r15
jne    11d0 <mkstemp+0xa0>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
or     (%rax),%esi
mov    0x2da1(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x180,%edx
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    127a <mkstemp+0x14a>
mov    0x2d7e(%rip),%rax
mov    (%rax),%rcx
mov    $0xffffffff,%eax
mov    0x2d7f(%rip),%rdx
cmp    (%rdx),%rcx
jne    127a <mkstemp+0x14a>
lea    -0x1(%r13),%ecx
cmp    $0x1,%r13d
mov    %ecx,%r13d
ja     11c0 <mkstemp+0x90>
jmp    127a <mkstemp+0x14a>
mov    0x2d40(%rip),%rax
mov    (%rax),%rax
mov    0x2d46(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@mkstemp.alphanumerics = internal constant [63 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\00"
@ENOENT = global i64 0
@errno = global i64 0
@RAND_MAX = global i64 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@EEXIST = global i64 0
define i32 @mkstemp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca float
store i8* %0, i8** %3
%10 = call i32 @strlen(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @mkstemp.alphanumerics, i64 0, i64 0))
store i32 %10, i32* %8
%11 = load i8*, i8** %3
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
store i8* %15, i8** %4
br label %16
16: ; preds = %28, %1
%17 = load i8*, i8** %4
%18 = load i8*, i8** %3
%19 = icmp ugt i8* %17, %18
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 -1
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 88
br label %26
26: ; preds = %20, %16
%27 = phi i1 [ false, %16 ], [ %25, %20 ]
br i1 %27, label %28, label %31
28: ; preds = %26
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %4
br label %16
31: ; preds = %26
%32 = load i8*, i8** %4
%33 = load i8*, i8** %3
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i64, i64* @ENOENT
store i64 %36, i64* @errno
store i32 -1, i32* %2
br label %84
37: ; preds = %31
store i32 10, i32* %7
br label %38
38: ; preds = %80, %37
%39 = load i8*, i8** %4
store i8* %39, i8** %5
br label %40
40: ; preds = %58, %38
%41 = load i8*, i8** %5
%42 = load i8, i8* %41
%43 = icmp ne i8 %42, 0
br i1 %43, label %44, label %61
44: ; preds = %40
%45 = call float (...) @rand()
%46 = load i64, i64* @RAND_MAX
%47 = sitofp i64 %46 to float
%48 = fdiv float %45, %47
store float %48, float* %9
%49 = load float, float* %9
%50 = load i32, i32* %8
%51 = sitofp i32 %50 to float
%52 = fmul float %49, %51
%53 = fptosi float %52 to i32
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [63 x i8], [63 x i8]* @mkstemp.alphanumerics, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = load i8*, i8** %5
store i8 %56, i8* %57
br label %58
58: ; preds = %44
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %5
br label %40
61: ; preds = %40
%62 = load i8*, i8** %3
%63 = load i32, i32* @O_RDWR
%64 = load i32, i32* @O_CREAT
%65 = or i32 %63, %64
%66 = load i32, i32* @O_EXCL
%67 = or i32 %65, %66
%68 = call i32 @open(i8* %62, i32 %67, i32 384)
store i32 %68, i32* %6
br label %69
69: ; preds = %61
%70 = load i32, i32* %6
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %80
72: ; preds = %69
%73 = load i64, i64* @errno
%74 = load i64, i64* @EEXIST
%75 = icmp eq i64 %73, %74
br i1 %75, label %76, label %80
76: ; preds = %72
%77 = load i32, i32* %7
%78 = add nsw i32 %77, -1
store i32 %78, i32* %7
%79 = icmp sgt i32 %78, 0
br label %80
80: ; preds = %76, %72, %69
%81 = phi i1 [ false, %72 ], [ false, %69 ], [ %79, %76 ]
br i1 %81, label %38, label %82
82: ; preds = %80
%83 = load i32, i32* %6
store i32 %83, i32* %2
br label %84
84: ; preds = %82, %35
%85 = load i32, i32* %2
ret i32 %85
}

LLVM IR (-O2):
@mkstemp.alphanumerics = internal constant [63 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\00"
@ENOENT = global i64 0
@errno = global i64 0
@RAND_MAX = global i64 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@EEXIST = global i64 0
define i32 @mkstemp(i8* %0) {
%2 = tail call i32 @strlen(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @mkstemp.alphanumerics, i64 0, i64 0))
%3 = tail call i32 @strlen(i8* %0)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
br label %6
6: ; preds = %9, %1
%7 = phi i8* [ %5, %1 ], [ %10, %9 ]
%8 = icmp ugt i8* %7, %0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = getelementptr inbounds i8, i8* %7, i64 -1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 88
br i1 %12, label %6, label %13
13: ; preds = %6, %9
%14 = icmp eq i8* %7, %0
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = sitofp i32 %2 to float
br label %19
17: ; preds = %13
%18 = load i64, i64* @ENOENT
store i64 %18, i64* @errno
br label %52
19: ; preds = %15, %45
%20 = phi i32 [ %49, %45 ], [ 10, %15 ]
%21 = load i8, i8* %7
%22 = icmp eq i8 %21, 0
br i1 %22, label %37, label %23
23: ; preds = %19, %23
%24 = phi i8* [ %34, %23 ], [ %7, %19 ]
%25 = tail call float (...) @rand()
%26 = load i64, i64* @RAND_MAX
%27 = sitofp i64 %26 to float
%28 = fdiv float %25, %27
%29 = fmul float %28, %16
%30 = fptosi float %29 to i32
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [63 x i8], [63 x i8]* @mkstemp.alphanumerics, i64 0, i64 %31
%33 = load i8, i8* %32
store i8 %33, i8* %24
%34 = getelementptr inbounds i8, i8* %24, i64 1
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 0
br i1 %36, label %37, label %23
37: ; preds = %23, %19
%38 = load i32, i32* @O_RDWR
%39 = load i32, i32* @O_CREAT
%40 = or i32 %39, %38
%41 = load i32, i32* @O_EXCL
%42 = or i32 %40, %41
%43 = tail call i32 @open(i8* %0, i32 %42, i32 384)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %52
45: ; preds = %37
%46 = load i64, i64* @errno
%47 = load i64, i64* @EEXIST
%48 = icmp eq i64 %46, %47
%49 = add nsw i32 %20, -1
%50 = icmp ugt i32 %20, 1
%51 = select i1 %48, i1 %50, i1 false
br i1 %51, label %19, label %52
52: ; preds = %45, %37, %17
%53 = phi i32 [ -1, %17 ], [ -1, %45 ], [ %43, %37 ]
ret i32 %53
}