#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 stat {
  int dummy;
};

/* Variables and functions */
scalar_t__ alloca(int);
char *check_dir(char const *);
int /*<<< orphan*/ cvt(char *, int, int);
char *getenv(char *);
int random();
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char const *);
char *xstrdup(char *);
char *my_tempnam(char const *dir, char const *pfx) {
  char *actdir = 0;
  char *actname = 0;
  char *actpfx = 0;
  struct stat st;
  int len, rnd;
  while (1) {
    if ((actdir = check_dir(getenv("TMPDIR"))))
      break;
    if ((actdir = check_dir(getenv("TEMPDIR"))))
      break;
    if ((actdir = check_dir(dir)))
      break;
    if ((actdir = check_dir("/tmp")))
      break;
    if ((actdir = check_dir(".")))
      break;
    return 0;
  }
  len = strlen(actdir);
  if (pfx)
    len += strlen(pfx);
  actpfx = (char *)alloca(len + 32);
  actname = (char *)alloca(len + 32);
  strcpy(actpfx, actdir);
  if (!*actpfx)
    strcpy(actpfx, ".");
  len = strlen(actpfx);
  if (actpfx[len - 1] != '/')
    strcat(actpfx, "/");
  if (pfx)
    strcat(actpfx, pfx);
  len = strlen(actpfx);
  while (1) {
    rnd = random();
    strcpy(actname, actpfx);
    cvt(actname + len, 6, rnd);
    if (stat(actname, &st) < 0)
      break;
  }
  return xstrdup(actname);
}


