char *_tmpnam(char *s, unsigned long int *u) {
  int n;
  do {
    if (*u >= 32767)
      return ((void *)0);
    n = tmppath(s);
    if (n == 0 || n > 255 - 12)
      return ((void *)0);
    s += n;
    sprintf(s, "t%4x.%lu", getpid(), (*u)++);
  } while (access(s, 0) == 0);
  return s;
}


x86 assembly (clang, -O0):
<_tmpnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x7fff,(%rax)
jb     116e <_tmpnam+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    1203 <_tmpnam+0xc3>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1060 <tmppath@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1193 <_tmpnam+0x53>
cmpl   $0xf3,-0x1c(%rbp)
jle    11a0 <_tmpnam+0x60>
movq   $0x0,-0x8(%rbp)
jmp    1203 <_tmpnam+0xc3>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,(%rax)
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1040 <access@plt>
cmp    $0x0,%eax
je     1150 <_tmpnam+0x10>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tmpnam>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeae(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpq   $0x7ffe,(%r14)
ja     11c3 <_tmpnam+0x83>
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <tmppath@plt>
test   %eax,%eax
je     11c3 <_tmpnam+0x83>
mov    %eax,%ecx
mov    $0x0,%eax
cmp    $0xf3,%ecx
jg     11c5 <_tmpnam+0x85>
movslq %ecx,%rax
add    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    (%r14),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%r14)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <access@plt>
mov    %eax,%ecx
mov    %rbx,%rax
test   %ecx,%ecx
je     1160 <_tmpnam+0x20>
jmp    11c5 <_tmpnam+0x85>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"t%4x.%lu\00"
define i8* @_tmpnam(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
br label %7
7: ; preds = %32, %2
%8 = load i64*, i64** %5
%9 = load i64, i64* %8
%10 = icmp uge i64 %9, 32767
br i1 %10, label %11, label %12
11: ; preds = %7
store i8* null, i8** %3
br label %38
12: ; preds = %7
%13 = load i8*, i8** %4
%14 = call i32 (i8*, ...) bitcast (i32 (...)* @tmppath to i32 (i8*, ...)*)(i8* %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 243
br i1 %19, label %20, label %21
20: ; preds = %17, %12
store i8* null, i8** %3
br label %38
21: ; preds = %17
%22 = load i32, i32* %6
%23 = load i8*, i8** %4
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = call i32 (...) @getpid()
%28 = load i64*, i64** %5
%29 = load i64, i64* %28
%30 = add i64 %29, 1
store i64 %30, i64* %28
%31 = call i32 (i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %27, i64 %29)
br label %32
32: ; preds = %21
%33 = load i8*, i8** %4
%34 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* %33, i32 0)
%35 = icmp eq i32 %34, 0
br i1 %35, label %7, label %36
36: ; preds = %32
%37 = load i8*, i8** %4
store i8* %37, i8** %3
br label %38
38: ; preds = %36, %20, %11
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@.str = [9 x i8] c"t%4x.%lu\00"
define i8* @_tmpnam(i8* %0, i64* nocapture %1) {
br label %3
3: ; preds = %12, %2
%4 = phi i8* [ %0, %2 ], [ %14, %12 ]
%5 = load i64, i64* %1
%6 = icmp ugt i64 %5, 32766
br i1 %6, label %21, label %7
7: ; preds = %3
%8 = tail call i32 (i8*, ...) bitcast (i32 (...)* @tmppath to i32 (i8*, ...)*)(i8* %4)
%9 = icmp eq i32 %8, 0
%10 = icmp sgt i32 %8, 243
%11 = or i1 %9, %10
br i1 %11, label %21, label %12
12: ; preds = %7
%13 = sext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %4, i64 %13
%15 = tail call i32 (...) @getpid()
%16 = load i64, i64* %1
%17 = add i64 %16, 1
store i64 %17, i64* %1
%18 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15, i64 %16)
%19 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* %14, i32 0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %3, label %21
21: ; preds = %12, %7, %3
%22 = phi i8* [ null, %3 ], [ null, %7 ], [ %14, %12 ]
ret i8* %22
}