#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 */
size_t check_fread(void *, size_t, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ swap_endian_4byte(int /*<<< orphan*/ *);
size_t fread_swap(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);
  if (!(size % 4))
    for (i = 0; i < (n * size); i += 4)
      swap_endian_4byte(e + i);
  return n;
}


x86 assembly (clang, -O0):
<fread_swap>:
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    -0x10(%rbp),%rax
and    $0x3,%rax
cmp    $0x0,%rax
jne    11b3 <fread_swap+0x93>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
imul   -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    11ae <fread_swap+0x8e>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1040 <swap_endian_4byte@plt>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    1173 <fread_swap+0x53>
jmp    11b3 <fread_swap+0x93>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_swap>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r13
call   1030 <check_fread@plt>
mov    %rax,%r14
test   $0x3,%r12b
jne    1165 <fread_swap+0x45>
mov    %rax,%r15
imul   %r12,%r15
test   %r15,%r15
je     1165 <fread_swap+0x45>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1040 <swap_endian_4byte@plt>
add    $0x4,%rbx
add    $0x10,%r13
cmp    %r15,%rbx
jb     1150 <fread_swap+0x30>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @fread_swap(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* %6
%20 = urem i64 %19, 4
%21 = icmp ne i64 %20, 0
br i1 %21, label %38, label %22
22: ; preds = %4
store i64 0, i64* %9
br label %23
23: ; preds = %34, %22
%24 = load i64, i64* %9
%25 = load i64, i64* %10
%26 = load i64, i64* %6
%27 = mul i64 %25, %26
%28 = icmp ult i64 %24, %27
br i1 %28, label %29, label %37
29: ; preds = %23
%30 = load i32*, i32** %11
%31 = load i64, i64* %9
%32 = getelementptr inbounds i32, i32* %30, i64 %31
%33 = call i32 @swap_endian_4byte(i32* %32)
br label %34
34: ; preds = %29
%35 = load i64, i64* %9
%36 = add i64 %35, 4
store i64 %36, i64* %9
br label %23
37: ; preds = %23
br label %38
38: ; preds = %37, %4
%39 = load i64, i64* %10
ret i64 %39
}

LLVM IR (-O2):
define i64 @fread_swap(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 = and i64 %1, 3
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %4
%10 = mul i64 %6, %1
%11 = icmp eq i64 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %9, %12
%13 = phi i64 [ %16, %12 ], [ 0, %9 ]
%14 = getelementptr inbounds i32, i32* %5, i64 %13
%15 = tail call i32 @swap_endian_4byte(i32* %14)
%16 = add i64 %13, 4
%17 = icmp ult i64 %16, %10
br i1 %17, label %12, label %18
18: ; preds = %12, %9, %4
ret i64 %6
}