#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 */
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strfmt(char *, int, char *, char *);
int strlen(char *);
char *tmpfilename(char *);
int test_tmpfilename(void) {
  int rc = 0;
  char path[128];
  char *tmp = tmpfilename(NULL);
  assert(tmp && *tmp);
  if (*(tmp + strlen(tmp) - 1) == '/') {
    printf("Bad tmp '%s'\n", tmp);
    rc = 1;
  }
  strfmt(path, sizeof(path), "%s/test", tmp);
  char *fname = tmpfilename("test");
  if (strcmp(fname, path)) {
    printf("'%s' != '%s'\n", fname, path);
    rc = 1;
  }
  free(fname);
  fname = tmpfilename("/test");
  if (strcmp(fname, path)) {
    printf("'%s' != '%s'\n", fname, path);
    rc = 1;
  }
  free(fname);
  free(tmp);
  return rc;
}


x86 assembly (clang, -O0):
<test_tmpfilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <tmpfilename@plt>
mov    %rax,-0x98(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x98(%rbp)
mov    %al,-0xa1(%rbp)
je     11be <test_tmpfilename+0x4e>
mov    -0x98(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0xa1(%rbp)
mov    -0xa1(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x98(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x98(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0xb0(%rbp),%rax
movslq %ecx,%rcx
movsbl -0x1(%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    121e <test_tmpfilename+0xae>
mov    -0x98(%rbp),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rcx
mov    $0x80,%esi
lea    0xdd6(%rip),%rdx
call   1080 <strfmt@plt>
lea    0xdcd(%rip),%rdi
call   1090 <tmpfilename@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1290 <test_tmpfilename+0x120>
mov    -0xa0(%rbp),%rsi
lea    -0x90(%rbp),%rdx
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0xa0(%rbp),%rdi
call   1030 <free@plt>
lea    0xd6d(%rip),%rdi
call   1090 <tmpfilename@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12ef <test_tmpfilename+0x17f>
mov    -0xa0(%rbp),%rsi
lea    -0x90(%rbp),%rdx
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0xa0(%rbp),%rdi
call   1030 <free@plt>
mov    -0x98(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_tmpfilename>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
xor    %r14d,%r14d
xor    %edi,%edi
call   1090 <tmpfilename@plt>
mov    %rax,%r15
mov    $0x0,%edi
test   %rax,%rax
je     119f <test_tmpfilename+0x2f>
xor    %edi,%edi
cmpb   $0x0,(%r15)
setne  %dil
call   1070 <assert@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%r15,%rax,1)
jne    11cd <test_tmpfilename+0x5d>
lea    0xe43(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r14d
lea    0xe3a(%rip),%rdx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x80,%esi
mov    %r15,%rcx
call   1080 <strfmt@plt>
lea    0xe23(%rip),%rdi
call   1090 <tmpfilename@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1220 <test_tmpfilename+0xb0>
lea    0xe09(%rip),%rdi
mov    %rsp,%rdx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xde1(%rip),%rdi
call   1090 <tmpfilename@plt>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1261 <test_tmpfilename+0xf1>
lea    0xdc8(%rip),%rdi
mov    %rsp,%rdx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Bad tmp '%s'\0A\00"
@.str.1 = [8 x i8] c"%s/test\00"
@.str.2 = [5 x i8] c"test\00"
@.str.3 = [14 x i8] c"'%s' != '%s'\0A\00"
@.str.4 = [6 x i8] c"/test\00"
define i32 @test_tmpfilename() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i8* @tmpfilename(i8* null)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br label %13
13: ; preds = %8, %0
%14 = phi i1 [ false, %0 ], [ %12, %8 ]
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %3
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = getelementptr inbounds i8, i8* %21, i64 -1
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 47
br i1 %25, label %26, label %29
26: ; preds = %13
%27 = load i8*, i8** %3
%28 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 1, i32* %1
br label %29
29: ; preds = %26, %13
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%31 = load i8*, i8** %3
%32 = call i32 @strfmt(i8* %30, i32 128, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i8* @tmpfilename(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %33, i8** %4
%34 = load i8*, i8** %4
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%36 = call i64 @strcmp(i8* %34, i8* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %29
%39 = load i8*, i8** %4
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %39, i8* %40)
store i32 1, i32* %1
br label %42
42: ; preds = %38, %29
%43 = load i8*, i8** %4
%44 = call i32 @free(i8* %43)
%45 = call i8* @tmpfilename(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i8* %45, i8** %4
%46 = load i8*, i8** %4
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%48 = call i64 @strcmp(i8* %46, i8* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %42
%51 = load i8*, i8** %4
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%53 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %51, i8* %52)
store i32 1, i32* %1
br label %54
54: ; preds = %50, %42
%55 = load i8*, i8** %4
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %3
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
@.str = [14 x i8] c"Bad tmp '%s'\0A\00"
@.str.1 = [8 x i8] c"%s/test\00"
@.str.2 = [5 x i8] c"test\00"
@.str.3 = [14 x i8] c"'%s' != '%s'\0A\00"
@.str.4 = [6 x i8] c"/test\00"
define i32 @test_tmpfilename() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i8* @tmpfilename(i8* null)
%4 = icmp eq i8* %3, null
br i1 %4, label %9, label %5
5: ; preds = %0
%6 = load i8, i8* %3
%7 = icmp ne i8 %6, 0
%8 = zext i1 %7 to i32
br label %9
9: ; preds = %5, %0
%10 = phi i32 [ 0, %0 ], [ %8, %5 ]
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32 @strlen(i8* %3)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %3, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 -1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 47
br i1 %17, label %18, label %20
18: ; preds = %9
%19 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %20
20: ; preds = %18, %9
%21 = phi i32 [ 1, %18 ], [ 0, %9 ]
%22 = call i32 @strfmt(i8* %2, i32 128, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%23 = call i8* @tmpfilename(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = call i64 @strcmp(i8* %23, i8* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %20
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %23, i8* %2)
br label %28
28: ; preds = %26, %20
%29 = phi i32 [ 1, %26 ], [ %21, %20 ]
%30 = call i32 @free(i8* %23)
%31 = call i8* @tmpfilename(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%32 = call i64 @strcmp(i8* %31, i8* %2)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %28
%35 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %31, i8* %2)
br label %36
36: ; preds = %34, %28
%37 = phi i32 [ 1, %34 ], [ %29, %28 ]
%38 = call i32 @free(i8* %31)
%39 = call i32 @free(i8* %3)
ret i32 %37
}