#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*/ FILE;

/* Variables and functions */
int S_IRUSR;
int S_IWUSR;
scalar_t__ fchmod(int, int);
int /*<<< orphan*/ *fdopen(int, char *);
int mkstemp(char *);
char *strdup(char *);
int /*<<< orphan*/ unlink(char *);
FILE *safe_tmpfile(void) {
  int fd;
  char *tfile;
  tfile = strdup("hn-inXXXXXX");
  if ((fd = mkstemp(tfile)) < 0)
    return (NULL);
  unlink(tfile);
  if (fchmod(fd, S_IRUSR | S_IWUSR) != 0)
    return (NULL);
  return (fdopen(fd, "w+b"));
}


x86 assembly (clang, -O0):
<safe_tmpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <mkstemp@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    118a <safe_tmpfile+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    11d7 <safe_tmpfile+0x87>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0xc(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
call   1060 <fchmod@plt>
cmp    $0x0,%rax
je     11c4 <safe_tmpfile+0x74>
movq   $0x0,-0x8(%rbp)
jmp    11d7 <safe_tmpfile+0x87>
mov    -0xc(%rbp),%edi
lea    0xe3e(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_tmpfile>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <mkstemp@plt>
test   %eax,%eax
js     1196 <safe_tmpfile+0x46>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e59(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   1060 <fchmod@plt>
test   %rax,%rax
je     119f <safe_tmpfile+0x4f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe66(%rip),%rsi
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <fdopen@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"hn-inXXXXXX\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [4 x i8] c"w+b\00"
define i32* @safe_tmpfile() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i8*
%4 = call i8* @strdup(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @mkstemp(i8* %5)
store i32 %6, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
store i32* null, i32** %1
br label %22
9: ; preds = %0
%10 = load i8*, i8** %3
%11 = call i32 @unlink(i8* %10)
%12 = load i32, i32* %2
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = call i64 @fchmod(i32 %12, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %9
store i32* null, i32** %1
br label %22
19: ; preds = %9
%20 = load i32, i32* %2
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %1
br label %22
22: ; preds = %19, %18, %8
%23 = load i32*, i32** %1
ret i32* %23
}

LLVM IR (-O2):
@.str = [12 x i8] c"hn-inXXXXXX\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [4 x i8] c"w+b\00"
define noalias i32* @safe_tmpfile() {
%1 = tail call dereferenceable_or_null(12) i8* @strdup(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @mkstemp(i8* %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %13, label %4
4: ; preds = %0
%5 = tail call i32 @unlink(i8* %1)
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i64 @fchmod(i32 %2, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = tail call i32* @fdopen(i32 %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %4, %0, %11
%14 = phi i32* [ %12, %11 ], [ null, %0 ], [ null, %4 ]
ret i32* %14
}