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

/* Variables and functions */
int write(int, void const *, size_t);
ssize_t write_large(int fd, const void *buf, size_t count) {
  ssize_t result = 0;
  while (count > 1024 * 1024 * 1024) {
    ssize_t r = write(fd, buf, 1024 * 1024 * 1024);
    if (r < 0)
      return r;
    result += r;
    if (r != 1024 * 1024 * 1024)
      return result;
    buf = ((const char *)buf) + 1024 * 1024 * 1024;
    count -= 1024 * 1024 * 1024;
  }
  ssize_t r = write(fd, buf, count);
  if (r < 0)
    return r;
  return result += r;
}


x86 assembly (clang, -O0):
<write_large>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x40000000,-0x18(%rbp)
40
jbe    11a3 <write_large+0x93>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x40000000,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1161 <write_large+0x51>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <write_large+0xc7>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x40000000,-0x20(%rbp)
je     1182 <write_large+0x72>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <write_large+0xc7>
mov    -0x10(%rbp),%rax
add    $0x40000000,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x40000000,%rax
mov    %rax,-0x18(%rbp)
jmp    112a <write_large+0x1a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11cb <write_large+0xbb>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <write_large+0xc7>
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_large>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %r12d,%r12d
xor    %r14d,%r14d
cmp    $0x40000001,%rdx
jb     1165 <write_large+0x55>
mov    $0x40000000,%edx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
test   %eax,%eax
js     117e <write_large+0x6e>
add    %eax,%r14d
cmp    $0x40000000,%eax
jne    1181 <write_large+0x71>
add    $0x40000000,%rbp
add    $0xffffffffc0000000,%rbx
cmp    $0x40000000,%rbx
ja     1130 <write_large+0x20>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %eax,%eax
cmovs  %r12d,%r14d
add    %eax,%r14d
jmp    1181 <write_large+0x71>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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