#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, long);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ *tmpfile();
int main(int argc, char *argv[]) {
  FILE *tin = NULL;
  char buf[128];
  if ((tin = tmpfile()) == NULL) {
    perror("tmpfile()");
    return 0;
  }
  fprintf(tin, "PID %ld was here.\n", (long)getpid());
  fflush(tin);
  rewind(tin);
  fgets(buf, sizeof(buf), tin);
  printf("GOT: %s", buf);
  fclose(tin);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1080 <tmpfile@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11de <main+0x4e>
lea    0xe33(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1250 <main+0xc0>
mov    -0x18(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0xa8(%rbp),%rdi
mov    %rax,%rdx
lea    0xe09(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <fflush@plt>
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x80,%esi
call   1070 <fgets@plt>
lea    -0xa0(%rbp),%rsi
lea    0xde2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x88,%rsp
xor    %eax,%eax
call   1080 <tmpfile@plt>
test   %rax,%rax
je     1209 <main+0x79>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe53(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   10a0 <fflush@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
lea    0xe31(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf0(%rip),%rdi
call   10b0 <perror@plt>
jmp    11fc <main+0x6c>

LLVM IR (-O0):
@.str = [10 x i8] c"tmpfile()\00"
@.str.1 = [19 x i8] c"PID %ld was here.\0A\00"
@.str.2 = [8 x i8] c"GOT: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
%8 = call i32* (...) @tmpfile()
store i32* %8, i32** %6
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %27
12: ; preds = %2
%13 = load i32*, i32** %6
%14 = call i64 (...) @getpid()
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i64 %14)
%16 = load i32*, i32** %6
%17 = call i32 @fflush(i32* %16)
%18 = load i32*, i32** %6
%19 = call i32 @rewind(i32* %18)
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%21 = load i32*, i32** %6
%22 = call i32 @fgets(i8* %20, i32 128, i32* %21)
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%24 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
store i32 0, i32* %3
br label %27
27: ; preds = %12, %10
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [10 x i8] c"tmpfile()\00"
@.str.1 = [19 x i8] c"PID %ld was here.\0A\00"
@.str.2 = [8 x i8] c"GOT: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = tail call i32* (...) @tmpfile()
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %17
9: ; preds = %2
%10 = tail call i64 (...) @getpid()
%11 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i64 %10)
%12 = tail call i32 @fflush(i32* %5)
%13 = tail call i32 @rewind(i32* %5)
%14 = call i32 @fgets(i8* %4, i32 128, i32* %5)
%15 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%16 = call i32 @fclose(i32* %5)
br label %17
17: ; preds = %9, %7
ret i32 0
}