#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 size_t uint64_t;
typedef size_t int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ UINT32_MAX;
int /*<<< orphan*/ _fwrite_fortran(char *, size_t, size_t,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
size_t fwrite_fortran(void *ptr, size_t size, size_t nitems, FILE *stream) {
  int64_t nwritten = 0, n_to_write;
  assert(size > 0);
  while (nwritten < nitems) {
    n_to_write = (((uint64_t)UINT32_MAX) / size);
    if (n_to_write > (nitems - nwritten))
      n_to_write = nitems - nwritten;
    _fwrite_fortran(((char *)ptr) + (size * nwritten), size, n_to_write,
                    stream);
    nwritten += n_to_write;
  }
  return ((size_t)nwritten);
}


x86 assembly (clang, -O0):
<fwrite_fortran>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c9 <fwrite_fortran+0xa9>
mov    0x2e91(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jbe    1197 <fwrite_fortran+0x77>
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
imul   -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <_fwrite_fortran@plt>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1152 <fwrite_fortran+0x32>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fwrite_fortran>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r12
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1030 <assert@plt>
xor    %ebx,%ebx
test   %r15,%r15
jne    1192 <fwrite_fortran+0x72>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xchg   %ax,%ax
xor    %edx,%edx
div    %r13
mov    %r15,%rbp
sub    %rbx,%rbp
cmp    %rbp,%rax
cmovbe %rax,%rbp
mov    %rbx,%rdi
imul   %r13,%rdi
add    %r12,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
call   1040 <_fwrite_fortran@plt>
add    %rbp,%rbx
cmp    %r15,%rbx
jae    114c <fwrite_fortran+0x2c>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %r13,%rcx
shr    $0x20,%rcx
jne    1160 <fwrite_fortran+0x40>
xor    %edx,%edx
div    %r13d
jmp    1165 <fwrite_fortran+0x45>

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

LLVM IR (-O2):
@UINT32_MAX = global i64 0
define i64 @fwrite_fortran(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = icmp ne i64 %1, 0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp eq i64 %2, 0
br i1 %8, label %21, label %9
9: ; preds = %4, %9
%10 = phi i64 [ %19, %9 ], [ 0, %4 ]
%11 = load i64, i64* @UINT32_MAX
%12 = udiv i64 %11, %1
%13 = sub i64 %2, %10
%14 = icmp ugt i64 %12, %13
%15 = select i1 %14, i64 %13, i64 %12
%16 = mul i64 %10, %1
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = tail call i32 @_fwrite_fortran(i8* %17, i64 %1, i64 %15, i32* %3)
%19 = add i64 %15, %10
%20 = icmp ult i64 %19, %2
br i1 %20, label %9, label %21
21: ; preds = %9, %4
%22 = phi i64 [ 0, %4 ], [ %19, %9 ]
ret i64 %22
}