#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int mkstemp(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *tmpfile();
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  printf("Creating a temporary file...\n");
  FILE *tmp = tmpfile();
  fprintf(tmp, "Hello world");
  fclose(tmp);
  char tfile[] = "/tmp/my-temp-file-XXXXXX";
  int fd = mkstemp(tfile);
  printf("Created: %s\n", tfile);
  if (fd == -1) {
    perror("mkstemp");
    exit(1);
  }
  write(fd, "hello", 5);
  close(fd);
  unlink(tfile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <tmpfile@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    0xe68(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe65(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe62(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe60(%rip),%al
mov    %al,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
call   10a0 <mkstemp@plt>
mov    %eax,-0x34(%rbp)
lea    -0x30(%rbp),%rsi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0xffffffff,-0x34(%rbp)
jne    1249 <main+0xa9>
lea    0xdfd(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x34(%rbp),%edi
lea    0xdec(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xe69(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1090 <tmpfile@plt>
mov    %rax,%rbx
lea    0xe2c(%rip),%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
movups 0xe64(%rip),%xmm0
movups %xmm0,0x9(%rsp)
movaps 0xe4f(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
call   10a0 <mkstemp@plt>
mov    %eax,%ebx
lea    0xdf3(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0xffffffff,%ebx
je     1254 <main+0xa4>
lea    0xdf2(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1050 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdbe(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"Creating a temporary file...\0A\00"
@.str.1 = [12 x i8] c"Hello world\00"
@__const.main.tfile = [25 x i8] c"/tmp/my-temp-file-XXXXXX\00"
@.str.2 = [13 x i8] c"Created: %s\0A\00"
@.str.3 = [8 x i8] c"mkstemp\00"
@.str.4 = [6 x i8] c"hello\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [25 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%6 = call i32* (...) @tmpfile()
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32*, i32** %2
%10 = call i32 @fclose(i32* %9)
%11 = bitcast [25 x i8]* %3 to i8*
%12 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%13 = call i32 @mkstemp(i8* %12)
store i32 %13, i32* %4
%14 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %0
%22 = load i32, i32* %4
%23 = call i32 @write(i32 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%27 = call i32 @unlink(i8* %26)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"Hello world\00"
@__const.main.tfile = [25 x i8] c"/tmp/my-temp-file-XXXXXX\00"
@.str.2 = [13 x i8] c"Created: %s\0A\00"
@.str.3 = [8 x i8] c"mkstemp\00"
@.str.4 = [6 x i8] c"hello\00"
@str = [29 x i8] c"Creating a temporary file...\00"
define i32 @main() {
%1 = alloca [25 x i8]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%3 = tail call i32* (...) @tmpfile()
%4 = tail call i64 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 11, i64 1, i32* %3)
%5 = tail call i32 @fclose(i32* %3)
%6 = getelementptr inbounds [25 x i8], [25 x i8]* %1, i64 0, i64 0
%7 = call i32 @mkstemp(i8* %6)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%9 = icmp eq i32 %7, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 @write(i32 %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%15 = call i32 @close(i32 %7)
%16 = call i32 @unlink(i8* %6)
ret i32 0
}