#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*/ EXIT_FAILURE;
int /*<<< orphan*/ _IOFBF;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setvbuf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/, int);
void write_data(int buf_size) {
  FILE *file;
  char *buf;
  int i = 0;
  file = fopen("foo.txt", "w+");
  if (file != NULL) {
    buf = (char *)malloc(sizeof(char) * buf_size);
    if (setvbuf(file, buf, _IOFBF, buf_size) != 0) {
      perror("Error on setvbuf,");
      exit(EXIT_FAILURE);
    }
    for (i = 0; i <= 100000000; i++) {
      fputc('0', file);
    }
    free(buf);
    fclose(file);
  } else {
    perror("Error on fopen,");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe68(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     124b <write_data+0xcb>
movslq -0x4(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    -0x4(%rbp),%ecx
call   1070 <setvbuf@plt>
cmp    $0x0,%rax
je     1204 <write_data+0x84>
lea    0xe1a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5f5e100,-0x1c(%rbp)
jg     1234 <write_data+0xb4>
mov    -0x10(%rbp),%rsi
mov    $0x30,%edi
call   1050 <fputc@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120b <write_data+0x8b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1265 <write_data+0xe5>
lea    0xdcb(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
lea    0xe73(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f6 <write_data+0x76>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%ecx
call   1070 <setvbuf@plt>
test   %rax,%rax
jne    11ff <write_data+0x7f>
mov    $0x5f5e101,%ebp
nopl   0x0(%rax,%rax,1)
mov    $0x30,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0xffffffff,%ebp
jne    11d0 <write_data+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe20(%rip),%rdi
jmp    1206 <write_data+0x86>
lea    0xe05(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"foo.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@_IOFBF = global i32 0
@.str.2 = [18 x i8] c"Error on setvbuf,\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [16 x i8] c"Error on fopen,\00"
define void @write_data(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
%6 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %41
9: ; preds = %1
%10 = load i32, i32* %2
%11 = sext i32 %10 to i64
%12 = mul i64 1, %11
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %4
%16 = load i32*, i32** %3
%17 = load i8*, i8** %4
%18 = load i32, i32* @_IOFBF
%19 = load i32, i32* %2
%20 = call i64 @setvbuf(i32* %16, i8* %17, i32 %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %9
%23 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %9
store i32 0, i32* %5
br label %27
27: ; preds = %33, %26
%28 = load i32, i32* %5
%29 = icmp sle i32 %28, 100000000
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = load i32*, i32** %3
%32 = call i32 @fputc(i32 48, i32* %31)
br label %33
33: ; preds = %30
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %27
36: ; preds = %27
%37 = load i8*, i8** %4
%38 = call i32 @free(i8* %37)
%39 = load i32*, i32** %3
%40 = call i32 @fclose(i32* %39)
br label %45
41: ; preds = %1
%42 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %36
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"foo.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@_IOFBF = global i32 0
@.str.2 = [18 x i8] c"Error on setvbuf,\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [16 x i8] c"Error on fopen,\00"
define void @write_data(i32 %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = tail call i64 @malloc(i32 %0)
%6 = inttoptr i64 %5 to i8*
%7 = load i32, i32* @_IOFBF
%8 = tail call i64 @setvbuf(i32* %2, i8* %6, i32 %7, i32 %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4, %14
%15 = phi i32 [ %17, %14 ], [ 0, %4 ]
%16 = tail call i32 @fputc(i32 48, i32* %2)
%17 = add nuw nsw i32 %15, 1
%18 = icmp eq i32 %17, 100000001
br i1 %18, label %19, label %14
19: ; preds = %14
%20 = tail call i32 @free(i8* %6)
%21 = tail call i32 @fclose(i32* %2)
ret void
22: ; preds = %1
%23 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
}