#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 O_CREAT;
int O_RDWR;
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *tmpnam(int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(char *);
void tmpnam_and_open(void) {
  printf("Entered tmpnam_and_open()\n");
  char *filename = tmpnam(NULL);
  printf("filename = %s\n", filename);
  int fd;
  fd = open(filename, O_RDWR | O_CREAT, 0);
  if (fd < 0) {
    perror("Error creating file");
  } else {
    printf("Created file %s\n", filename);
    unlink(filename);
  }
  printf("Leaving tmpnam_and_open()\n");
}


x86 assembly (clang, -O0):
<tmpnam_and_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <tmpnam@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c0 <tmpnam_and_open+0x70>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
jmp    11db <tmpnam_and_open+0x8b>
mov    -0x8(%rbp),%rsi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tmpnam_and_open>:
push   %rbx
lea    0xecc(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <tmpnam@plt>
mov    %rax,%rbx
lea    0xe82(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e51(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11ce <tmpnam_and_open+0x6e>
lea    0xe74(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
lea    0xe86(%rip),%rdi
pop    %rbx
jmp    1040 <puts@plt>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
jmp    11c1 <tmpnam_and_open+0x61>

LLVM IR (-O0):
@.str = [27 x i8] c"Entered tmpnam_and_open()\0A\00"
@.str.1 = [15 x i8] c"filename = %s\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [20 x i8] c"Error creating file\00"
@.str.3 = [17 x i8] c"Created file %s\0A\00"
@.str.4 = [27 x i8] c"Leaving tmpnam_and_open()\0A\00"
define void @tmpnam_and_open() {
%1 = alloca i8*
%2 = alloca i32
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%4 = call i8* @tmpnam(i32* null)
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%7 = load i8*, i8** %1
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %21
16: ; preds = %0
%17 = load i8*, i8** %1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %1
%20 = call i32 @unlink(i8* %19)
br label %21
21: ; preds = %16, %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"filename = %s\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [20 x i8] c"Error creating file\00"
@.str.3 = [17 x i8] c"Created file %s\0A\00"
@str = [26 x i8] c"Entered tmpnam_and_open()\00"
@str.5 = [26 x i8] c"Leaving tmpnam_and_open()\00"
define void @tmpnam_and_open() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%2 = tail call i8* @tmpnam(i32* null)
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %2, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %14
11: ; preds = %0
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%13 = tail call i32 @unlink(i8* %2)
br label %14
14: ; preds = %11, %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.5, i64 0, i64 0))
ret void
}