#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int main(int argc, char *argv[]) {
  const char *file = "test.tmp";
  FILE *stream;
  const char *buf = "abcdef";
  stream = fopen(file, "w");
  if (!stream) {
    perror("fopen");
  }
  if (!fwrite(buf, sizeof(char), 3, stream)) {
    perror("fwrite");
  }
  if (!fseek(stream, 6, SEEK_SET)) {
    perror("fseek");
  }
  if (!fwrite(buf + 3, sizeof(char), 3, stream)) {
    perror("fwrite");
  }
  if (fclose(stream)) {
    perror("fclose");
  }
  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    0xe93(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a7 <main+0x57>
lea    0xe70(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1070 <fwrite@plt>
cmp    $0x0,%eax
jne    11d3 <main+0x83>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    $0x6,%esi
call   1040 <fseek@plt>
cmp    $0x0,%eax
jne    11ff <main+0xaf>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%rdi
add    $0x3,%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1070 <fwrite@plt>
cmp    $0x0,%eax
jne    122f <main+0xdf>
lea    0xdee(%rip),%rdi
call   1060 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     124e <main+0xfe>
lea    0xddc(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d1 <main+0x81>
lea    0xe96(%rip),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
test   %eax,%eax
je     11df <main+0x8f>
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x6,%esi
call   1040 <fseek@plt>
test   %eax,%eax
je     11ed <main+0x9d>
lea    0xe62(%rip),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
test   %eax,%eax
je     11fb <main+0xab>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jne    1209 <main+0xb9>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
jmp    116c <main+0x1c>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
jmp    1189 <main+0x39>
lea    0xe2b(%rip),%rdi
call   1060 <perror@plt>
jmp    11a3 <main+0x53>
lea    0xe16(%rip),%rdi
call   1060 <perror@plt>
jmp    11c0 <main+0x70>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
jmp    11cd <main+0x7d>

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

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