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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  const char *file = "test.tmp";
  ssize_t bytes_read = -1;
  char buf[4] = "zzz\n";
  int i = 0;
  FILE *stream;
  stream = fopen(file, "w+");
  if (!stream) {
    perror("fopen");
  }
  if (!fwrite("abcdef", sizeof(char), 6, stream)) {
    perror("fwrite");
  }
  if (!fseek(stream, 1, SEEK_SET)) {
    perror("fseek");
  }
  if (!fread(buf, sizeof(char), 3, stream)) {
    perror("fwrite");
  }
  if (fclose(stream)) {
    perror("fclose");
  }
  printf("Result: %s\n", buf);
  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)
lea    0xe73(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0xe9b(%rip),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d3 <main+0x63>
lea    0xe3e(%rip),%rdi
call   1080 <perror@plt>
mov    -0x30(%rbp),%rcx
lea    0xe34(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
call   1090 <fwrite@plt>
cmp    $0x0,%eax
jne    1202 <main+0x92>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
mov    -0x30(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1060 <fseek@plt>
cmp    $0x0,%eax
jne    122e <main+0xbe>
lea    0xdf7(%rip),%rdi
call   1080 <perror@plt>
lea    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1030 <fread@plt>
cmp    $0x0,%eax
jne    125a <main+0xea>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1279 <main+0x109>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
lea    -0x20(%rbp),%rsi
lea    0xda9(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0xa7a7a7a,0xc(%rsp)
0a
lea    0xe7c(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1210 <main+0xa0>
lea    0xe73(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
test   %eax,%eax
je     1221 <main+0xb1>
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1060 <fseek@plt>
test   %eax,%eax
je     122f <main+0xbf>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     123d <main+0xcd>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    124b <main+0xdb>
lea    0xe2f(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
jmp    1198 <main+0x28>
lea    0xdf1(%rip),%rdi
call   1080 <perror@plt>
jmp    11b5 <main+0x45>
lea    0xdea(%rip),%rdi
call   1080 <perror@plt>
jmp    11cf <main+0x5f>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
jmp    11ea <main+0x7a>
lea    0xdd4(%rip),%rdi
call   1080 <perror@plt>
jmp    11f7 <main+0x87>

LLVM IR (-O0):
@.str = [9 x i8] c"test.tmp\00"
@__const.main.buf = [4 x i8] c"zzz\0A"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"abcdef\00"
@.str.4 = [7 x i8] c"fwrite\00"
@SEEK_SET = global i32 0
@.str.5 = [6 x i8] c"fseek\00"
@.str.6 = [7 x i8] c"fclose\00"
@.str.7 = [12 x i8] c"Result: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [4 x i8]
%9 = alloca i32
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %6
store i32 -1, i32* %7
%11 = bitcast [4 x i8]* %8 to i8*
store i32 0, i32* %9
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp ne i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32*, i32** %10
%20 = call i32 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 6, i32* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32*, i32** %10
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 1, i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%33 = load i32*, i32** %10
%34 = call i32 @fread(i8* %32, i32 1, i32 3, i32* %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %31
%39 = load i32*, i32** %10
%40 = call i64 @fclose(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%46 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i8* %45)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.tmp\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"abcdef\00"
@.str.4 = [7 x i8] c"fwrite\00"
@SEEK_SET = global i32 0
@.str.5 = [6 x i8] c"fseek\00"
@.str.6 = [7 x i8] c"fclose\00"
@.str.7 = [12 x i8] c"Result: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 175798906, i32* %3
%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 %9
9: ; preds = %7, %2
%10 = tail call i32 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 6, i32* %5)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %5, i32 1, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = call i32 @fread(i8* %4, i32 1, i32 3, i32* %5)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = tail call i64 @fclose(i32* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i8* %4)
ret i32 0
}