#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 */
struct file_writer {
  int dummy;
};

/* Variables and functions */
size_t file_writer_wavail(struct file_writer *);
int /*<<< orphan*/ file_writer_wloc(struct file_writer *);
int /*<<< orphan*/ file_writer_wseek(struct file_writer *, size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void const *, size_t);
int file_writer_write(struct file_writer *fw, const void *buf, size_t size) {
  for (;;) {
    size_t wav = file_writer_wavail(fw);
    size_t n = wav >= size ? size : wav;
    memcpy(file_writer_wloc(fw), buf, n);
    file_writer_wseek(fw, n);
    size -= n;
    if (0 == size)
      break;
    buf += n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<file_writer_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <file_writer_wavail@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     117c <file_writer_write+0x3c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1184 <file_writer_write+0x44>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <file_writer_wloc@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <file_writer_wseek@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11d1 <file_writer_write+0x91>
jmp    11e2 <file_writer_write+0xa2>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1154 <file_writer_write+0x14>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_writer_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <file_writer_wavail@plt>
mov    %rax,%rbx
cmp    %r12,%rax
cmovae %r12,%rbx
mov    %r15,%rdi
call   1040 <file_writer_wloc@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <file_writer_wseek@plt>
add    %rbx,%r14
sub    %rbx,%r12
jne    1160 <file_writer_write+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.file_writer = type { i32 }
define i32 @file_writer_write(%struct.file_writer* %0, i8* %1, i64 %2) {
%4 = alloca %struct.file_writer*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store %struct.file_writer* %0, %struct.file_writer** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %9
9: ; preds = %35, %3
%10 = load %struct.file_writer*, %struct.file_writer** %4
%11 = call i64 @file_writer_wavail(%struct.file_writer* %10)
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = load i64, i64* %6
%14 = icmp uge i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = load i64, i64* %6
br label %19
17: ; preds = %9
%18 = load i64, i64* %7
br label %19
19: ; preds = %17, %15
%20 = phi i64 [ %16, %15 ], [ %18, %17 ]
store i64 %20, i64* %8
%21 = load %struct.file_writer*, %struct.file_writer** %4
%22 = call i32 @file_writer_wloc(%struct.file_writer* %21)
%23 = load i8*, i8** %5
%24 = load i64, i64* %8
%25 = call i32 @memcpy(i32 %22, i8* %23, i64 %24)
%26 = load %struct.file_writer*, %struct.file_writer** %4
%27 = load i64, i64* %8
%28 = call i32 @file_writer_wseek(%struct.file_writer* %26, i64 %27)
%29 = load i64, i64* %8
%30 = load i64, i64* %6
%31 = sub i64 %30, %29
store i64 %31, i64* %6
%32 = load i64, i64* %6
%33 = icmp eq i64 0, %32
br i1 %33, label %34, label %35
34: ; preds = %19
br label %39
35: ; preds = %19
%36 = load i64, i64* %8
%37 = load i8*, i8** %5
%38 = getelementptr i8, i8* %37, i64 %36
store i8* %38, i8** %5
br label %9
39: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
%struct.file_writer = type { i32 }
define i32 @file_writer_write(%struct.file_writer* %0, i8* %1, i64 %2) {
br label %4
4: ; preds = %4, %3
%5 = phi i64 [ %2, %3 ], [ %13, %4 ]
%6 = phi i8* [ %1, %3 ], [ %15, %4 ]
%7 = tail call i64 @file_writer_wavail(%struct.file_writer* %0)
%8 = icmp ult i64 %7, %5
%9 = select i1 %8, i64 %7, i64 %5
%10 = tail call i32 @file_writer_wloc(%struct.file_writer* %0)
%11 = tail call i32 @memcpy(i32 %10, i8* %6, i64 %9)
%12 = tail call i32 @file_writer_wseek(%struct.file_writer* %0, i64 %9)
%13 = sub i64 %5, %9
%14 = icmp eq i64 %13, 0
%15 = getelementptr i8, i8* %6, i64 %9
br i1 %14, label %16, label %4
16: ; preds = %4
ret i32 0
}