#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__ ENOENT;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int mkstemp(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
void make_temp(char *template) {
  int fd;
  struct stat sbuf;
  if ((fd = mkstemp(template)) < 0) {
    fprintf(stderr, "can't create temp file\n");
    exit(1);
  }
  printf("temp name = %s\n", template);
  close(fd);
  if (stat(template, &sbuf) < 0) {
    if (errno == ENOENT) {
      printf("file doesn't exist\n");
    } else {
      fprintf(stderr, "stat failed\n");
      exit(1);
    }
  } else {
    printf("file exists\n");
    unlink(template);
  }
}


x86 assembly (clang, -O0):
<make_temp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11b0 <make_temp+0x40>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    1232 <make_temp+0xc2>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
jne    120e <make_temp+0x9e>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122d <make_temp+0xbd>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1249 <make_temp+0xd9>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_temp>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1090 <mkstemp@plt>
test   %eax,%eax
js     11f8 <make_temp+0x78>
mov    %eax,%ebp
lea    0xe80(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
js     11cf <make_temp+0x4f>
lea    0xe75(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
jmp    11f1 <make_temp+0x71>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <make_temp+0x8a>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
jmp    121a <make_temp+0x9a>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"can't create temp file\0A\00"
@.str.1 = [16 x i8] c"temp name = %s\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [20 x i8] c"file doesn't exist\0A\00"
@.str.3 = [13 x i8] c"stat failed\0A\00"
@.str.4 = [13 x i8] c"file exists\0A\00"
define void @make_temp(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @mkstemp(i8* %5)
store i32 %6, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* %3
%16 = call i32 @close(i32 %15)
%17 = load i8*, i8** %2
%18 = call i64 @stat(i8* %17, %struct.stat* %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %12
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %30
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %24
br label %35
31: ; preds = %12
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%33 = load i8*, i8** %2
%34 = call i32 @unlink(i8* %33)
br label %35
35: ; preds = %31, %30
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"can't create temp file\0A\00"
@.str.1 = [16 x i8] c"temp name = %s\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.3 = [13 x i8] c"stat failed\0A\00"
@str = [12 x i8] c"file exists\00"
@str.5 = [19 x i8] c"file doesn't exist\00"
define void @make_temp(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @mkstemp(i8* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = tail call i32 @close(i32 %4)
%13 = call i64 @stat(i8* %0, %struct.stat* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
br label %28
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %10
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%27 = call i32 @unlink(i8* %0)
br label %28
28: ; preds = %25, %19
ret void
}