#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 */

/* Variables and functions */
int /*<<< orphan*/ MAXPATHLEN;
char *P_tmpdir;
char *_PATH_TMP;
char *_mktemp(char *);
int errno;
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ issetugid();
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int /*<<< orphan*/, char *, char *,
                            char const *, ...);
int strlen(char *);
char *tempnam(const char *dir, const char *pfx) {
  int sverrno;
  char *f, *name;
  if (!(name = malloc(MAXPATHLEN)))
    return (NULL);
  if (!pfx)
    pfx = "tmp.";
  if (issetugid() == 0 && (f = getenv("TMPDIR"))) {
    (void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXXXXXX", f,
                   *(f + strlen(f) - 1) == '/' ? "" : "/", pfx);
    if ((f = _mktemp(name)))
      return (f);
  }
  if ((f = (char *)dir)) {
    (void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXXXXXX", f,
                   *(f + strlen(f) - 1) == '/' ? "" : "/", pfx);
    if ((f = _mktemp(name)))
      return (f);
  }
  f = P_tmpdir;
  (void)snprintf(name, MAXPATHLEN, "%s%sXXXXXXXXX", f, pfx);
  if ((f = _mktemp(name)))
    return (f);
  f = _PATH_TMP;
  (void)snprintf(name, MAXPATHLEN, "%s%sXXXXXXXXX", f, pfx);
  if ((f = _mktemp(name)))
    return (f);
  sverrno = errno;
  free(name);
  errno = sverrno;
  return (NULL);
}


