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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
size_t write(int, unsigned char *, size_t);
size_t sqImageFileWrite(void *ptr_arg, size_t sz, size_t count, int f) {
  size_t to_be_written = sz * count, nwritten_in_total = 0;
  unsigned char *ptr = ptr_arg;
  do {
    ssize_t n = write(f, ptr,
                      ((to_be_written - nwritten_in_total) <= (0x40000000)
                           ? (to_be_written - nwritten_in_total)
                           : 0x40000000));
    if (n == (size_t)-1) {
      perror("sqImageFileWrite write");
      return nwritten_in_total;
    }
    nwritten_in_total += n;
    ptr += n;
  } while (nwritten_in_total < to_be_written);
  return nwritten_in_total / sz;
}


x86 assembly (clang, -O0):
<sqImageFileWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x18(%rbp),%rax
imul   -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x38(%rbp),%rax
cmp    $0x40000000,%rax
ja     1187 <sqImageFileWrite+0x67>
mov    -0x30(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    1195 <sqImageFileWrite+0x75>
mov    $0x40000000,%eax
mov    %rax,-0x60(%rbp)
jmp    1195 <sqImageFileWrite+0x75>
mov    -0x50(%rbp),%rsi
mov    -0x54(%rbp),%edi
mov    -0x60(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    11cd <sqImageFileWrite+0xad>
lea    0xe45(%rip),%rdi
call   1040 <perror@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1203 <sqImageFileWrite+0xe3>
mov    -0x48(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jb     1154 <sqImageFileWrite+0x34>
mov    -0x38(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqImageFileWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbp
imul   %rsi,%r12
xor    %ebx,%ebx
mov    $0x40000000,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdx
sub    %rbx,%rdx
cmp    $0x40000000,%rdx
cmovae %r13,%rdx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     11b1 <sqImageFileWrite+0x91>
add    %rax,%rbx
add    %rax,%rbp
cmp    %r12,%rbx
jb     1150 <sqImageFileWrite+0x30>
mov    %rbx,%rax
or     %r14,%rax
shr    $0x20,%rax
je     1196 <sqImageFileWrite+0x76>
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
mov    %rax,%rbx
jmp    119f <sqImageFileWrite+0x7f>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
mov    %eax,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe48(%rip),%rdi
call   1040 <perror@plt>
jmp    119f <sqImageFileWrite+0x7f>

LLVM IR (-O0):
@.str = [23 x i8] c"sqImageFileWrite write\00"
define i64 @sqImageFileWrite(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 i8*
%13 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%14 = load i64, i64* %7
%15 = load i64, i64* %8
%16 = mul i64 %14, %15
store i64 %16, i64* %10
store i64 0, i64* %11
%17 = load i8*, i8** %6
store i8* %17, i8** %12
br label %18
18: ; preds = %45, %4
%19 = load i32, i32* %9
%20 = load i8*, i8** %12
%21 = load i64, i64* %10
%22 = load i64, i64* %11
%23 = sub i64 %21, %22
%24 = icmp ule i64 %23, 1073741824
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i64, i64* %10
%27 = load i64, i64* %11
%28 = sub i64 %26, %27
br label %30
29: ; preds = %18
br label %30
30: ; preds = %29, %25
%31 = phi i64 [ %28, %25 ], [ 1073741824, %29 ]
%32 = call i64 @write(i32 %19, i8* %20, i64 %31)
store i64 %32, i64* %13
%33 = load i64, i64* %13
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%37 = load i64, i64* %11
store i64 %37, i64* %5
br label %53
38: ; preds = %30
%39 = load i64, i64* %13
%40 = load i64, i64* %11
%41 = add i64 %40, %39
store i64 %41, i64* %11
%42 = load i64, i64* %13
%43 = load i8*, i8** %12
%44 = getelementptr inbounds i8, i8* %43, i64 %42
store i8* %44, i8** %12
br label %45
45: ; preds = %38
%46 = load i64, i64* %11
%47 = load i64, i64* %10
%48 = icmp ult i64 %46, %47
br i1 %48, label %18, label %49
49: ; preds = %45
%50 = load i64, i64* %11
%51 = load i64, i64* %7
%52 = udiv i64 %50, %51
store i64 %52, i64* %5
br label %53
53: ; preds = %49, %35
%54 = load i64, i64* %5
ret i64 %54
}

LLVM IR (-O2):
@.str = [23 x i8] c"sqImageFileWrite write\00"
define i64 @sqImageFileWrite(i8* nocapture %0, i64 %1, i64 %2, i32 %3) {
%5 = mul i64 %2, %1
br label %6
6: ; preds = %16, %4
%7 = phi i64 [ 0, %4 ], [ %17, %16 ]
%8 = phi i8* [ %0, %4 ], [ %18, %16 ]
%9 = sub i64 %5, %7
%10 = icmp ult i64 %9, 1073741824
%11 = select i1 %10, i64 %9, i64 1073741824
%12 = tail call i64 @write(i32 %3, i8* %8, i64 %11)
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %22
16: ; preds = %6
%17 = add i64 %12, %7
%18 = getelementptr inbounds i8, i8* %8, i64 %12
%19 = icmp ult i64 %17, %5
br i1 %19, label %6, label %20
20: ; preds = %16
%21 = udiv i64 %17, %1
br label %22
22: ; preds = %14, %20
%23 = phi i64 [ %21, %20 ], [ %7, %14 ]
ret i64 %23
}