#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 */
size_t fwrite(char *, unsigned long long const, int, int /*<<< orphan*/ *);
size_t mod_fwrite(const void *array, unsigned long long size,
                  unsigned long long count, FILE *stream) {
  unsigned long long pos, tot_size, pos_ct;
  const unsigned long long block_size = 4 * 512 * 512 * 512;
  tot_size = size * count;
  if (tot_size <= block_size)
    return fwrite(array, size, count, stream);
  else {
    pos = 0;
    pos_ct = 0;
    while ((pos + block_size) < tot_size) {
      fwrite((char *)array + pos, block_size, 1, stream);
      pos_ct++;
      pos = block_size * pos_ct;
    }
    return fwrite((char *)array + pos, tot_size - pos, 1, stream);
  }
}


x86 assembly (clang, -O0):
<mod_fwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x20000000,-0x48(%rbp)
20
mov    -0x18(%rbp),%rax
imul   -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x20000000,-0x38(%rbp)
20
ja     116b <mod_fwrite+0x5b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
call   1030 <fwrite@plt>
mov    %rax,-0x8(%rbp)
jmp    11e9 <mod_fwrite+0xd9>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    $0x20000000,%rax
cmp    -0x38(%rbp),%rax
jae    11c7 <mod_fwrite+0xb7>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x20000000,%esi
mov    $0x1,%edx
call   1030 <fwrite@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
shl    $0x1d,%rax
mov    %rax,-0x30(%rbp)
jmp    117b <mod_fwrite+0x6b>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fwrite@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mod_fwrite>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdi,%r15
mov    %rdx,%r12
imul   %rsi,%r12
cmp    $0x20000001,%r12
jae    1133 <mod_fwrite+0x23>
mov    %r15,%rdi
jmp    117d <mod_fwrite+0x6d>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rbx
lea    (%r15,%rax,1),%rdi
mov    $0x20000000,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fwrite@plt>
lea    0x20000000(%rbx),%rax
add    $0x40000000,%rbx
cmp    %r12,%rbx
jb     1140 <mod_fwrite+0x30>
add    %rax,%r15
sub    %rax,%r12
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %r14,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <fwrite@plt>

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

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