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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
size_t check_fread(size_t *, int, int, int /*<<< orphan*/ *);
size_t fread_swap(size_t *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, size_t, size_t);
size_t _fread_fortran(void *ptr, size_t size, size_t nitems, FILE *stream,
                      int swap) {
  uint32_t head, tail;
  size_t n;
  if (swap)
    fread_swap(&head, 4, 1, stream);
  else
    check_fread(&head, 4, 1, stream);
  assert((size * nitems) >= head);
  if (swap)
    n = fread_swap(ptr, size, nitems, stream);
  else
    n = check_fread(ptr, size, nitems, stream);
  if (swap)
    fread_swap(&tail, 4, 1, stream);
  else
    check_fread(&tail, 4, 1, stream);
  if (head != tail) {
    printf("Header length: %d; tail length: %d\n", head, tail);
    assert(head == tail);
  }
  return n;
}


x86 assembly (clang, -O0):
<_fread_fortran>:
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    %r8d,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1182 <_fread_fortran+0x42>
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1040 <fread_swap@plt>
jmp    1199 <_fread_fortran+0x59>
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <check_fread@plt>
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
cmp    -0x30(%rbp),%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0x0,-0x24(%rbp)
je     11df <_fread_fortran+0x9f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
call   1040 <fread_swap@plt>
mov    %rax,-0x40(%rbp)
jmp    11fc <_fread_fortran+0xbc>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
call   1030 <check_fread@plt>
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1222 <_fread_fortran+0xe2>
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1040 <fread_swap@plt>
jmp    1239 <_fread_fortran+0xf9>
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <check_fread@plt>
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     1270 <_fread_fortran+0x130>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xdaa(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x40(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fread_fortran>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%ebp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
test   %r8d,%r8d
je     1174 <_fread_fortran+0x34>
call   1040 <fread_swap@plt>
jmp    1179 <_fread_fortran+0x39>
call   1030 <check_fread@plt>
mov    %r15,%rax
imul   %rbx,%rax
xor    %edi,%edi
cmp    (%rsp),%rax
setae  %dil
call   1060 <assert@plt>
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %r14,%rcx
test   %ebp,%ebp
je     11bf <_fread_fortran+0x7f>
call   1040 <fread_swap@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread_swap@plt>
jmp    11de <_fread_fortran+0x9e>
call   1030 <check_fread@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <check_fread@plt>
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
cmp    %rdx,%rsi
je     120c <_fread_fortran+0xcc>
lea    0xe0d(%rip),%rdi
call   1050 <printf@plt>
mov    (%rsp),%rax
xor    %edi,%edi
cmp    0x8(%rsp),%rax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Header length: %d; tail length: %d\0A\00"
define i64 @_fread_fortran(i8* %0, i64 %1, i64 %2, i32* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i32 %4, i32* %10
%14 = load i32, i32* %10
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %5
%17 = load i32*, i32** %9
%18 = call i64 @fread_swap(i64* %11, i32 4, i32 1, i32* %17)
br label %22
19: ; preds = %5
%20 = load i32*, i32** %9
%21 = call i64 @check_fread(i64* %11, i32 4, i32 1, i32* %20)
br label %22
22: ; preds = %19, %16
%23 = load i64, i64* %7
%24 = load i64, i64* %8
%25 = mul i64 %23, %24
%26 = load i64, i64* %11
%27 = icmp uge i64 %25, %26
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i32, i32* %10
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %22
%33 = load i8*, i8** %6
%34 = bitcast i8* %33 to i64*
%35 = load i64, i64* %7
%36 = trunc i64 %35 to i32
%37 = load i64, i64* %8
%38 = trunc i64 %37 to i32
%39 = load i32*, i32** %9
%40 = call i64 @fread_swap(i64* %34, i32 %36, i32 %38, i32* %39)
store i64 %40, i64* %13
br label %50
41: ; preds = %22
%42 = load i8*, i8** %6
%43 = bitcast i8* %42 to i64*
%44 = load i64, i64* %7
%45 = trunc i64 %44 to i32
%46 = load i64, i64* %8
%47 = trunc i64 %46 to i32
%48 = load i32*, i32** %9
%49 = call i64 @check_fread(i64* %43, i32 %45, i32 %47, i32* %48)
store i64 %49, i64* %13
br label %50
50: ; preds = %41, %32
%51 = load i32, i32* %10
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32*, i32** %9
%55 = call i64 @fread_swap(i64* %12, i32 4, i32 1, i32* %54)
br label %59
56: ; preds = %50
%57 = load i32*, i32** %9
%58 = call i64 @check_fread(i64* %12, i32 4, i32 1, i32* %57)
br label %59
59: ; preds = %56, %53
%60 = load i64, i64* %11
%61 = load i64, i64* %12
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %72
63: ; preds = %59
%64 = load i64, i64* %11
%65 = load i64, i64* %12
%66 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %64, i64 %65)
%67 = load i64, i64* %11
%68 = load i64, i64* %12
%69 = icmp eq i64 %67, %68
%70 = zext i1 %69 to i32
%71 = call i32 @assert(i32 %70)
br label %72
72: ; preds = %63, %59
%73 = load i64, i64* %13
ret i64 %73
}

LLVM IR (-O2):
@.str = [36 x i8] c"Header length: %d; tail length: %d\0A\00"
define i64 @_fread_fortran(i8* %0, i64 %1, i64 %2, i32* %3, i32 %4) {
%6 = alloca i64
%7 = alloca i64
%8 = bitcast i64* %6 to i8*
%9 = bitcast i64* %7 to i8*
%10 = icmp eq i32 %4, 0
br i1 %10, label %13, label %11
11: ; preds = %5
%12 = call i64 @fread_swap(i64* %6, i32 4, i32 1, i32* %3)
br label %15
13: ; preds = %5
%14 = call i64 @check_fread(i64* %6, i32 4, i32 1, i32* %3)
br label %15
15: ; preds = %13, %11
%16 = mul i64 %2, %1
%17 = load i64, i64* %6
%18 = icmp uge i64 %16, %17
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = bitcast i8* %0 to i64*
%22 = trunc i64 %1 to i32
%23 = trunc i64 %2 to i32
br i1 %10, label %27, label %24
24: ; preds = %15
%25 = call i64 @fread_swap(i64* %21, i32 %22, i32 %23, i32* %3)
%26 = call i64 @fread_swap(i64* %7, i32 4, i32 1, i32* %3)
br label %30
27: ; preds = %15
%28 = call i64 @check_fread(i64* %21, i32 %22, i32 %23, i32* %3)
%29 = call i64 @check_fread(i64* %7, i32 4, i32 1, i32* %3)
br label %30
30: ; preds = %27, %24
%31 = phi i64 [ %28, %27 ], [ %25, %24 ]
%32 = load i64, i64* %6
%33 = load i64, i64* %7
%34 = icmp eq i64 %32, %33
br i1 %34, label %42, label %35
35: ; preds = %30
%36 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %32, i64 %33)
%37 = load i64, i64* %6
%38 = load i64, i64* %7
%39 = icmp eq i64 %37, %38
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
br label %42
42: ; preds = %35, %30
ret i64 %31
}