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

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char *);
int main() {
  FILE *fd = NULL;
  char buff[100];
  memset(buff, 0, sizeof(buff));
  fd = fopen("test.txt", "rw+");
  if (NULL == fd) {
    printf(" fopen() error \n");
    return 1;
  }
  printf("\n file opened successfully through fopen()\n");
  if (1 * 5 != fread(buff, 1, 5, fd)) {
    printf(" failed\n");
    return 1;
  }
  printf("some bytes read through fread\n");
  printf("\n The bytes read are [%s]\n", buff);
  if (0 != fseek(fd, 10, SEEK_CUR)) {
    printf("\n fseek() failed\n");
    return 1;
  }
  printf("\n fseek() successful\n");
  if (1 * 5 != fwrite(buff, 1, strlen(buff), fd)) {
    printf("\n fwrite() failed\n");
    return 1;
  }
  printf("\n fwrite() successful, data written to text file\n");
  fclose(fd);
  printf("\n File stream closed through fclose()\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
lea    0xe4f(%rip),%rdi
lea    0xe51(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11e7 <main+0x67>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1322 <main+0x1a2>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x5,%edx
call   1030 <fread@plt>
mov    %eax,%ecx
mov    $0x5,%eax
cmp    %ecx,%eax
je     1235 <main+0xb5>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1322 <main+0x1a2>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   1080 <fseek@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1294 <main+0x114>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1322 <main+0x1a2>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    %eax,%ecx
mov    $0x5,%eax
cmp    %ecx,%eax
je     12f6 <main+0x176>
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1322 <main+0x1a2>
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1080 <memset@plt>
lea    0xe52(%rip),%rdi
lea    0xe54(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     122e <main+0x9e>
mov    %rax,%rbx
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x5,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x5,%eax
jne    123c <main+0xac>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe0d(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1090 <fseek@plt>
test   %rax,%rax
je     1253 <main+0xc3>
lea    0xec3(%rip),%rdi
jmp    1243 <main+0xb3>
mov    $0x1,%ebp
lea    0xec6(%rip),%rdi
jmp    1243 <main+0xb3>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    $0x1,%ebp
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
cmp    $0x5,%eax
jne    12a5 <main+0x115>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebp,%ebp
lea    0xe14(%rip),%rdi
jmp    1243 <main+0xb3>
lea    0xe31(%rip),%rdi
jmp    1243 <main+0xb3>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [4 x i8] c"rw+\00"
@.str.2 = [17 x i8] c" fopen() error \0A\00"
@.str.3 = [44 x i8] c"\0A file opened successfully through fopen()\0A\00"
@.str.4 = [9 x i8] c" failed\0A\00"
@.str.5 = [31 x i8] c"some bytes read through fread\0A\00"
@.str.6 = [27 x i8] c"\0A The bytes read are [%s]\0A\00"
@SEEK_CUR = global i32 0
@.str.7 = [18 x i8] c"\0A fseek() failed\0A\00"
@.str.8 = [22 x i8] c"\0A fseek() successful\0A\00"
@.str.9 = [19 x i8] c"\0A fwrite() failed\0A\00"
@.str.10 = [50 x i8] c"\0A fwrite() successful, data written to text file\0A\00"
@.str.11 = [39 x i8] c"\0A File stream closed through fclose()\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [100 x i8]
store i32 0, i32* %1
store i32* null, i32** %2
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 100)
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* null, %7
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %44
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i32 @fread(i8* %13, i32 1, i32 5, i32* %14)
%16 = icmp ne i32 5, %15
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %44
19: ; preds = %11
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %2
%24 = load i32, i32* @SEEK_CUR
%25 = call i64 @fseek(i32* %23, i32 10, i32 %24)
%26 = icmp ne i64 0, %25
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %44
29: ; preds = %19
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0))
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = load i32*, i32** %2
%35 = call i32 @fwrite(i8* %31, i32 1, i32 %33, i32* %34)
%36 = icmp ne i32 5, %35
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %1
br label %44
39: ; preds = %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.10, i64 0, i64 0))
%41 = load i32*, i32** %2
%42 = call i32 @fclose(i32* %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.11, i64 0, i64 0))
store i32 0, i32* %1
br label %44
44: ; preds = %39, %37, %27, %17, %9
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [4 x i8] c"rw+\00"
@.str.6 = [27 x i8] c"\0A The bytes read are [%s]\0A\00"
@SEEK_CUR = global i32 0
@str = [43 x i8] c"\0A file opened successfully through fopen()\00"
@str.12 = [30 x i8] c"some bytes read through fread\00"
@str.13 = [21 x i8] c"\0A fseek() successful\00"
@str.14 = [49 x i8] c"\0A fwrite() successful, data written to text file\00"
@str.15 = [38 x i8] c"\0A File stream closed through fclose()\00"
@str.16 = [18 x i8] c"\0A fwrite() failed\00"
@str.17 = [17 x i8] c"\0A fseek() failed\00"
@str.18 = [8 x i8] c" failed\00"
@str.19 = [16 x i8] c" fopen() error \00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 100)
%4 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %24, label %6
6: ; preds = %0
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%8 = call i32 @fread(i8* %2, i32 1, i32 5, i32* %4)
%9 = icmp eq i32 %8, 5
br i1 %9, label %10, label %24
10: ; preds = %6
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.12, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%13 = load i32, i32* @SEEK_CUR
%14 = call i64 @fseek(i32* %4, i32 10, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %10
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.13, i64 0, i64 0))
%18 = call i32 @strlen(i8* %2)
%19 = call i32 @fwrite(i8* %2, i32 1, i32 %18, i32* %4)
%20 = icmp eq i32 %19, 5
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.14, i64 0, i64 0))
%23 = call i32 @fclose(i32* %4)
br label %24
24: ; preds = %16, %10, %6, %0, %21
%25 = phi i8* [ getelementptr inbounds ([38 x i8], [38 x i8]* @str.15, i64 0, i64 0), %21 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.19, i64 0, i64 0), %0 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @str.18, i64 0, i64 0), %6 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.17, i64 0, i64 0), %10 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @str.16, i64 0, i64 0), %16 ]
%26 = phi i32 [ 0, %21 ], [ 1, %0 ], [ 1, %6 ], [ 1, %10 ], [ 1, %16 ]
%27 = call i32 @puts(i8* dereferenceable(1) %25)
ret i32 %26
}