#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 timespec {
  unsigned int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int F_OK;
char *P_tmpdir;
int R_OK;
int W_OK;
int X_OK;
int a_fetch_add(int *, int);
scalar_t__ access(char const *, int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ free(char *);
int getpid();
char *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char const *,
                            int, int, int);
int strlen(char const *);
char *tempnam(const char *dir, const char *pfx) {
  static int index;
  char *s;
  struct timespec ts;
  int pid = getpid();
  size_t l;
  int n;
  int try = 0;
  if (!dir)
    dir = P_tmpdir;
  if (!pfx)
    pfx = "temp";
  if (access(dir, R_OK | W_OK | X_OK) != 0)
    return NULL;
  l = strlen(dir) + 1 + strlen(pfx) + 3 * (sizeof(int) * 3 + 2) + 1;
  s = malloc(l);
  if (!s)
    return s;
  do {
    clock_gettime(CLOCK_REALTIME, &ts);
    n = ts.tv_nsec ^ (unsigned)&s ^ (unsigned)s;
    snprintf(s, l, "%s/%s-%d-%d-%x", dir, pfx, pid, a_fetch_add(&index, 1), n);
  } while (!access(s, F_OK) && try++ < 100);
  if (try >= 100) {
    free(s);
    return 0;
  }
  return s;
}