x86 assembly (clang, -O0):
<my_tempnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe31(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   10b0 <check_dir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ef <my_tempnam+0x4f>
jmp    127d <my_tempnam+0xdd>
lea    0xe11(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   10b0 <check_dir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1216 <my_tempnam+0x76>
jmp    127d <my_tempnam+0xdd>
mov    -0x10(%rbp),%rdi
call   10b0 <check_dir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1232 <my_tempnam+0x92>
jmp    127d <my_tempnam+0xdd>
lea    0xdd6(%rip),%rdi
call   10b0 <check_dir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1251 <my_tempnam+0xb1>
jmp    127d <my_tempnam+0xdd>
lea    0xdbc(%rip),%rdi
call   10b0 <check_dir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1270 <my_tempnam+0xd0>
jmp    127d <my_tempnam+0xdd>
movq   $0x0,-0x8(%rbp)
jmp    13a2 <my_tempnam+0x202>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12a3 <my_tempnam+0x103>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
add    $0x20,%edi
call   1070 <alloca@plt>
mov    %rax,-0x30(%rbp)
mov    -0x3c(%rbp),%edi
add    $0x20,%edi
call   1070 <alloca@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
jne    12eb <my_tempnam+0x14b>
mov    -0x30(%rbp),%rdi
lea    0xd2e(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x3c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1321 <my_tempnam+0x181>
mov    -0x30(%rbp),%rdi
lea    0xcfa(%rip),%rsi
call   10a0 <strcat@plt>
cmpq   $0x0,-0x18(%rbp)
je     1339 <my_tempnam+0x199>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1090 <random@plt>
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
mov    -0x40(%rbp),%edx
mov    $0x6,%esi
call   10c0 <cvt@plt>
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    1390 <my_tempnam+0x1f0>
jmp    1395 <my_tempnam+0x1f5>
jmp    1345 <my_tempnam+0x1a5>
mov    -0x28(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_tempnam>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe47(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   10b0 <check_dir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1226 <my_tempnam+0x86>
lea    0xe32(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   10b0 <check_dir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1226 <my_tempnam+0x86>
mov    %rbx,%rdi
call   10b0 <check_dir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1226 <my_tempnam+0x86>
lea    0xe0e(%rip),%rdi
call   10b0 <check_dir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1226 <my_tempnam+0x86>
lea    0xdff(%rip),%rdi
call   10b0 <check_dir@plt>
mov    %rax,%r12
test   %rax,%rax
je     1308 <my_tempnam+0x168>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
test   %r14,%r14
je     123f <my_tempnam+0x9f>
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %eax,%ebx
add    $0x20,%ebx
mov    %ebx,%edi
call   1070 <alloca@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1070 <alloca@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
cmpb   $0x0,(%r15)
jne    1276 <my_tempnam+0xd6>
lea    0xda6(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r15,1)
je     1297 <my_tempnam+0xf7>
lea    0xd87(%rip),%rsi
mov    %r15,%rdi
call   10a0 <strcat@plt>
test   %r14,%r14
je     12a7 <my_tempnam+0x107>
mov    %r15,%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%r14
add    %rbx,%r14
lea    0x8(%rsp),%r12
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <random@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
mov    $0x6,%esi
mov    %ebp,%edx
call   10c0 <cvt@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jns    12c0 <my_tempnam+0x120>
mov    %rbx,%rdi
call   1060 <xstrdup@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    12fb <my_tempnam+0x15b>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [8 x i8] c"TEMPDIR\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [2 x i8] c"/\00"
define i8* @my_tempnam(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
br label %12
12: ; preds = %2
%13 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i8* @check_dir(i8* %13)
store i8* %14, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %12
br label %36
17: ; preds = %12
%18 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = call i8* @check_dir(i8* %18)
store i8* %19, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17
br label %36
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = call i8* @check_dir(i8* %23)
store i8* %24, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %22
br label %36
27: ; preds = %22
%28 = call i8* @check_dir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %28, i8** %6
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %27
br label %36
31: ; preds = %27
%32 = call i8* @check_dir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %32, i8** %6
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %31
br label %36
35: ; preds = %31
store i8* null, i8** %3
br label %107
36: ; preds = %34, %30, %26, %21, %16
%37 = load i8*, i8** %6
%38 = call i32 @strlen(i8* %37)
store i32 %38, i32* %10
%39 = load i8*, i8** %5
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = load i8*, i8** %5
%43 = call i32 @strlen(i8* %42)
%44 = load i32, i32* %10
%45 = add nsw i32 %44, %43
store i32 %45, i32* %10
br label %46
46: ; preds = %41, %36
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 32
%49 = call i64 @alloca(i32 %48)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %8
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 32
%53 = call i64 @alloca(i32 %52)
%54 = inttoptr i64 %53 to i8*
store i8* %54, i8** %7
%55 = load i8*, i8** %8
%56 = load i8*, i8** %6
%57 = call i32 @strcpy(i8* %55, i8* %56)
%58 = load i8*, i8** %8
%59 = load i8, i8* %58
%60 = icmp ne i8 %59, 0
br i1 %60, label %64, label %61
61: ; preds = %46
%62 = load i8*, i8** %8
%63 = call i32 @strcpy(i8* %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %64
64: ; preds = %61, %46
%65 = load i8*, i8** %8
%66 = call i32 @strlen(i8* %65)
store i32 %66, i32* %10
%67 = load i8*, i8** %8
%68 = load i32, i32* %10
%69 = sub nsw i32 %68, 1
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %67, i64 %70
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp ne i32 %73, 47
br i1 %74, label %75, label %78
75: ; preds = %64
%76 = load i8*, i8** %8
%77 = call i32 @strcat(i8* %76, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %78
78: ; preds = %75, %64
%79 = load i8*, i8** %5
%80 = icmp ne i8* %79, null
br i1 %80, label %81, label %85
81: ; preds = %78
%82 = load i8*, i8** %8
%83 = load i8*, i8** %5
%84 = call i32 @strcat(i8* %82, i8* %83)
br label %85
85: ; preds = %81, %78
%86 = load i8*, i8** %8
%87 = call i32 @strlen(i8* %86)
store i32 %87, i32* %10
br label %88
88: ; preds = %85, %103
%89 = call i32 (...) @random()
store i32 %89, i32* %11
%90 = load i8*, i8** %7
%91 = load i8*, i8** %8
%92 = call i32 @strcpy(i8* %90, i8* %91)
%93 = load i8*, i8** %7
%94 = load i32, i32* %10
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %93, i64 %95
%97 = load i32, i32* %11
%98 = call i32 @cvt(i8* %96, i32 6, i32 %97)
%99 = load i8*, i8** %7
%100 = call i64 @stat(i8* %99, %struct.stat* %9)
%101 = icmp slt i64 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %88
br label %104
103: ; preds = %88
br label %88
104: ; preds = %102
%105 = load i8*, i8** %7
%106 = call i8* @xstrdup(i8* %105)
store i8* %106, i8** %3
br label %107
107: ; preds = %104, %35
%108 = load i8*, i8** %3
ret i8* %108
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [8 x i8] c"TEMPDIR\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [2 x i8] c"/\00"
define i8* @my_tempnam(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @check_dir(i8* %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %21
8: ; preds = %2
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i8* @check_dir(i8* %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = tail call i8* @check_dir(i8* %0)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = tail call i8* @check_dir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i8* @check_dir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %64, label %21
21: ; preds = %18, %15, %12, %8, %2
%22 = phi i8* [ %6, %2 ], [ %10, %8 ], [ %13, %12 ], [ %16, %15 ], [ %19, %18 ]
%23 = tail call i32 @strlen(i8* %22)
%24 = icmp eq i8* %1, null
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = tail call i32 @strlen(i8* %1)
%27 = add nsw i32 %26, %23
br label %28
28: ; preds = %25, %21
%29 = phi i32 [ %27, %25 ], [ %23, %21 ]
%30 = add nsw i32 %29, 32
%31 = tail call i64 @alloca(i32 %30)
%32 = inttoptr i64 %31 to i8*
%33 = tail call i64 @alloca(i32 %30)
%34 = inttoptr i64 %33 to i8*
%35 = tail call i32 @strcpy(i8* %32, i8* %22)
%36 = load i8, i8* %32
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = tail call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %28
%41 = tail call i32 @strlen(i8* %32)
%42 = add nsw i32 %41, -1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %32, i64 %43
%45 = load i8, i8* %44
%46 = icmp eq i8 %45, 47
br i1 %46, label %49, label %47
47: ; preds = %40
%48 = tail call i32 @strcat(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %40
br i1 %24, label %52, label %50
50: ; preds = %49
%51 = tail call i32 @strcat(i8* %32, i8* %1)
br label %52
52: ; preds = %50, %49
%53 = tail call i32 @strlen(i8* %32)
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %34, i64 %54
br label %56
56: ; preds = %56, %52
%57 = call i32 (...) @random()
%58 = call i32 @strcpy(i8* %34, i8* %32)
%59 = call i32 @cvt(i8* %55, i32 6, i32 %57)
%60 = call i64 @stat(i8* %34, %struct.stat* %3)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %56
62: ; preds = %56
%63 = call i8* @xstrdup(i8* %34)
br label %64
64: ; preds = %18, %62
%65 = phi i8* [ %63, %62 ], [ null, %18 ]
ret i8* %65
}