char *mktempname(char *ptr, const char *ext) {
  static char tmpbuf[160];
  char *dir = getTempDir();
  int l = strlen(dir);
  char *sep = "";
  if (!ptr)
    ptr = tmpbuf;
  if (l && dir[l - 1] != '/' && dir[l - 1] != '\\') {
    sep = "/";
  }
  static int count = 1;
  unsigned int r1 = time(0);
  unsigned int r2 = (unsigned int)tmpbuf << 8 ^ count;
  count++;
  if (ext) {
    sprintf(ptr, "%s%s%04x%04x.%s", dir, sep, r1, r2, ext);
  } else {
    sprintf(ptr, "%s%s%04x%04x", dir, sep, r1, r2);
  }
  return ptr;
}


x86 assembly (clang, -O0):
<mktempname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getTempDir@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    118a <mktempname+0x4a>
lea    0x2eda(%rip),%rax
mov    %rax,-0x8(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11d3 <mktempname+0x93>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11d3 <mktempname+0x93>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5c,%eax
je     11d3 <mktempname+0x93>
lea    0xe31(%rip),%rax
mov    %rax,-0x28(%rbp)
xor    %edi,%edi
mov    $0x0,%al
call   1050 <time@plt>
mov    %eax,-0x2c(%rbp)
lea    0x2e7a(%rip),%rax
shl    $0x8,%eax
xor    0x2e51(%rip),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e48(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e3f(%rip)
cmpq   $0x0,-0x10(%rbp)
je     123b <mktempname+0xfb>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
mov    -0x30(%rbp),%r9d
mov    -0x10(%rbp),%rax
lea    0xdd7(%rip),%rsi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    125d <mktempname+0x11d>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
mov    -0x30(%rbp),%r9d
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mktempname>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getTempDir@plt>
movslq %eax,%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %rbx,%rbx
lea    0x2ef6(%rip),%r12
cmovne %rbx,%r12
test   %eax,%eax
je     1197 <mktempname+0x57>
add    $0xffffffff,%eax
cltq
mov    (%r14,%rax,1),%al
cmp    $0x5c,%al
lea    0xe7d(%rip),%rcx
lea    0xe75(%rip),%rbx
cmove  %rcx,%rbx
cmp    $0x2f,%al
cmove  %rcx,%rbx
jmp    119e <mktempname+0x5e>
lea    0xe63(%rip),%rbx
xor    %edi,%edi
xor    %eax,%eax
call   1050 <time@plt>
mov    0x2e93(%rip),%ecx
lea    0x2eac(%rip),%r9
shl    $0x8,%r9d
xor    %ecx,%r9d
add    $0x1,%ecx
mov    %ecx,0x2e7c(%rip)
test   %r15,%r15
je     11d6 <mktempname+0x96>
mov    %r15,(%rsp)
lea    0xe2e(%rip),%rsi
jmp    11dd <mktempname+0x9d>
lea    0xe35(%rip),%rsi
mov    %r12,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@mktempname.tmpbuf = internal global [160 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@mktempname.count = internal global i32 1
@.str.2 = [16 x i8] c"%s%s%04x%04x.%s\00"
@.str.3 = [13 x i8] c"%s%s%04x%04x\00"
define i8* @mktempname(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = call i32 (...) @getTempDir()
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
%15 = trunc i64 %14 to i32
store i32 %15, i32* %6
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %7
%16 = load i8*, i8** %3
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %2
store i8* getelementptr inbounds ([160 x i8], [160 x i8]* @mktempname.tmpbuf, i64 0, i64 0), i8** %3
br label %19
19: ; preds = %18, %2
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %41
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 47
br i1 %30, label %31, label %41
31: ; preds = %22
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 92
br i1 %39, label %40, label %41
40: ; preds = %31
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %41
41: ; preds = %40, %31, %22, %19
%42 = call i32 (i32, ...) bitcast (i32 (...)* @time to i32 (i32, ...)*)(i32 0)
store i32 %42, i32* %8
%43 = load i32, i32* @mktempname.count
%44 = xor i32 shl (i32 ptrtoint ([160 x i8]* @mktempname.tmpbuf to i32), i32 8), %43
store i32 %44, i32* %9
%45 = load i32, i32* @mktempname.count
%46 = add nsw i32 %45, 1
store i32 %46, i32* @mktempname.count
%47 = load i8*, i8** %4
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %57
49: ; preds = %41
%50 = load i8*, i8** %3
%51 = load i8*, i8** %5
%52 = load i8*, i8** %7
%53 = load i32, i32* %8
%54 = load i32, i32* %9
%55 = load i8*, i8** %4
%56 = call i32 (i8*, i8*, ...) @sprintf(i8* %50, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %51, i8* %52, i32 %53, i32 %54, i8* %55)
br label %64
57: ; preds = %41
%58 = load i8*, i8** %3
%59 = load i8*, i8** %5
%60 = load i8*, i8** %7
%61 = load i32, i32* %8
%62 = load i32, i32* %9
%63 = call i32 (i8*, i8*, ...) @sprintf(i8* %58, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %59, i8* %60, i32 %61, i32 %62)
br label %64
64: ; preds = %57, %49
%65 = load i8*, i8** %3
ret i8* %65
}

LLVM IR (-O2):
@mktempname.tmpbuf = internal global [160 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@mktempname.count = internal unnamed_addr global i32 1
@.str.2 = [16 x i8] c"%s%s%04x%04x.%s\00"
@.str.3 = [13 x i8] c"%s%s%04x%04x\00"
define i8* @mktempname(i8* writeonly %0, i8* %1) {
%3 = tail call i32 (...) @getTempDir()
%4 = sext i32 %3 to i64
%5 = inttoptr i64 %4 to i8*
%6 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%7 = trunc i64 %6 to i32
%8 = icmp eq i8* %0, null
%9 = select i1 %8, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @mktempname.tmpbuf, i64 0, i64 0), i8* %0
%10 = icmp eq i32 %7, 0
br i1 %10, label %21, label %11
11: ; preds = %2
%12 = shl i64 %6, 32
%13 = add i64 %12, -4294967296
%14 = ashr exact i64 %13, 32
%15 = getelementptr inbounds i8, i8* %5, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 47
%18 = icmp eq i8 %16, 92
%19 = or i1 %17, %18
%20 = select i1 %19, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
br label %21
21: ; preds = %11, %2
%22 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %2 ], [ %20, %11 ]
%23 = tail call i32 (i32, ...) bitcast (i32 (...)* @time to i32 (i32, ...)*)(i32 0)
%24 = load i32, i32* @mktempname.count
%25 = xor i32 %24, shl (i32 ptrtoint ([160 x i8]* @mktempname.tmpbuf to i32), i32 8)
%26 = add nsw i32 %24, 1
store i32 %26, i32* @mktempname.count
%27 = icmp eq i8* %1, null
br i1 %27, label %30, label %28
28: ; preds = %21
%29 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %22, i32 %23, i32 %25, i8* %1)
br label %32
30: ; preds = %21
%31 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %22, i32 %23, i32 %25)
br label %32
32: ; preds = %30, %28
ret i8* %9
}