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

/* Variables and functions */
int /*<<< orphan*/ _IOFBF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(int *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memset(int *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int setvbuf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ util_hex_dump(char *, char *, int);
int main(int argc, char *argv[]) {
  int i;
  char *pbuf;
  int ret;
  FILE *file;
  uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
  size_t wsize;
  if (argc < 2) {
    printf("Usage: %s filename\n", argv[0]);
    exit(1);
  }
  pbuf = malloc(0x20);
  file = fopen(argv[1], "wb+");
  if (file == NULL) {
    perror("fopen error");
    exit(1);
  }
  ret = setvbuf(file, pbuf, _IOFBF, 0x20);
  if (ret != 0) {
    perror("setvbuf error");
    exit(1);
  }
  for (i = 0; i < 2; ++i) {
    memset(data, (uint8_t)(i + 1), sizeof(data));
    wsize = fwrite(data, sizeof(data), 1, file);
    if (wsize == -1) {
      perror("fwrite error");
      exit(1);
    } else {
      util_hex_dump("buf", pbuf, 0x20);
      printf("fwrite return %d\n", wsize);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe53(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe50(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e5 <main+0x55>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe22(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122c <main+0x9c>
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
mov    $0x20,%ecx
call   1070 <setvbuf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     126a <main+0xda>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jge    1305 <main+0x175>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x10,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    12cb <main+0x13b>
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xd79(%rip),%rdi
mov    $0x20,%edx
call   1050 <util_hex_dump@plt>
mov    -0x48(%rbp),%rsi
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12f7 <main+0x167>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1271 <main+0xe1>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movaps 0xe5d(%rip),%xmm0
movaps %xmm0,(%rsp)
cmp    $0x1,%edi
jle    12c3 <main+0x133>
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x8(%rbx),%rdi
lea    0xe5c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12de <main+0x14e>
mov    %rax,%r15
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x20,%ecx
call   1070 <setvbuf@plt>
test   %eax,%eax
jne    12e7 <main+0x157>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10b0 <fwrite@plt>
cmp    $0xffffffffffffffff,%rax
je     12ba <main+0x12a>
mov    %rax,%rbx
lea    0xe17(%rip),%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1050 <util_hex_dump@plt>
lea    0xe07(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10b0 <fwrite@plt>
cmp    $0xffffffffffffffff,%rax
je     12ba <main+0x12a>
mov    %rax,%rbx
lea    0xdbf(%rip),%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1050 <util_hex_dump@plt>
lea    0xdaf(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd81(%rip),%rdi
jmp    12ee <main+0x15e>
mov    (%rbx),%rsi
lea    0xd43(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd43(%rip),%rdi
jmp    12ee <main+0x15e>
lea    0xd46(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.data = [4 x i32] [i32 1, i32 2, i32 3, i32 4]
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@.str.1 = [4 x i8] c"wb+\00"
@.str.2 = [12 x i8] c"fopen error\00"
@_IOFBF = global i32 0
@.str.3 = [14 x i8] c"setvbuf error\00"
@.str.4 = [13 x i8] c"fwrite error\00"
@.str.5 = [4 x i8] c"buf\00"
@.str.6 = [18 x i8] c"fwrite return %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [4 x i32]
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [4 x i32]* %10 to i8*
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = call i8* @malloc(i32 32)
store i8* %22, i8** %7
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %9
%27 = load i32*, i32** %9
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i32*, i32** %9
%34 = load i8*, i8** %7
%35 = load i32, i32* @_IOFBF
%36 = call i32 @setvbuf(i32* %33, i8* %34, i32 %35, i32 32)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %32
store i32 0, i32* %6
br label %43
43: ; preds = %65, %42
%44 = load i32, i32* %6
%45 = icmp slt i32 %44, 2
br i1 %45, label %46, label %68
46: ; preds = %43
%47 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 0
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
%50 = call i32 @memset(i32* %47, i32 %49, i32 16)
%51 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 0
%52 = load i32*, i32** %9
%53 = call i64 @fwrite(i32* %51, i32 16, i32 1, i32* %52)
store i64 %53, i64* %11
%54 = load i64, i64* %11
%55 = icmp eq i64 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %46
%57 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %46
%60 = load i8*, i8** %7
%61 = call i32 @util_hex_dump(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %60, i32 32)
%62 = load i64, i64* %11
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i64 %62)
br label %64
64: ; preds = %59
br label %65
65: ; preds = %64
%66 = load i32, i32* %6
%67 = add nsw i32 %66, 1
store i32 %67, i32* %6
br label %43
68: ; preds = %43
ret i32 0
}

LLVM IR (-O2):
@__const.main.data = [4 x i32] [i32 1, i32 2, i32 3, i32 4]
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@.str.1 = [4 x i8] c"wb+\00"
@.str.2 = [12 x i8] c"fopen error\00"
@_IOFBF = global i32 0
@.str.3 = [14 x i8] c"setvbuf error\00"
@.str.4 = [13 x i8] c"fwrite error\00"
@.str.5 = [4 x i8] c"buf\00"
@.str.6 = [18 x i8] c"fwrite return %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i32]
%4 = bitcast [4 x i32]* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = load i32, i32* @_IOFBF
%21 = tail call i32 @setvbuf(i32* %14, i8* %11, i32 %20, i32 32)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%25 = call i32 @memset(i32* %24, i32 1, i32 16)
%26 = call i64 @fwrite(i32* %24, i32 16, i32 1, i32* %14)
%27 = icmp eq i64 %26, -1
br i1 %27, label %31, label %34
28: ; preds = %19
%29 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %34, %23
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %23
%35 = call i32 @util_hex_dump(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %11, i32 32)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i64 %26)
%37 = call i32 @memset(i32* %24, i32 2, i32 16)
%38 = call i64 @fwrite(i32* %24, i32 16, i32 1, i32* %14)
%39 = icmp eq i64 %38, -1
br i1 %39, label %31, label %40
40: ; preds = %34
%41 = call i32 @util_hex_dump(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %11, i32 32)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i64 %38)
ret i32 0
}