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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int stream_io(void) {
  fpos_t fpos;
  int i;
  char buffer[5] = {'b'};
  FILE *fp = fopen("./myfile", "w+");
  if (fp == NULL) {
    perror("Failed to open file \"myfile\"");
    return EXIT_FAILURE;
  }
  for (i = 0; i < 5; i++) {
    fwrite(buffer, 1, 5, fp);
  }
  fprintf(fp, "bbbbb");
  fclose(fp);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<stream_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0xea2(%rip),%eax
mov    %eax,-0x11(%rbp)
mov    0xe9d(%rip),%al
mov    %al,-0xd(%rbp)
lea    0xe94(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a9 <stream_io+0x59>
lea    0xe7e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <stream_io+0xb4>
movl   $0x0,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jge    11df <stream_io+0x8f>
lea    -0x11(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x5,%edx
call   1070 <fwrite@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b0 <stream_io+0x60>
mov    -0x20(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stream_io>:
push   %r14
push   %rbx
push   %rax
movb   $0x0,0x4(%rsp)
movl   $0x62,(%rsp)
lea    0xeae(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     120d <stream_io+0xcd>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
lea    0xe4c(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdfd(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dc0(%rip),%rax
jmp    1203 <stream_io+0xc3>

LLVM IR (-O0):
@__const.stream_io.buffer = [5 x i8] c"b\00\00\00\00"
@.str = [9 x i8] c"./myfile\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [29 x i8] c"Failed to open file \22myfile\22\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [6 x i8] c"bbbbb\00"
@EXIT_SUCCESS = global i32 0
define i32 @stream_io() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [5 x i8]
%5 = alloca i32*
%6 = bitcast [5 x i8]* %4 to i8*
%7 = 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* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
store i32 %12, i32* %1
br label %30
13: ; preds = %0
store i32 0, i32* %3
br label %14
14: ; preds = %21, %13
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 5
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %5
%20 = call i32 @fwrite(i8* %18, i32 1, i32 5, i32* %19)
br label %21
21: ; preds = %17
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %14
24: ; preds = %14
%25 = load i32*, i32** %5
%26 = call i32 @fprintf(i32* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* @EXIT_SUCCESS
store i32 %29, i32* %1
br label %30
30: ; preds = %24, %10
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@__const.stream_io.buffer = [5 x i8] c"b\00\00\00\00"
@.str = [9 x i8] c"./myfile\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [29 x i8] c"Failed to open file \22myfile\22\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [6 x i8] c"bbbbb\00"
@EXIT_SUCCESS = global i32 0
define i32 @stream_io() {
%1 = alloca [5 x i8]
%2 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %13, label %5
5: ; preds = %0
%6 = call i32 @fwrite(i8* %2, i32 1, i32 5, i32* %3)
%7 = call i32 @fwrite(i8* %2, i32 1, i32 5, i32* %3)
%8 = call i32 @fwrite(i8* %2, i32 1, i32 5, i32* %3)
%9 = call i32 @fwrite(i8* %2, i32 1, i32 5, i32* %3)
%10 = call i32 @fwrite(i8* %2, i32 1, i32 5, i32* %3)
%11 = tail call i64 bitcast (i32 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 5, i64 1, i32* %3)
%12 = tail call i32 @fclose(i32* %3)
br label %15
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %5, %13
%16 = phi i32* [ @EXIT_FAILURE, %13 ], [ @EXIT_SUCCESS, %5 ]
%17 = load i32, i32* %16
ret i32 %17
}