x86 assembly (clang, -O0):
<tempnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11a9 <tempnam+0x39>
movq   $0x0,-0x8(%rbp)
jmp    13fb <tempnam+0x28b>
cmpq   $0x0,-0x18(%rbp)
jne    11bf <tempnam+0x4f>
lea    0xe45(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <issetugid@plt>
cmp    $0x0,%rax
jne    1280 <tempnam+0x110>
lea    0xe2e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1280 <tempnam+0x110>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rcx
mov    %eax,%edx
mov    -0x38(%rbp),%rax
movslq %edx,%rdx
movsbl -0x1(%rax,%rdx,1),%edx
lea    0xde6(%rip),%r8
lea    0xdc6(%rip),%rax
cmp    $0x2f,%edx
cmove  %rax,%r8
mov    -0x18(%rbp),%r9
lea    0xdbc(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1060 <_mktemp@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     127b <tempnam+0x10b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13fb <tempnam+0x28b>
jmp    1280 <tempnam+0x110>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1328 <tempnam+0x1b8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2d3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x64(%rbp),%esi
mov    -0x60(%rbp),%rcx
mov    %eax,%edx
mov    -0x58(%rbp),%rax
movslq %edx,%rdx
movsbl -0x1(%rax,%rdx,1),%edx
lea    0xd3e(%rip),%r8
lea    0xd1e(%rip),%rax
cmp    $0x2f,%edx
cmove  %rax,%r8
mov    -0x18(%rbp),%r9
lea    0xd14(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1060 <_mktemp@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1323 <tempnam+0x1b3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13fb <tempnam+0x28b>
jmp    1328 <tempnam+0x1b8>
mov    0x2cb9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xccd(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1060 <_mktemp@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     137d <tempnam+0x20d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13fb <tempnam+0x28b>
mov    0x2c44(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2c4a(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xc78(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1060 <_mktemp@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     13d2 <tempnam+0x262>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13fb <tempnam+0x28b>
mov    0x2bf7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2bdf(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tempnam>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
mov    0x2e58(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     126e <tempnam+0xfe>
mov    %rax,%r13
test   %rbx,%rbx
lea    0xe5a(%rip),%r14
cmovne %rbx,%r14
xor    %eax,%eax
call   1080 <issetugid@plt>
test   %rax,%rax
je     127f <tempnam+0x10f>
test   %r15,%r15
je     120a <tempnam+0x9a>
mov    0x0(%rbp),%ebx
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%r15,%rax,1)
lea    0xe2b(%rip),%rax
lea    0xe3d(%rip),%r8
cmove  %rax,%r8
lea    0xe21(%rip),%rdx
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r15,%rcx
mov    %r14,%r9
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %r13,%rdi
call   1060 <_mktemp@plt>
test   %rax,%rax
jne    1270 <tempnam+0x100>
mov    0x2dd7(%rip),%rax
mov    (%rax),%rcx
mov    0x0(%rbp),%esi
lea    0xe01(%rip),%rdx
mov    %r13,%rdi
mov    %r14,%r8
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %r13,%rdi
call   1060 <_mktemp@plt>
test   %rax,%rax
jne    1270 <tempnam+0x100>
mov    0x2d91(%rip),%rax
mov    (%rax),%rcx
mov    0x0(%rbp),%esi
lea    0xdd3(%rip),%rdx
mov    %r13,%rdi
mov    %r14,%r8
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %r13,%rdi
call   1060 <_mktemp@plt>
test   %rax,%rax
jne    1270 <tempnam+0x100>
mov    %r13,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ba <tempnam+0x4a>
mov    %rax,%r12
mov    0x0(%rbp),%ebx
mov    %rax,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%r12,%rax,1)
lea    0xd53(%rip),%rax
lea    0xd65(%rip),%r8
cmove  %rax,%r8
lea    0xd49(%rip),%rdx
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r12,%rcx
mov    %r14,%r9
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %r13,%rdi
call   1060 <_mktemp@plt>
test   %rax,%rax
jne    1270 <tempnam+0x100>
jmp    11ba <tempnam+0x4a>

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@.str = [5 x i8] c"tmp.\00"
@.str.1 = [7 x i8] c"TMPDIR\00"
@.str.2 = [17 x i8] c"%s%s%sXXXXXXXXXX\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [2 x i8] c"/\00"
@P_tmpdir = global i8* null
@.str.5 = [14 x i8] c"%s%sXXXXXXXXX\00"
@_PATH_TMP = global i8* null
@errno = global i32 0
define i8* @tempnam(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i32, i32* @MAXPATHLEN
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %8
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i8* null, i8** %3
br label %101
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %13
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %5
br label %17
17: ; preds = %16, %13
%18 = call i64 (...) @issetugid()
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %46
20: ; preds = %17
%21 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* %21, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %46
23: ; preds = %20
%24 = load i8*, i8** %8
%25 = load i32, i32* @MAXPATHLEN
%26 = load i8*, i8** %7
%27 = load i8*, i8** %7
%28 = load i8*, i8** %7
%29 = call i32 @strlen(i8* %28)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = getelementptr inbounds i8, i8* %31, i64 -1
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 47
%36 = zext i1 %35 to i64
%37 = select i1 %35, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0)
%38 = load i8*, i8** %5
%39 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %24, i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %37, i8* %38)
%40 = load i8*, i8** %8
%41 = call i8* @_mktemp(i8* %40)
store i8* %41, i8** %7
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %23
%44 = load i8*, i8** %7
store i8* %44, i8** %3
br label %101
45: ; preds = %23
br label %46
46: ; preds = %45, %20, %17
%47 = load i8*, i8** %4
store i8* %47, i8** %7
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %72
49: ; preds = %46
%50 = load i8*, i8** %8
%51 = load i32, i32* @MAXPATHLEN
%52 = load i8*, i8** %7
%53 = load i8*, i8** %7
%54 = load i8*, i8** %7
%55 = call i32 @strlen(i8* %54)
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
%58 = getelementptr inbounds i8, i8* %57, i64 -1
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 47
%62 = zext i1 %61 to i64
%63 = select i1 %61, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0)
%64 = load i8*, i8** %5
%65 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %50, i32 %51, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %52, i8* %63, i8* %64)
%66 = load i8*, i8** %8
%67 = call i8* @_mktemp(i8* %66)
store i8* %67, i8** %7
%68 = icmp ne i8* %67, null
br i1 %68, label %69, label %71
69: ; preds = %49
%70 = load i8*, i8** %7
store i8* %70, i8** %3
br label %101
71: ; preds = %49
br label %72
72: ; preds = %71, %46
%73 = load i8*, i8** @P_tmpdir
store i8* %73, i8** %7
%74 = load i8*, i8** %8
%75 = load i32, i32* @MAXPATHLEN
%76 = load i8*, i8** %7
%77 = load i8*, i8** %5
%78 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %74, i32 %75, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %76, i8* %77)
%79 = load i8*, i8** %8
%80 = call i8* @_mktemp(i8* %79)
store i8* %80, i8** %7
%81 = icmp ne i8* %80, null
br i1 %81, label %82, label %84
82: ; preds = %72
%83 = load i8*, i8** %7
store i8* %83, i8** %3
br label %101
84: ; preds = %72
%85 = load i8*, i8** @_PATH_TMP
store i8* %85, i8** %7
%86 = load i8*, i8** %8
%87 = load i32, i32* @MAXPATHLEN
%88 = load i8*, i8** %7
%89 = load i8*, i8** %5
%90 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %86, i32 %87, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %88, i8* %89)
%91 = load i8*, i8** %8
%92 = call i8* @_mktemp(i8* %91)
store i8* %92, i8** %7
%93 = icmp ne i8* %92, null
br i1 %93, label %94, label %96
94: ; preds = %84
%95 = load i8*, i8** %7
store i8* %95, i8** %3
br label %101
96: ; preds = %84
%97 = load i32, i32* @errno
store i32 %97, i32* %6
%98 = load i8*, i8** %8
%99 = call i32 @free(i8* %98)
%100 = load i32, i32* %6
store i32 %100, i32* @errno
store i8* null, i8** %3
br label %101
101: ; preds = %96, %94, %82, %69, %43, %12
%102 = load i8*, i8** %3
ret i8* %102
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@.str = [5 x i8] c"tmp.\00"
@.str.1 = [7 x i8] c"TMPDIR\00"
@.str.2 = [17 x i8] c"%s%s%sXXXXXXXXXX\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [2 x i8] c"/\00"
@P_tmpdir = global i8* null
@.str.5 = [14 x i8] c"%s%sXXXXXXXXX\00"
@_PATH_TMP = global i8* null
@errno = global i32 0
define i8* @tempnam(i8* %0, i8* %1) {
%3 = load i32, i32* @MAXPATHLEN
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %54, label %6
6: ; preds = %2
%7 = icmp eq i8* %1, null
%8 = select i1 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1
%9 = tail call i64 (...) @issetugid()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %6
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %26, label %14
14: ; preds = %11
%15 = load i32, i32* @MAXPATHLEN
%16 = tail call i32 @strlen(i8* %12)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %12, i64 %17
%19 = getelementptr inbounds i8, i8* %18, i64 -1
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 47
%22 = select i1 %21, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0)
%23 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %4, i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %12, i8* %22, i8* %8)
%24 = tail call i8* @_mktemp(i8* %4)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %54
26: ; preds = %14, %11, %6
%27 = icmp eq i8* %0, null
br i1 %27, label %40, label %28
28: ; preds = %26
%29 = load i32, i32* @MAXPATHLEN
%30 = tail call i32 @strlen(i8* %0)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %0, i64 %31
%33 = getelementptr inbounds i8, i8* %32, i64 -1
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 47
%36 = select i1 %35, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0)
%37 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %4, i32 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %36, i8* %8)
%38 = tail call i8* @_mktemp(i8* %4)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %54
40: ; preds = %28, %26
%41 = load i8*, i8** @P_tmpdir
%42 = load i32, i32* @MAXPATHLEN
%43 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %4, i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %41, i8* %8)
%44 = tail call i8* @_mktemp(i8* %4)
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %54
46: ; preds = %40
%47 = load i8*, i8** @_PATH_TMP
%48 = load i32, i32* @MAXPATHLEN
%49 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %4, i32 %48, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %47, i8* %8)
%50 = tail call i8* @_mktemp(i8* %4)
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = tail call i32 @free(i8* %4)
br label %54
54: ; preds = %46, %40, %28, %14, %2, %52
%55 = phi i8* [ null, %52 ], [ null, %2 ], [ %24, %14 ], [ %38, %28 ], [ %44, %40 ], [ %50, %46 ]
ret i8* %55
}