#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int mkstemp(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ unlink(char *);
int main() {
  const char *sdn = "/securedir";
  char sfn[] = "/securedir/temp-XXXXXX";
  FILE *sfp;
  int fd = mkstemp(sfn);
  if (fd == -1) {
    printf("Could not create temporary file\n");
  }
  sfp = fdopen(fd, "w+");
  if (sfp == NULL) {
    close(fd);
    printf("Could not open temp file to write\n");
  }
  fwrite("Hello world", 10, 1, sfp);
  fclose(sfp);
  unlink(sfn);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xecf(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xecc(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xeca(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xec4(%rip),%ax
mov    %ax,-0x1c(%rbp)
mov    0xebc(%rip),%al
mov    %al,-0x1a(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <mkstemp@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11df <main+0x6f>
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
mov    -0x3c(%rbp),%edi
lea    0xe43(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1211 <main+0xa1>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
lea    0xe23(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rcx
lea    0xe36(%rip),%rdi
mov    $0xa,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movaps 0xe83(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x5858585858582d,%rax
mov    %rax,0xf(%rsp)
mov    %rsp,%rdi
call   1070 <mkstemp@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ab <main+0x3b>
lea    0xea2(%rip),%rdi
call   1040 <puts@plt>
lea    0xe65(%rip),%rsi
mov    %ebp,%edi
call   1080 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d4 <main+0x64>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe57(%rip),%rdi
call   1040 <puts@plt>
lea    0xe3f(%rip),%rdi
mov    $0xa,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/securedir\00"
@__const.main.sfn = [23 x i8] c"/securedir/temp-XXXXXX\00"
@.str.1 = [33 x i8] c"Could not create temporary file\0A\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [35 x i8] c"Could not open temp file to write\0A\00"
@.str.4 = [12 x i8] c"Hello world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [23 x i8]
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %2
%6 = bitcast [23 x i8]* %3 to i8*
%7 = getelementptr inbounds [23 x i8], [23 x i8]* %3, i64 0, i64 0
%8 = call i32 @mkstemp(i8* %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = load i32, i32* %5
%15 = call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %18, %13
%23 = load i32*, i32** %4
%24 = call i32 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 10, i32 1, i32* %23)
%25 = load i32*, i32** %4
%26 = call i32 @fclose(i32* %25)
%27 = getelementptr inbounds [23 x i8], [23 x i8]* %3, i64 0, i64 0
%28 = call i32 @unlink(i8* %27)
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@__const.main.sfn = [23 x i8] c"/securedir/temp-XXXXXX\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.4 = [12 x i8] c"Hello world\00"
@str = [34 x i8] c"Could not open temp file to write\00"
@str.5 = [32 x i8] c"Could not create temporary file\00"
define i32 @main() {
%1 = alloca [23 x i8]
%2 = getelementptr inbounds [23 x i8], [23 x i8]* %1, i64 0, i64 0
%3 = call i32 @mkstemp(i8* %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.5, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = call i32* @fdopen(i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = call i32 @close(i32 %3)
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %10, %7
%14 = call i32 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 10, i32 1, i32* %8)
%15 = call i32 @fclose(i32* %8)
%16 = call i32 @unlink(i8* %2)
ret i32 0
}