#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, long);
int /*<<< orphan*/ free(char *);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *tempnam(char *, char *);
int main(int argc, char *argv[]) {
  char *tf_path = NULL;
  FILE *tin = NULL;
  if ((tf_path = tempnam("./my_tmp", "tmp-")) == NULL) {
    perror(tf_path);
    return 0;
  }
  printf("temp. file name is '%s'\n", tf_path);
  if ((tin = fopen(tf_path, "w+")) == NULL) {
    perror(tf_path);
    return 0;
  }
  fprintf(tin, "PID %ld was here.\n", (long)getpid());
  fclose(tin);
  free(tf_path);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe53(%rip),%rdi
lea    0xe55(%rip),%rsi
call   1080 <tempnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11dc <main+0x5c>
mov    -0x18(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    125a <main+0xda>
mov    -0x18(%rbp),%rsi
lea    0xe27(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    121f <main+0x9f>
mov    -0x18(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    125a <main+0xda>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rdx
lea    0xdee(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe75(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1080 <tempnam@plt>
test   %rax,%rax
je     11f8 <main+0x78>
mov    %rax,%r14
lea    0xe68(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
lea    0xe72(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1201 <main+0x81>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
call   10a0 <perror@plt>
jmp    11ee <main+0x6e>
mov    %r14,%rdi
call   10a0 <perror@plt>
jmp    11ee <main+0x6e>

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

LLVM IR (-O2):
@.str = [9 x i8] c"./my_tmp\00"
@.str.1 = [5 x i8] c"tmp-\00"
@.str.2 = [25 x i8] c"temp. file name is '%s'\0A\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [19 x i8] c"PID %ld was here.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @tempnam(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* null)
br label %18
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%9 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* %3)
br label %18
13: ; preds = %7
%14 = tail call i64 (...) @getpid()
%15 = tail call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %14)
%16 = tail call i32 @fclose(i32* %9)
%17 = tail call i32 @free(i8* %3)
br label %18
18: ; preds = %13, %11, %5
ret i32 0
}