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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int fputs(char *, int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *tmpfile();
int main(int argc, char *argv[]) {
  FILE *tmp = tmpfile();
  if (tmp == NULL) {
    perror("tmpfile");
    return -1;
  }
  int ii = fputs("hello world\n", tmp);
  printf("fputs %d\n", ii);
  fputs("hello world\n", tmp);
  size_t i = fwrite("hello world", 1, 10, tmp);
  printf("fwrite %zu\n", i);
  if (i == -1) {
    perror("fwrite");
    return errno;
  }
  char content[64] = {};
  fseek(tmp, 0, SEEK_SET);
  i = fread(content, 1, sizeof(content), tmp);
  printf("fread %zu\n", i);
  if (i == -1) {
    perror("fread");
    return errno;
  }
  printf("content %s\n", content);
  char *res = fgets(content, sizeof(content), tmp);
  if (res == NULL) {
    perror("fgets");
    return -1;
  }
  printf("content %s\n", content);
  fclose(tmp);
  fread(content, 1, 20, tmp);
  printf("content %s\n", content);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <tmpfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e7 <main+0x47>
lea    0xe2a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1382 <main+0x1e2>
mov    -0x18(%rbp),%rsi
lea    0xe16(%rip),%rdi
call   1060 <fputs@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xdf2(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x18(%rbp),%rcx
lea    0xdf9(%rip),%rdi
mov    $0x1,%esi
mov    $0xa,%edx
call   10c0 <fwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1273 <main+0xd3>
lea    0xdda(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1382 <main+0x1e2>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x40,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    12ec <main+0x14c>
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1382 <main+0x1e2>
lea    -0x70(%rbp),%rsi
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x40,%esi
call   1080 <fgets@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    1337 <main+0x197>
lea    0xd35(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1382 <main+0x1e2>
lea    -0x70(%rbp),%rsi
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x14,%edx
call   1030 <fread@plt>
lea    -0x70(%rbp),%rsi
lea    0xcdb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
xor    %eax,%eax
call   1080 <tmpfile@plt>
test   %rax,%rax
je     12f2 <main+0x162>
mov    %rax,%rbx
lea    0xe54(%rip),%rbp
mov    %rbp,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
lea    0xe4f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rbp,%rdi
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
lea    0xe34(%rip),%rdi
mov    $0x1,%esi
mov    $0xa,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rax,%rbp
lea    0xe24(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xffffffffffffffff,%rbp
je     12fb <main+0x16b>
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x40,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,%rbp
lea    0xddb(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xffffffffffffffff,%rbp
je     1304 <main+0x174>
lea    0xdd1(%rip),%rdi
xor    %r14d,%r14d
mov    %rsp,%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     131c <main+0x18c>
lea    0xda5(%rip),%r15
mov    %rsp,%rbp
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x14,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd07(%rip),%rdi
jmp    1323 <main+0x193>
lea    0xd35(%rip),%rdi
jmp    130b <main+0x17b>
lea    0xd3e(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%r14d
jmp    12e4 <main+0x154>
lea    0xd38(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    12e4 <main+0x154>

LLVM IR (-O0):
@.str = [8 x i8] c"tmpfile\00"
@.str.1 = [13 x i8] c"hello world\0A\00"
@.str.2 = [10 x i8] c"fputs %d\0A\00"
@.str.3 = [12 x i8] c"hello world\00"
@.str.4 = [12 x i8] c"fwrite %zu\0A\00"
@.str.5 = [7 x i8] c"fwrite\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.6 = [11 x i8] c"fread %zu\0A\00"
@.str.7 = [6 x i8] c"fread\00"
@.str.8 = [12 x i8] c"content %s\0A\00"
@.str.9 = [6 x i8] c"fgets\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i64
%9 = alloca [64 x i8]
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32* (...) @tmpfile()
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32* %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i32*, i32** %6
%22 = call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32* %21)
%23 = load i32*, i32** %6
%24 = call i64 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 10, i32* %23)
store i64 %24, i64* %8
%25 = load i64, i64* %8
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i64 %25)
%27 = load i64, i64* %8
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %16
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @errno
store i32 %31, i32* %3
br label %67
32: ; preds = %16
%33 = bitcast [64 x i8]* %9 to i8*
%34 = load i32*, i32** %6
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @fseek(i32* %34, i32 0, i32 %35)
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%38 = load i32*, i32** %6
%39 = call i64 @fread(i8* %37, i32 1, i32 64, i32* %38)
store i64 %39, i64* %8
%40 = load i64, i64* %8
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i64 %40)
%42 = load i64, i64* %8
%43 = icmp eq i64 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %32
%45 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%46 = load i32, i32* @errno
store i32 %46, i32* %3
br label %67
47: ; preds = %32
%48 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %48)
%50 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%51 = load i32*, i32** %6
%52 = call i8* @fgets(i8* %50, i32 64, i32* %51)
store i8* %52, i8** %10
%53 = load i8*, i8** %10
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %57
55: ; preds = %47
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
57: ; preds = %47
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %58)
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
%62 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%63 = load i32*, i32** %6
%64 = call i64 @fread(i8* %62, i32 1, i32 20, i32* %63)
%65 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %65)
store i32 0, i32* %3
br label %67
67: ; preds = %57, %55, %44, %29, %14
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [8 x i8] c"tmpfile\00"
@.str.1 = [13 x i8] c"hello world\0A\00"
@.str.2 = [10 x i8] c"fputs %d\0A\00"
@.str.3 = [12 x i8] c"hello world\00"
@.str.4 = [12 x i8] c"fwrite %zu\0A\00"
@.str.5 = [7 x i8] c"fwrite\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.6 = [11 x i8] c"fread %zu\0A\00"
@.str.7 = [6 x i8] c"fread\00"
@.str.8 = [12 x i8] c"content %s\0A\00"
@.str.9 = [6 x i8] c"fgets\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x i8]
%4 = tail call i32* (...) @tmpfile()
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %41
8: ; preds = %2
%9 = tail call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = tail call i64 bitcast (i64 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64 12, i64 1, i32* %4)
%12 = tail call i64 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 10, i32* %4)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i64 %12)
%14 = icmp eq i64 %12, -1
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%17 = load i32, i32* @errno
br label %41
18: ; preds = %8
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @fseek(i32* %4, i32 0, i32 %20)
%22 = call i64 @fread(i8* %19, i32 1, i32 64, i32* %4)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i64 %22)
%24 = icmp eq i64 %22, -1
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%27 = load i32, i32* @errno
br label %39
28: ; preds = %18
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %19)
%30 = call i8* @fgets(i8* %19, i32 64, i32* %4)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %39
34: ; preds = %28
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %19)
%36 = call i32 @fclose(i32* %4)
%37 = call i64 @fread(i8* %19, i32 1, i32 20, i32* %4)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %19)
br label %39
39: ; preds = %32, %34, %25
%40 = phi i32 [ %27, %25 ], [ -1, %32 ], [ 0, %34 ]
br label %41
41: ; preds = %15, %39, %6
%42 = phi i32 [ -1, %6 ], [ %17, %15 ], [ %40, %39 ]
ret i32 %42
}