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

/* Variables and functions */
int /*<<< orphan*/ ahtons(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int fwrite(int /*<<< orphan*/ *, size_t, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, void const *, size_t);
size_t nfwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  uint16_t *tmp = malloc(size * nmemb);
  if (!tmp) {
    return -1;
  }
  memcpy(tmp, ptr, size * nmemb);
  ahtons(tmp, size * nmemb / 2);
  int res = fwrite(tmp, size, nmemb, stream);
  free(tmp);
  return res;
}


x86 assembly (clang, -O0):
<nfwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
imul   -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1192 <nfwrite+0x42>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e7 <nfwrite+0x97>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
imul   -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
shr    $0x1,%rsi
call   1040 <ahtons@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1070 <fwrite@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movslq -0x34(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nfwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbp
mov    %rdx,%rbx
imul   %rsi,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ba <nfwrite+0x6a>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
shr    %rbx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <ahtons@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <fwrite@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1030 <free@plt>
movslq %ebx,%rax
jmp    11c1 <nfwrite+0x71>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @nfwrite(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 i32*
%11 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%12 = load i64, i64* %7
%13 = load i64, i64* %8
%14 = mul i64 %12, %13
%15 = call i32* @malloc(i64 %14)
store i32* %15, i32** %10
%16 = load i32*, i32** %10
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %4
store i64 -1, i64* %5
br label %41
19: ; preds = %4
%20 = load i32*, i32** %10
%21 = load i8*, i8** %6
%22 = load i64, i64* %7
%23 = load i64, i64* %8
%24 = mul i64 %22, %23
%25 = call i32 @memcpy(i32* %20, i8* %21, i64 %24)
%26 = load i32*, i32** %10
%27 = load i64, i64* %7
%28 = load i64, i64* %8
%29 = mul i64 %27, %28
%30 = udiv i64 %29, 2
%31 = call i32 @ahtons(i32* %26, i64 %30)
%32 = load i32*, i32** %10
%33 = load i64, i64* %7
%34 = load i64, i64* %8
%35 = load i32*, i32** %9
%36 = call i32 @fwrite(i32* %32, i64 %33, i64 %34, i32* %35)
store i32 %36, i32* %11
%37 = load i32*, i32** %10
%38 = call i32 @free(i32* %37)
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
store i64 %40, i64* %5
br label %41
41: ; preds = %19, %18
%42 = load i64, i64* %5
ret i64 %42
}

LLVM IR (-O2):
define i64 @nfwrite(i8* %0, i64 %1, i64 %2, i32* nocapture %3) {
%5 = mul i64 %2, %1
%6 = tail call i32* @malloc(i64 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %15, label %8
8: ; preds = %4
%9 = tail call i32 @memcpy(i32* %6, i8* %0, i64 %5)
%10 = lshr i64 %5, 1
%11 = tail call i32 @ahtons(i32* %6, i64 %10)
%12 = tail call i32 @fwrite(i32* %6, i64 %1, i64 %2, i32* %3)
%13 = tail call i32 @free(i32* %6)
%14 = sext i32 %12 to i64
br label %15
15: ; preds = %4, %8
%16 = phi i64 [ %14, %8 ], [ -1, %4 ]
ret i64 %16
}