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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strlen(char *);
int main(int argc, char *argv[]) {
  FILE *file = fopen("test.txt", "w+");
  if (file == NULL) {
    perror("fopen");
    return -1;
  }
  fputs("hello world by fputs\n", file);
  fseek(file, 0, SEEK_SET);
  char content[128] = {};
  fgets(content, sizeof(content), file);
  printf("fgets => %s\n", content);
  char *str = "hello by fwrite\n";
  fwrite(str, strlen(str), 1, file);
  fseek(file, (long)strlen(content), SEEK_SET);
  char str2[128];
  fread(str2, sizeof(str2), 1, file);
  printf("fread => %s\n", str2);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
lea    0xe32(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1203 <main+0x53>
lea    0xe1a(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <main+0x160>
mov    -0x18(%rbp),%rsi
lea    0xe04(%rip),%rdi
call   1070 <fputs@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x80,%esi
call   1090 <fgets@plt>
lea    -0xa0(%rbp),%rsi
lea    0xdc9(%rip),%rdi
call   1060 <printf@plt>
lea    0xdca(%rip),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    -0xa8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x140(%rbp),%rdi
mov    %rax,%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x138(%rbp),%rdi
mov    %rax,%rsi
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x80,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
lea    -0x130(%rbp),%rsi
lea    0xd4b(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
lea    0xe5b(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12b0 <main+0x120>
mov    %rax,%rbx
lea    0xe4e(%rip),%rdi
mov    $0x15,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    0x2e03(%rip),%r12
mov    (%r12),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
lea    0xdf6(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
lea    0xdf4(%rip),%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1080 <fseek@plt>
lea    0x80(%rsp),%r15
mov    %r15,%rdi
mov    $0x80,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xdb8(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd55(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    129e <main+0x10e>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [22 x i8] c"hello world by fputs\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"fgets => %s\0A\00"
@.str.5 = [17 x i8] c"hello by fwrite\0A\00"
@.str.6 = [13 x i8] c"fread => %s\0A\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]
%8 = alloca i8*
%9 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %44
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = call i32 @fputs(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32* %16)
%18 = load i32*, i32** %6
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %18, i64 0, i32 %19)
%21 = bitcast [128 x i8]* %7 to i8*
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%23 = load i32*, i32** %6
%24 = call i32 @fgets(i8* %22, i32 128, i32* %23)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%26 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %25)
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8** %8
%27 = load i8*, i8** %8
%28 = load i8*, i8** %8
%29 = call i64 @strlen(i8* %28)
%30 = load i32*, i32** %6
%31 = call i32 @fwrite(i8* %27, i64 %29, i32 1, i32* %30)
%32 = load i32*, i32** %6
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%34 = call i64 @strlen(i8* %33)
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @fseek(i32* %32, i64 %34, i32 %35)
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%38 = load i32*, i32** %6
%39 = call i32 @fread(i8* %37, i32 128, i32 1, i32* %38)
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %40)
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
store i32 0, i32* %3
br label %44
44: ; preds = %15, %13
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [22 x i8] c"hello world by fputs\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"fgets => %s\0A\00"
@.str.5 = [17 x i8] c"hello by fwrite\0A\00"
@.str.6 = [13 x i8] c"fread => %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = alloca [128 x i8]
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %24
9: ; preds = %2
%10 = tail call i64 bitcast (i32 (i8*, i64, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 21, i64 1, i32* %5)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %5, i64 0, i32 %11)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%14 = call i32 @fgets(i8* %13, i32 128, i32* %5)
%15 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%16 = call i32 @fwrite(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i64 16, i32 1, i32* %5)
%17 = call i64 @strlen(i8* %13)
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %5, i64 %17, i32 %18)
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%21 = call i32 @fread(i8* %20, i32 128, i32 1, i32* %5)
%22 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %20)
%23 = call i32 @fclose(i32* %5)
br label %24
24: ; preds = %9, %7
%25 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %25
}