#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 {
  uintptr_t 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 ^ (uintptr_t)&s ^ (uintptr_t)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    135c <tempnam+0x1dc>
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    135c <tempnam+0x1dc>
jmp    1265 <tempnam+0xe5>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x28(%rbp),%rax
lea    -0x20(%rbp),%rcx
xor    %rcx,%rax
mov    -0x20(%rbp),%rcx
xor    %rcx,%rax
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    0x2daf(%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    0xd21(%rip),%rdx
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
call   1070 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2cb4(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x69(%rbp)
jne    1329 <tempnam+0x1a9>
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     1354 <tempnam+0x1d4>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    135c <tempnam+0x1dc>
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,0xc(%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    12b0 <tempnam+0x130>
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,(%rsp)
xor    %ebp,%ebp
test   %rax,%rax
je     12b0 <tempnam+0x130>
nop
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%eax
mov    %rsp,%rcx
xor    %ecx,%eax
mov    (%rsp),%r14
mov    %r14d,%ebx
xor    %eax,%ebx
lea    0x2e1c(%rip),%rdi
mov    $0x1,%esi
call   1080 <a_fetch_add@plt>
mov    %r14,%rdi
mov    %r12,%rsi
lea    0xda2(%rip),%rdx
mov    %r15,%rcx
mov    %r13,%r8
mov    0xc(%rsp),%r9d
push   %rbx
push   %rax
call   1070 <snprintf@plt>
add    $0x10,%rsp
mov    (%rsp),%rdi
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
test   %rax,%rax
jne    129a <tempnam+0x11a>
add    $0x1,%ebp
cmp    $0x65,%ebp
jne    1220 <tempnam+0xa0>
jmp    129f <tempnam+0x11f>
cmp    $0x64,%ebp
jb     12ac <tempnam+0x12c>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    12b0 <tempnam+0x130>
mov    (%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 { i64 }
@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 %86
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 %86
47: ; preds = %31
br label %48
48: ; preds = %76, %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 i64, i64* %51
%53 = ptrtoint i8** %6 to i64
%54 = xor i64 %52, %53
%55 = load i8*, i8** %6
%56 = ptrtoint i8* %55 to i64
%57 = xor i64 %54, %56
%58 = trunc i64 %57 to i32
store i32 %58, i32* %10
%59 = load i8*, i8** %6
%60 = load i64, i64* %9
%61 = load i8*, i8** %4
%62 = load i8*, i8** %5
%63 = load i32, i32* %8
%64 = call i32 @a_fetch_add(i32* @tempnam.index, i32 1)
%65 = load i32, i32* %10
%66 = call i32 @snprintf(i8* %59, i64 %60, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %61, i8* %62, i32 %63, i32 %64, i32 %65)
br label %67
67: ; preds = %48
%68 = load i8*, i8** %6
%69 = load i32, i32* @F_OK
%70 = call i64 @access(i8* %68, i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %76, label %72
72: ; preds = %67
%73 = load i32, i32* %11
%74 = add nsw i32 %73, 1
store i32 %74, i32* %11
%75 = icmp slt i32 %73, 100
br label %76
76: ; preds = %72, %67
%77 = phi i1 [ false, %67 ], [ %75, %72 ]
br i1 %77, label %48, label %78
78: ; preds = %76
%79 = load i32, i32* %11
%80 = icmp sge i32 %79, 100
br i1 %80, label %81, label %84
81: ; preds = %78
%82 = load i8*, i8** %6
%83 = call i32 @free(i8* %82)
store i8* null, i8** %3
br label %86
84: ; preds = %78
%85 = load i8*, i8** %6
store i8* %85, i8** %3
br label %86
86: ; preds = %84, %81, %45, %30
%87 = load i8*, i8** %3
ret i8* %87
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
@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 %58
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 %58, label %29
29: ; preds = %20
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%31 = ptrtoint i8** %3 to i64
br label %32
32: ; preds = %29, %48
%33 = phi i32 [ %49, %48 ], [ 0, %29 ]
%34 = load i32, i32* @CLOCK_REALTIME
%35 = call i32 @clock_gettime(i32 %34, %struct.timespec* %4)
%36 = load i64, i64* %30
%37 = xor i64 %36, %31
%38 = load i8*, i8** %3
%39 = ptrtoint i8* %38 to i64
%40 = xor i64 %37, %39
%41 = trunc i64 %40 to i32
%42 = call i32 @a_fetch_add(i32* @tempnam.index, i32 1)
%43 = call i32 @snprintf(i8* %38, i64 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %12, i32 %7, i32 %42, i32 %41)
%44 = load i8*, i8** %3
%45 = load i32, i32* @F_OK
%46 = call i64 @access(i8* %44, i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %32
%49 = add nuw nsw i32 %33, 1
%50 = icmp eq i32 %49, 101
br i1 %50, label %53, label %32
51: ; preds = %32
%52 = icmp ugt i32 %33, 99
br i1 %52, label %53, label %56
53: ; preds = %48, %51
%54 = load i8*, i8** %3
%55 = call i32 @free(i8* %54)
br label %58
56: ; preds = %51
%57 = load i8*, i8** %3
br label %58
58: ; preds = %20, %2, %56, %53
%59 = phi i8* [ null, %53 ], [ %57, %56 ], [ null, %2 ], [ null, %20 ]
ret i8* %59
}