#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ write(int, unsigned char *, size_t);
int writefile(int fd, void *buf, size_t len) {
  size_t remaining;
  unsigned char *adv_p;
  ssize_t bytes_written;
  size_t chunk_sz = 4096;
  size_t amt;
  adv_p = buf;
  remaining = len;
  while (remaining > 0) {
    amt = chunk_sz;
    if (remaining < amt) {
      amt = remaining;
    }
    bytes_written = write(fd, adv_p + len - remaining, amt);
    if (!bytes_written)
      break;
    if (bytes_written < 0) {
      return -1;
    }
    remaining -= amt;
  }
  return len;
}


x86 assembly (clang, -O0):
<writefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x1000,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11c0 <writefile+0xb0>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    1164 <writefile+0x54>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    -0x18(%rbp),%rsi
xor    %eax,%eax
sub    -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x40(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1195 <writefile+0x85>
jmp    11c0 <writefile+0xb0>
cmpq   $0x0,-0x30(%rbp)
jge    11ac <writefile+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <writefile+0xb7>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    113b <writefile+0x2b>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writefile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
test   %rdx,%rdx
je     1165 <writefile+0x55>
mov    %rsi,%r12
mov    %edi,%r15d
add    %r14,%r12
mov    %r14,%rbx
nopl   0x0(%rax)
cmp    $0x1000,%rbx
mov    $0x1000,%ebp
cmovb  %rbx,%rbp
mov    %r12,%rsi
sub    %rbx,%rsi
mov    %r15d,%edi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
je     1165 <writefile+0x55>
js     115f <writefile+0x4f>
sub    %rbp,%rbx
jne    1130 <writefile+0x20>
jmp    1165 <writefile+0x55>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @writefile(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 4096, i64* %11
%13 = load i8*, i8** %6
store i8* %13, i8** %9
%14 = load i64, i64* %7
store i64 %14, i64* %8
br label %15
15: ; preds = %42, %3
%16 = load i64, i64* %8
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %46
18: ; preds = %15
%19 = load i64, i64* %11
store i64 %19, i64* %12
%20 = load i64, i64* %8
%21 = load i64, i64* %12
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i64, i64* %8
store i64 %24, i64* %12
br label %25
25: ; preds = %23, %18
%26 = load i32, i32* %5
%27 = load i8*, i8** %9
%28 = load i64, i64* %7
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i64, i64* %8
%31 = sub i64 0, %30
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i64, i64* %12
%34 = call i64 @write(i32 %26, i8* %32, i64 %33)
store i64 %34, i64* %10
%35 = load i64, i64* %10
%36 = icmp ne i64 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %25
br label %46
38: ; preds = %25
%39 = load i64, i64* %10
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 -1, i32* %4
br label %49
42: ; preds = %38
%43 = load i64, i64* %12
%44 = load i64, i64* %8
%45 = sub i64 %44, %43
store i64 %45, i64* %8
br label %15
46: ; preds = %37, %15
%47 = load i64, i64* %7
%48 = trunc i64 %47 to i32
store i32 %48, i32* %4
br label %49
49: ; preds = %46, %41
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
define i32 @writefile(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %3
%6 = getelementptr inbounds i8, i8* %1, i64 %2
br label %9
7: ; preds = %17
%8 = icmp eq i64 %19, 0
br i1 %8, label %20, label %9
9: ; preds = %5, %7
%10 = phi i64 [ %2, %5 ], [ %19, %7 ]
%11 = icmp ult i64 %10, 4096
%12 = select i1 %11, i64 %10, i64 4096
%13 = sub i64 0, %10
%14 = getelementptr inbounds i8, i8* %6, i64 %13
%15 = tail call i64 @write(i32 %0, i8* %14, i64 %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %9
%18 = icmp slt i64 %15, 0
%19 = sub i64 %10, %12
br i1 %18, label %22, label %7
20: ; preds = %7, %9, %3
%21 = trunc i64 %2 to i32
br label %22
22: ; preds = %17, %20
%23 = phi i32 [ %21, %20 ], [ -1, %17 ]
ret i32 %23
}