x86 assembly (clang, -O0):
<tempnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bd <tempnam+0x3d>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d3 <tempnam+0x53>
lea    0xe31(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%rax
je     120e <tempnam+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    1359 <tempnam+0x1d9>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
cltq
add    $0x2a,%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1260 <tempnam+0xe0>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1359 <tempnam+0x1d9>
jmp    1265 <tempnam+0xe5>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x28(%rbp),%eax
lea    -0x20(%rbp),%rcx
xor    %ecx,%eax
mov    -0x20(%rbp),%rcx
xor    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
lea    0x2db2(%rip),%rdi
mov    $0x1,%esi
call   1080 <a_fetch_add@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rcx
mov    -0x50(%rbp),%r8
mov    -0x48(%rbp),%r9d
mov    %eax,%r10d
mov    -0x3c(%rbp),%eax
lea    0xd24(%rip),%rdx
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
call   1070 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x69(%rbp)
jne    1326 <tempnam+0x1a6>
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x40(%rbp)
cmp    $0x64,%eax
setl   %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    1265 <tempnam+0xe5>
cmpl   $0x64,-0x40(%rbp)
jl     1351 <tempnam+0x1d1>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1359 <tempnam+0x1d9>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tempnam>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x14(%rsp)
test   %r15,%r15
jne    11b0 <tempnam+0x30>
mov    0x2e3b(%rip),%rax
mov    (%rax),%r15
test   %rbx,%rbx
lea    0xe46(%rip),%r13
cmovne %rbx,%r13
mov    0x2e03(%rip),%rax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
call   10a0 <access@plt>
test   %rax,%rax
jne    12b6 <tempnam+0x136>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1060 <strlen@plt>
add    %ebx,%eax
add    $0x1,%eax
movslq %eax,%r12
add    $0x2b,%r12
mov    %r12,%rdi
call   1090 <malloc@plt>
mov    %rax,0x8(%rsp)
xor    %ebp,%ebp
test   %rax,%rax
je     12b6 <tempnam+0x136>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%eax
lea    0x8(%rsp),%rcx
xor    %ecx,%eax
mov    0x8(%rsp),%r14
mov    %r14d,%ebx
xor    %eax,%ebx
lea    0x2e19(%rip),%rdi
mov    $0x1,%esi
call   1080 <a_fetch_add@plt>
mov    %r14,%rdi
mov    %r12,%rsi
lea    0xd9f(%rip),%rdx
mov    %r15,%rcx
mov    %r13,%r8
mov    0x14(%rsp),%r9d
push   %rbx
push   %rax
call   1070 <snprintf@plt>
add    $0x10,%rsp
mov    0x8(%rsp),%rdi
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
test   %rax,%rax
jne    129e <tempnam+0x11e>
add    $0x1,%ebp
cmp    $0x65,%ebp
jne    1220 <tempnam+0xa0>
jmp    12a3 <tempnam+0x123>
cmp    $0x64,%ebp
jb     12b1 <tempnam+0x131>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    12b6 <tempnam+0x136>
mov    0x8(%rsp),%rbp
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@tempnam.index = internal global i32 0
@P_tmpdir = global i8* null
@.str = [5 x i8] c"temp\00"
@R_OK = global i32 0
@W_OK = global i32 0
@X_OK = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.1 = [15 x i8] c"%s/%s-%d-%d-%x\00"
@F_OK = global i32 0
define i8* @tempnam(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.timespec
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = call i32 (...) @getpid()
store i32 %12, i32* %8
store i32 0, i32* %11
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = load i8*, i8** @P_tmpdir
store i8* %16, i8** %4
br label %17
17: ; preds = %15, %2
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %17
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %5
br label %21
21: ; preds = %20, %17
%22 = load i8*, i8** %4
%23 = load i32, i32* @R_OK
%24 = load i32, i32* @W_OK
%25 = or i32 %23, %24
%26 = load i32, i32* @X_OK
%27 = or i32 %25, %26
%28 = call i64 @access(i8* %22, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %21
store i8* null, i8** %3
br label %85
31: ; preds = %21
%32 = load i8*, i8** %4
%33 = call i32 @strlen(i8* %32)
%34 = add nsw i32 %33, 1
%35 = load i8*, i8** %5
%36 = call i32 @strlen(i8* %35)
%37 = add nsw i32 %34, %36
%38 = sext i32 %37 to i64
%39 = add i64 %38, 42
%40 = add i64 %39, 1
store i64 %40, i64* %9
%41 = load i64, i64* %9
%42 = call i8* @malloc(i64 %41)
store i8* %42, i8** %6
%43 = load i8*, i8** %6
%44 = icmp ne i8* %43, null
br i1 %44, label %47, label %45
45: ; preds = %31
%46 = load i8*, i8** %6
store i8* %46, i8** %3
br label %85
47: ; preds = %31
br label %48
48: ; preds = %75, %47
%49 = load i32, i32* @CLOCK_REALTIME
%50 = call i32 @clock_gettime(i32 %49, %struct.timespec* %7)
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%52 = load i32, i32* %51
%53 = ptrtoint i8** %6 to i32
%54 = xor i32 %52, %53
%55 = load i8*, i8** %6
%56 = ptrtoint i8* %55 to i32
%57 = xor i32 %54, %56
store i32 %57, i32* %10
%58 = load i8*, i8** %6
%59 = load i64, i64* %9
%60 = load i8*, i8** %4
%61 = load i8*, i8** %5
%62 = load i32, i32* %8
%63 = call i32 @a_fetch_add(i32* @tempnam.index, i32 1)
%64 = load i32, i32* %10
%65 = call i32 @snprintf(i8* %58, i64 %59, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %60, i8* %61, i32 %62, i32 %63, i32 %64)
br label %66
66: ; preds = %48
%67 = load i8*, i8** %6
%68 = load i32, i32* @F_OK
%69 = call i64 @access(i8* %67, i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %75, label %71
71: ; preds = %66
%72 = load i32, i32* %11
%73 = add nsw i32 %72, 1
store i32 %73, i32* %11
%74 = icmp slt i32 %72, 100
br label %75
75: ; preds = %71, %66
%76 = phi i1 [ false, %66 ], [ %74, %71 ]
br i1 %76, label %48, label %77
77: ; preds = %75
%78 = load i32, i32* %11
%79 = icmp sge i32 %78, 100
br i1 %79, label %80, label %83
80: ; preds = %77
%81 = load i8*, i8** %6
%82 = call i32 @free(i8* %81)
store i8* null, i8** %3
br label %85
83: ; preds = %77
%84 = load i8*, i8** %6
store i8* %84, i8** %3
br label %85
85: ; preds = %83, %80, %45, %30
%86 = load i8*, i8** %3
ret i8* %86
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@tempnam.index = internal global i32 0
@P_tmpdir = global i8* null
@.str = [5 x i8] c"temp\00"
@R_OK = global i32 0
@W_OK = global i32 0
@X_OK = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.1 = [15 x i8] c"%s/%s-%d-%d-%x\00"
@F_OK = global i32 0
define i8* @tempnam(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.timespec
%5 = bitcast i8** %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
%7 = tail call i32 (...) @getpid()
%8 = icmp eq i8* %0, null
%9 = load i8*, i8** @P_tmpdir
%10 = select i1 %8, i8* %9, i8* %0
%11 = icmp eq i8* %1, null
%12 = select i1 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1
%13 = load i32, i32* @R_OK
%14 = load i32, i32* @W_OK
%15 = or i32 %14, %13
%16 = load i32, i32* @X_OK
%17 = or i32 %15, %16
%18 = tail call i64 @access(i8* %10, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %59
20: ; preds = %2
%21 = tail call i32 @strlen(i8* %10)
%22 = add nsw i32 %21, 1
%23 = tail call i32 @strlen(i8* %12)
%24 = add nsw i32 %22, %23
%25 = sext i32 %24 to i64
%26 = add nsw i64 %25, 43
%27 = tail call i8* @malloc(i64 %26)
store i8* %27, i8** %3
%28 = icmp eq i8* %27, null
br i1 %28, label %59, label %29
29: ; preds = %20
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%31 = ptrtoint i8** %3 to i64
%32 = trunc i64 %31 to i32
br label %33
33: ; preds = %29, %49
%34 = phi i32 [ %50, %49 ], [ 0, %29 ]
%35 = load i32, i32* @CLOCK_REALTIME
%36 = call i32 @clock_gettime(i32 %35, %struct.timespec* %4)
%37 = load i32, i32* %30
%38 = xor i32 %37, %32
%39 = load i8*, i8** %3
%40 = ptrtoint i8* %39 to i64
%41 = trunc i64 %40 to i32
%42 = xor i32 %38, %41
%43 = call i32 @a_fetch_add(i32* @tempnam.index, i32 1)
%44 = call i32 @snprintf(i8* %39, i64 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %12, i32 %7, i32 %43, i32 %42)
%45 = load i8*, i8** %3
%46 = load i32, i32* @F_OK
%47 = call i64 @access(i8* %45, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %33
%50 = add nuw nsw i32 %34, 1
%51 = icmp eq i32 %50, 101
br i1 %51, label %54, label %33
52: ; preds = %33
%53 = icmp ugt i32 %34, 99
br i1 %53, label %54, label %57
54: ; preds = %49, %52
%55 = load i8*, i8** %3
%56 = call i32 @free(i8* %55)
br label %59
57: ; preds = %52
%58 = load i8*, i8** %3
br label %59
59: ; preds = %20, %2, %57, %54
%60 = phi i8* [ null, %54 ], [ %58, %57 ], [ null, %2 ], [ null, %20 ]
ret i8* %60
}