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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_SYNC;
int close(int);
int fdatasync(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int nwrite(int, char *, int);
int open(char *, int, int);
int file_write(char *filename, int bs, int count) {
  int fd = 0;
  int ret = -1;
  int i = 0;
  char *buf = NULL;
  bs = bs * 1024;
  buf = (char *)malloc(bs);
  if (buf == NULL)
    goto out;
  memset(buf, 0, bs);
  fd = open(filename, O_RDWR | O_CREAT | O_SYNC, 0600);
  while (i < count) {
    ret = nwrite(fd, buf, bs);
    if (ret == -1) {
      close(fd);
      goto out;
    }
    i++;
  }
  ret = fdatasync(fd);
  if (ret) {
    close(fd);
    goto out;
  }
  ret = close(fd);
  if (ret)
    goto out;
  ret = 0;
out:
  if (buf)
    free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
shl    $0xa,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c4 <file_write+0x54>
jmp    1284 <file_write+0x114>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
mov    0x2de9(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1241 <file_write+0xd1>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1080 <nwrite@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1233 <file_write+0xc3>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    1284 <file_write+0x114>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11fe <file_write+0x8e>
mov    -0x14(%rbp),%edi
call   1040 <fdatasync@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1263 <file_write+0xf3>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    1284 <file_write+0x114>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     127d <file_write+0x10d>
jmp    1284 <file_write+0x114>
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1298 <file_write+0x128>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%rbx
shl    $0xa,%r15d
mov    %r15d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1200 <file_write+0x90>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <memset@plt>
mov    0x2e39(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,%ebx
test   %ebp,%ebp
jle    11e7 <file_write+0x77>
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1080 <nwrite@plt>
cmp    $0xffffffff,%eax
je     1207 <file_write+0x97>
add    $0xffffffff,%ebp
jne    11d0 <file_write+0x60>
mov    %ebx,%edi
call   1040 <fdatasync@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %eax,%ebx
test   %ebp,%ebp
cmovne %ebp,%ebx
jmp    1213 <file_write+0xa3>
mov    $0xffffffff,%ebx
jmp    121b <file_write+0xab>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_SYNC = global i32 0
define i32 @file_write(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 -1, i32* %8
store i32 0, i32* %9
store i8* null, i8** %10
%11 = load i32, i32* %5
%12 = mul nsw i32 %11, 1024
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %10
%16 = load i8*, i8** %10
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %3
br label %62
19: ; preds = %3
%20 = load i8*, i8** %10
%21 = load i32, i32* %5
%22 = call i32 @memset(i8* %20, i32 0, i32 %21)
%23 = load i8*, i8** %4
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = load i32, i32* @O_SYNC
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %23, i32 %28, i32 384)
store i32 %29, i32* %7
br label %30
30: ; preds = %44, %19
%31 = load i32, i32* %9
%32 = load i32, i32* %6
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %47
34: ; preds = %30
%35 = load i32, i32* %7
%36 = load i8*, i8** %10
%37 = load i32, i32* %5
%38 = call i32 @nwrite(i32 %35, i8* %36, i32 %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
br label %62
44: ; preds = %34
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %30
47: ; preds = %30
%48 = load i32, i32* %7
%49 = call i32 @fdatasync(i32 %48)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
br label %62
55: ; preds = %47
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
br label %62
61: ; preds = %55
store i32 0, i32* %8
br label %62
62: ; preds = %61, %60, %52, %41, %18
%63 = load i8*, i8** %10
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i8*, i8** %10
%67 = call i32 @free(i8* %66)
br label %68
68: ; preds = %65, %62
%69 = load i32, i32* %8
ret i32 %69
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_SYNC = global i32 0
define i32 @file_write(i8* nocapture %0, i32 %1, i32 %2) {
%4 = shl nsw i32 %1, 10
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %34, label %8
8: ; preds = %3
%9 = tail call i32 @memset(i8* %6, i32 0, i32 %4)
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_SYNC
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %0, i32 %14, i32 384)
%16 = icmp sgt i32 %2, 0
br i1 %16, label %19, label %26
17: ; preds = %19
%18 = icmp eq i32 %23, %2
br i1 %18, label %26, label %19
19: ; preds = %8, %17
%20 = phi i32 [ %23, %17 ], [ 0, %8 ]
%21 = tail call i32 @nwrite(i32 %15, i8* %6, i32 %4)
%22 = icmp eq i32 %21, -1
%23 = add nuw nsw i32 %20, 1
br i1 %22, label %24, label %17
24: ; preds = %19
%25 = tail call i32 @close(i32 %15)
br label %31
26: ; preds = %17, %8
%27 = tail call i32 @fdatasync(i32 %15)
%28 = icmp eq i32 %27, 0
%29 = tail call i32 @close(i32 %15)
%30 = select i1 %28, i32 %29, i32 %27
br label %31
31: ; preds = %26, %24
%32 = phi i32 [ -1, %24 ], [ %30, %26 ]
%33 = tail call i32 @free(i8* %6)
br label %34
34: ; preds = %3, %31
%35 = phi i32 [ %32, %31 ], [ -1, %3 ]
ret i32 %35
}