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 = "/";
  }
  unsigned int r1 = (unsigned int)lrand48();
  unsigned int r2 = (unsigned int)lrand48();
  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    0x2eca(%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)
mov    $0x0,%al
call   1050 <lrand48@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <lrand48@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1221 <mktempname+0xe1>
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    0xdf1(%rip),%rsi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    1243 <mktempname+0x103>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
mov    -0x30(%rbp),%r9d
lea    0xdd6(%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   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
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    0x2ee2(%rip),%r12
cmovne %rbx,%r12
test   %eax,%eax
je     119b <mktempname+0x5b>
add    $0xffffffff,%eax
cltq
mov    (%r14,%rax,1),%al
cmp    $0x5c,%al
lea    0xe79(%rip),%rcx
lea    0xe71(%rip),%rbx
cmove  %rcx,%rbx
cmp    $0x2f,%al
cmove  %rcx,%rbx
jmp    11a2 <mktempname+0x62>
lea    0xe5f(%rip),%rbx
xor    %eax,%eax
call   1050 <lrand48@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <lrand48@plt>
test   %r15,%r15
je     11c4 <mktempname+0x84>
mov    %r15,(%rsp)
lea    0xe40(%rip),%rsi
jmp    11cb <mktempname+0x8b>
lea    0xe47(%rip),%rsi
mov    %r12,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %ebp,%r8d
mov    %eax,%r9d
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r12,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@mktempname.tmpbuf = internal global [160 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@.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 (...) @lrand48()
store i32 %42, i32* %8
%43 = call i32 (...) @lrand48()
store i32 %43, i32* %9
%44 = load i8*, i8** %4
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %54
46: ; preds = %41
%47 = load i8*, i8** %3
%48 = load i8*, i8** %5
%49 = load i8*, i8** %7
%50 = load i32, i32* %8
%51 = load i32, i32* %9
%52 = load i8*, i8** %4
%53 = call i32 (i8*, i8*, ...) @sprintf(i8* %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %48, i8* %49, i32 %50, i32 %51, i8* %52)
br label %61
54: ; preds = %41
%55 = load i8*, i8** %3
%56 = load i8*, i8** %5
%57 = load i8*, i8** %7
%58 = load i32, i32* %8
%59 = load i32, i32* %9
%60 = call i32 (i8*, i8*, ...) @sprintf(i8* %55, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %56, i8* %57, i32 %58, i32 %59)
br label %61
61: ; preds = %54, %46
%62 = load i8*, i8** %3
ret i8* %62
}

LLVM IR (-O2):
@mktempname.tmpbuf = internal global [160 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@.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 (...) @lrand48()
%24 = tail call i32 (...) @lrand48()
%25 = icmp eq i8* %1, null
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = 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 %24, i8* %1)
br label %30
28: ; preds = %21
%29 = 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 %24)
br label %30
30: ; preds = %28, %26
ret i8* %9
}