#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 */
int getpid();
int /*<<< orphan*/ sprintf(char *, char *, long);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
char *mktemp(char *template) {
  int n;
  long x;
  char *p;
  int c;
  struct stat stbuf;
  n = strlen(template);
  p = template + n - 6;
  if (n < 6 || strcmp(p, "XXXXXX") != 0) {
    *template = 0;
  } else {
    x = getpid() % 100000;
    sprintf(p, "%05d", x);
    p += 5;
    for (c = 'a'; c <= 'z'; c++) {
      *p = c;
      if (stat(template, &stbuf) < 0)
        return template;
    }
    *template = 0;
  }
  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    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
add    $0xfffffffffffffffa,%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x6,-0x14(%rbp)
jl     119f <mktemp+0x4f>
mov    -0x28(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11ab <mktemp+0x5b>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    123b <mktemp+0xeb>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x28(%rbp)
movl   $0x61,-0x2c(%rbp)
cmpl   $0x7a,-0x2c(%rbp)
jg     1234 <mktemp+0xe4>
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1221 <mktemp+0xd1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <mktemp+0xf3>
jmp    1226 <mktemp+0xd6>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e8 <mktemp+0x98>
mov    -0x10(%rbp),%rax
movb   $0x0,(%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   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1040 <strlen@plt>
cmp    $0x6,%eax
jl     11eb <mktemp+0x9b>
cltq
lea    (%r12,%rax,1),%r14
add    $0xfffffffffffffffa,%r14
lea    0xe86(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11eb <mktemp+0x9b>
xor    %eax,%eax
call   1030 <getpid@plt>
cltq
imul   $0x14f8b589,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x2d,%rcx
add    %edx,%ecx
imul   $0x186a0,%ecx,%ecx
sub    %ecx,%eax
movslq %eax,%rdx
lea    0xe51(%rip),%rsi
mov    %r14,%rdi
call   1070 <sprintf@plt>
mov    $0xffffffe6,%ebx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
lea    0x7b(%rbx),%eax
mov    %al,0x5(%r14)
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11f0 <mktemp+0xa0>
inc    %ebx
jne    11d0 <mktemp+0x80>
movb   $0x0,(%r12)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"XXXXXX\00"
@.str.1 = [5 x i8] c"%05d\00"
define i8* @mktemp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %4
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 -6
store i8* %15, i8** %6
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 6
br i1 %17, label %22, label %18
18: ; preds = %1
%19 = load i8*, i8** %6
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18, %1
%23 = load i8*, i8** %3
store i8 0, i8* %23
br label %51
24: ; preds = %18
%25 = call i32 (...) @getpid()
%26 = srem i32 %25, 100000
%27 = sext i32 %26 to i64
store i64 %27, i64* %5
%28 = load i8*, i8** %6
%29 = load i64, i64* %5
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %29)
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i64 5
store i8* %32, i8** %6
store i32 97, i32* %7
br label %33
33: ; preds = %46, %24
%34 = load i32, i32* %7
%35 = icmp sle i32 %34, 122
br i1 %35, label %36, label %49
36: ; preds = %33
%37 = load i32, i32* %7
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %6
store i8 %38, i8* %39
%40 = load i8*, i8** %3
%41 = call i64 @stat(i8* %40, %struct.stat* %8)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = load i8*, i8** %3
store i8* %44, i8** %2
br label %53
45: ; preds = %36
br label %46
46: ; preds = %45
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
br label %33
49: ; preds = %33
%50 = load i8*, i8** %3
store i8 0, i8* %50
br label %51
51: ; preds = %49, %22
%52 = load i8*, i8** %3
store i8* %52, i8** %2
br label %53
53: ; preds = %51, %43
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"XXXXXX\00"
@.str.1 = [5 x i8] c"%05d\00"
define i8* @mktemp(i8* returned %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = getelementptr inbounds i8, i8* %6, i64 -6
%8 = icmp slt i32 %4, 6
br i1 %8, label %26, label %9
9: ; preds = %1
%10 = tail call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %9
%13 = tail call i32 (...) @getpid()
%14 = srem i32 %13, 100000
%15 = sext i32 %14 to i64
%16 = tail call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = getelementptr inbounds i8, i8* %7, i64 5
br label %21
18: ; preds = %21
%19 = add nuw nsw i32 %22, 1
%20 = icmp eq i32 %19, 123
br i1 %20, label %26, label %21
21: ; preds = %12, %18
%22 = phi i32 [ 97, %12 ], [ %19, %18 ]
%23 = trunc i32 %22 to i8
store i8 %23, i8* %17
%24 = call i64 @stat(i8* %0, %struct.stat* %2)
%25 = icmp slt i64 %24, 0
br i1 %25, label %27, label %18
26: ; preds = %18, %1, %9
store i8 0, i8* %0
br label %27
27: ; preds = %21, %26
ret i8* %0
}