#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, long);
scalar_t__ getpid();
int mkstemp(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  char tf_path[64];
  int tfd = -1;
  FILE *in = NULL;
  strcpy(tf_path, "/tmp/01-XXXXXX");
  if ((tfd = mkstemp(tf_path)) < 0) {
    perror(tf_path);
    return 0;
  }
  printf("using temp file: '%s'\n", tf_path);
  in = fdopen(tfd, "w+");
  if (in == NULL) {
    perror(tf_path);
    return 0;
  }
  fprintf(in, "Written by PID=%ld\n", (long)getpid());
  fclose(in);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x54(%rbp)
movq   $0x0,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x50(%rbp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jge    11df <main+0x5f>
lea    -0x50(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1254 <main+0xd4>
lea    -0x50(%rbp),%rsi
lea    0xe25(%rip),%rdi
call   1060 <printf@plt>
mov    -0x54(%rbp),%edi
lea    0xe2d(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    1222 <main+0xa2>
lea    -0x50(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1254 <main+0xd4>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    %rax,%rdx
lea    0xdea(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x60(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x40,%rsp
lea    0xe74(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1080 <mkstemp@plt>
test   %eax,%eax
js     11f3 <main+0x73>
mov    %eax,%ebx
lea    0xe63(%rip),%rdi
mov    %rsp,%rsi
call   1060 <printf@plt>
lea    0xe6b(%rip),%rsi
mov    %ebx,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     11f3 <main+0x73>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe51(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
ret
mov    %rsp,%rdi
call   10a0 <perror@plt>
jmp    11eb <main+0x6b>

LLVM IR (-O0):
@.str = [15 x i8] c"/tmp/01-XXXXXX\00"
@.str.1 = [23 x i8] c"using temp file: '%s'\0A\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [20 x i8] c"Written by PID=%ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [64 x i8]
%7 = alloca i32
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %7
store i32* null, i32** %8
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%10 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%12 = call i32 @mkstemp(i8* %11)
store i32 %12, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%16 = call i32 @perror(i8* %15)
store i32 0, i32* %3
br label %33
17: ; preds = %2
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%19 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %7
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%26 = call i32 @perror(i8* %25)
store i32 0, i32* %3
br label %33
27: ; preds = %17
%28 = load i32*, i32** %8
%29 = call i64 (...) @getpid()
%30 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %29)
%31 = load i32*, i32** %8
%32 = call i32 @fclose(i32* %31)
store i32 0, i32* %3
br label %33
33: ; preds = %27, %24, %14
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [15 x i8] c"/tmp/01-XXXXXX\00"
@.str.1 = [23 x i8] c"using temp file: '%s'\0A\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [20 x i8] c"Written by PID=%ld\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @mkstemp(i8* %4)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @perror(i8* %4)
br label %20
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%12 = call i32* @fdopen(i32 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* %4)
br label %20
16: ; preds = %10
%17 = call i64 (...) @getpid()
%18 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %17)
%19 = call i32 @fclose(i32* %12)
br label %20
20: ; preds = %16, %14, %8
ret i32 0
}