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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
size_t check_fread(void *, size_t, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ swap_endian_8byte(int /*<<< orphan*/ *);
size_t fread_swap8(void *ptr, size_t size, size_t nitems, FILE *stream) {
  size_t i, n;
  int8_t *e = ptr;
  n = check_fread(ptr, size, nitems, stream);
  assert(!((n * size) % 8));
  for (i = 0; i < (n * size); i += 8)
    swap_endian_8byte(e + i);
  return n;
}


x86 assembly (clang, -O0):
<fread_swap8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <check_fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
imul   -0x10(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
imul   -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    11cc <fread_swap8+0x9c>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1050 <swap_endian_8byte@plt>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    1191 <fread_swap8+0x61>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_swap8>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r13
call   1030 <check_fread@plt>
mov    %rax,%r14
mov    %rax,%r12
imul   %r15,%r12
xor    %edi,%edi
test   $0x7,%r12b
sete   %dil
call   1040 <assert@plt>
test   %r12,%r12
je     1185 <fread_swap8+0x55>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1050 <swap_endian_8byte@plt>
add    $0x8,%rbx
add    $0x20,%r13
cmp    %r12,%rbx
jb     1170 <fread_swap8+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @fread_swap8(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
%11 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%12 = load i8*, i8** %5
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %11
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = load i64, i64* %7
%17 = load i32*, i32** %8
%18 = call i64 @check_fread(i8* %14, i64 %15, i64 %16, i32* %17)
store i64 %18, i64* %10
%19 = load i64, i64* %10
%20 = load i64, i64* %6
%21 = mul i64 %19, %20
%22 = urem i64 %21, 8
%23 = icmp ne i64 %22, 0
%24 = xor i1 %23, true
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
store i64 0, i64* %9
br label %27
27: ; preds = %38, %4
%28 = load i64, i64* %9
%29 = load i64, i64* %10
%30 = load i64, i64* %6
%31 = mul i64 %29, %30
%32 = icmp ult i64 %28, %31
br i1 %32, label %33, label %41
33: ; preds = %27
%34 = load i32*, i32** %11
%35 = load i64, i64* %9
%36 = getelementptr inbounds i32, i32* %34, i64 %35
%37 = call i32 @swap_endian_8byte(i32* %36)
br label %38
38: ; preds = %33
%39 = load i64, i64* %9
%40 = add i64 %39, 8
store i64 %40, i64* %9
br label %27
41: ; preds = %27
%42 = load i64, i64* %10
ret i64 %42
}

LLVM IR (-O2):
define i64 @fread_swap8(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = bitcast i8* %0 to i32*
%6 = tail call i64 @check_fread(i8* %0, i64 %1, i64 %2, i32* %3)
%7 = mul i64 %6, %1
%8 = and i64 %7, 7
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = icmp eq i64 %7, 0
br i1 %12, label %19, label %13
13: ; preds = %4, %13
%14 = phi i64 [ %17, %13 ], [ 0, %4 ]
%15 = getelementptr inbounds i32, i32* %5, i64 %14
%16 = tail call i32 @swap_endian_8byte(i32* %15)
%17 = add i64 %14, 8
%18 = icmp ult i64 %17, %7
br i1 %18, label %13, label %19
19: ; preds = %13, %4
ret i64 %6
}