#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;
scalar_t__ errno;
int mkstemp(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ stat(char *, struct stat *);
int make_temp(char *template) {
  int fd;
  struct stat sbuf;
  if ((fd = mkstemp(template)) < 0)
    perror("cant create temp file");
  if (stat(template, &sbuf) < 0) {
    if (errno == ENOENT)
      printf("file doesnt exist\n");
    else
      perror("stat failed");
  } else {
    return fd;
  }
}


x86 assembly (clang, -O0):
<make_temp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <mkstemp@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    116d <make_temp+0x2d>
lea    0xe98(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11c0 <make_temp+0x80>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    11af <make_temp+0x6f>
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
jmp    11bb <make_temp+0x7b>
lea    0xe73(%rip),%rdi
call   1060 <perror@plt>
jmp    11c6 <make_temp+0x86>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_temp>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <mkstemp@plt>
mov    %eax,%ebp
test   %eax,%eax
js     118c <make_temp+0x4c>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jns    1183 <make_temp+0x43>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <make_temp+0x5a>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6d(%rip),%rdi
call   1060 <perror@plt>
jmp    1151 <make_temp+0x11>
lea    0xe75(%rip),%rdi
call   1060 <perror@plt>
jmp    1183 <make_temp+0x43>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [22 x i8] c"cant create temp file\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [19 x i8] c"file doesnt exist\0A\00"
@.str.2 = [12 x i8] c"stat failed\00"
define i32 @make_temp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @mkstemp(i8* %6)
store i32 %7, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = load i8*, i8** %3
%13 = call i64 @stat(i8* %12, %struct.stat* %5)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %11
%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 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %19
br label %26
24: ; preds = %11
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %23
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [22 x i8] c"cant create temp file\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [12 x i8] c"stat failed\00"
@str = [18 x i8] c"file doesnt exist\00"
define i32 @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 %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = call i64 @stat(i8* %0, %struct.stat* %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOENT
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %15, %8
ret i32 %4
}