#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 O_CREAT;
int O_SYNC;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ _IOFBF;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fputc(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setvbuf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/, int);
void write_data(int buf_size) {
  FILE *file;
  int fd;
  char *buf;
  int i = 0;
  fd = open("bar.txt", O_CREAT | O_WRONLY | O_SYNC | O_TRUNC,
            S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
  if (fd != -1) {
    file = fdopen(fd, "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 <= 500000; i++) {
        fputc('0', file);
      }
      free(buf);
      fclose(file);
    } else {
      perror("Error on fdopen,");
    }
  } else {
    perror("Error on open,");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
or     (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
mov    0x2e2c(%rip),%rax
or     (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
mov    0x2dd1(%rip),%rax
or     (%rax),%edx
mov    0x2de0(%rip),%rax
or     (%rax),%edx
mov    0x2ddf(%rip),%rax
or     (%rax),%edx
mov    0x2d86(%rip),%rax
or     (%rax),%edx
lea    0xdfd(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     12db <write_data+0x14b>
mov    -0x14(%rbp),%edi
lea    0xde9(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12ca <write_data+0x13a>
movslq -0x4(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
mov    -0x4(%rbp),%ecx
call   1080 <setvbuf@plt>
cmp    $0x0,%rax
je     1283 <write_data+0xf3>
lea    0xd9a(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x7a120,-0x24(%rbp)
jg     12b3 <write_data+0x123>
mov    -0x10(%rbp),%rsi
mov    $0x30,%edi
call   1050 <fputc@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    128a <write_data+0xfa>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12d6 <write_data+0x146>
lea    0xd4b(%rip),%rdi
call   10a0 <perror@plt>
jmp    12f5 <write_data+0x165>
lea    0xd4b(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e5a(%rip),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
mov    0x2dde(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ddc(%rip),%rax
or     (%rax),%edx
mov    0x2deb(%rip),%rax
or     (%rax),%edx
mov    0x2dea(%rip),%rax
or     (%rax),%edx
mov    %edi,%r14d
mov    0x2d8e(%rip),%rax
or     (%rax),%edx
lea    0xe05(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1292 <write_data+0x102>
lea    0xdf8(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
test   %rax,%rax
je     127c <write_data+0xec>
mov    %rax,%rbx
mov    %r14d,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%ecx
call   1080 <setvbuf@plt>
test   %rax,%rax
jne    129b <write_data+0x10b>
mov    $0x7a121,%ebp
nopl   0x0(%rax,%rax,1)
mov    $0x30,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0xffffffff,%ebp
jne    1250 <write_data+0xc0>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xd99(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <perror@plt>
lea    0xd94(%rip),%rdi
jmp    12a2 <write_data+0x112>
lea    0xd68(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"bar.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_SYNC = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
@.str.1 = [2 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 = [17 x i8] c"Error on fdopen,\00"
@.str.4 = [15 x i8] c"Error on open,\00"
define void @write_data(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %6
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_SYNC
%11 = or i32 %9, %10
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IWGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IWOTH
%24 = or i32 %22, %23
%25 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %24)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %68
28: ; preds = %1
%29 = load i32, i32* %4
%30 = call i32* @fdopen(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %3
%31 = load i32*, i32** %3
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %65
33: ; preds = %28
%34 = load i32, i32* %2
%35 = sext i32 %34 to i64
%36 = mul i64 1, %35
%37 = trunc i64 %36 to i32
%38 = call i64 @malloc(i32 %37)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %5
%40 = load i32*, i32** %3
%41 = load i8*, i8** %5
%42 = load i32, i32* @_IOFBF
%43 = load i32, i32* %2
%44 = call i64 @setvbuf(i32* %40, i8* %41, i32 %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %33
%47 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %33
store i32 0, i32* %6
br label %51
51: ; preds = %57, %50
%52 = load i32, i32* %6
%53 = icmp sle i32 %52, 500000
br i1 %53, label %54, label %60
54: ; preds = %51
%55 = load i32*, i32** %3
%56 = call i32 @fputc(i32 48, i32* %55)
br label %57
57: ; preds = %54
%58 = load i32, i32* %6
%59 = add nsw i32 %58, 1
store i32 %59, i32* %6
br label %51
60: ; preds = %51
%61 = load i8*, i8** %5
%62 = call i32 @free(i8* %61)
%63 = load i32*, i32** %3
%64 = call i32 @fclose(i32* %63)
br label %67
65: ; preds = %28
%66 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %67
67: ; preds = %65, %60
br label %72
68: ; preds = %1
%69 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %67
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"bar.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_SYNC = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
@.str.1 = [2 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 = [17 x i8] c"Error on fdopen,\00"
@.str.4 = [15 x i8] c"Error on open,\00"
define void @write_data(i32 %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_WRONLY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_SYNC
%6 = or i32 %4, %5
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWOTH
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %45, label %22
22: ; preds = %1
%23 = tail call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %43, label %25
25: ; preds = %22
%26 = tail call i64 @malloc(i32 %0)
%27 = inttoptr i64 %26 to i8*
%28 = load i32, i32* @_IOFBF
%29 = tail call i64 @setvbuf(i32* %23, i8* %27, i32 %28, i32 %0)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %25, %35
%36 = phi i32 [ %38, %35 ], [ 0, %25 ]
%37 = tail call i32 @fputc(i32 48, i32* %23)
%38 = add nuw nsw i32 %36, 1
%39 = icmp eq i32 %38, 500001
br i1 %39, label %40, label %35
40: ; preds = %35
%41 = tail call i32 @free(i8* %27)
%42 = tail call i32 @fclose(i32* %23)
br label %49
43: ; preds = %22
%44 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %49
45: ; preds = %1
%46 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
49: ; preds = %40, %43
ret void
}