#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*/ abort();
int /*<<< orphan*/ flip2(unsigned short const *, unsigned short *, size_t);
int /*<<< orphan*/ flip4(unsigned int const *, unsigned int *, size_t);
int /*<<< orphan*/ printf(char *, int);
int read(int, void *, size_t);
ssize_t read_block(int f, void *data, size_t elements, int elsize) {
  if (elsize == 1) {
    return read(f, data, elements);
  } else if (elsize == 2) {
    ssize_t n;
    n = read(f, data, elements * 2) / 2;
    if (n == elements) {
      flip2((const unsigned short *)data, (unsigned short *)data, elements);
    }
    return n;
  } else if (elsize == 4) {
    ssize_t n;
    n = read(f, data, elements * 4) / 4;
    if (n == elements) {
      flip4((const unsigned int *)data, (unsigned int *)data, elements);
    }
    return n;
  } else {
    printf("Fatal error in read_block(): bad elsize (%d)\n", elsize);
    abort();
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    118b <read_block+0x3b>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <read@plt>
cltq
mov    %rax,-0x8(%rbp)
jmp    1251 <read_block+0x101>
cmpl   $0x2,-0x24(%rbp)
jne    11e3 <read_block+0x93>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
shl    $0x1,%rdx
call   1050 <read@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11d6 <read_block+0x86>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <flip2@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1251 <read_block+0x101>
cmpl   $0x4,-0x24(%rbp)
jne    123b <read_block+0xeb>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
shl    $0x2,%rdx
call   1050 <read@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    122e <read_block+0xde>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <flip4@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1251 <read_block+0x101>
mov    -0x24(%rbp),%esi
lea    0xdbb(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_block>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
cmp    $0x4,%ecx
je     1178 <read_block+0x28>
cmp    $0x2,%ecx
je     11ab <read_block+0x5b>
cmp    $0x1,%ecx
jne    11e1 <read_block+0x91>
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
cltq
jmp    11d9 <read_block+0x89>
lea    0x0(,%rbx,4),%rdx
mov    %r14,%rsi
call   1050 <read@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x2,%ecx
movslq %ecx,%rax
cmp    %rbx,%rax
jne    11d9 <read_block+0x89>
mov    %r14,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <flip4@plt>
jmp    11d6 <read_block+0x86>
lea    (%rbx,%rbx,1),%rdx
mov    %r14,%rsi
call   1050 <read@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rax
cmp    %rbx,%rax
jne    11d9 <read_block+0x89>
mov    %r14,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <flip2@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe18(%rip),%rdi
mov    %ecx,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

LLVM IR (-O2):
@.str = [46 x i8] c"Fatal error in read_block(): bad elsize (%d)\0A\00"
define i64 @read_block(i32 %0, i8* %1, i64 %2, i32 %3) {
switch i32 %3, label %26 [
i32 1, label %5
i32 2, label %8
i32 4, label %17
]
5: ; preds = %4
%6 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%7 = sext i32 %6 to i64
br label %29
8: ; preds = %4
%9 = shl i64 %2, 1
%10 = tail call i32 @read(i32 %0, i8* %1, i64 %9)
%11 = sdiv i32 %10, 2
%12 = sext i32 %11 to i64
%13 = icmp eq i64 %12, %2
br i1 %13, label %14, label %29
14: ; preds = %8
%15 = bitcast i8* %1 to i16*
%16 = tail call i32 @flip2(i16* %15, i16* %15, i64 %2)
br label %29
17: ; preds = %4
%18 = shl i64 %2, 2
%19 = tail call i32 @read(i32 %0, i8* %1, i64 %18)
%20 = sdiv i32 %19, 4
%21 = sext i32 %20 to i64
%22 = icmp eq i64 %21, %2
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = bitcast i8* %1 to i32*
%25 = tail call i32 @flip4(i32* %24, i32* %24, i64 %2)
br label %29
26: ; preds = %4
%27 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %3)
%28 = tail call i32 (...) @abort()
unreachable
29: ; preds = %17, %23, %8, %14, %5
%30 = phi i64 [ %7, %5 ], [ %2, %14 ], [ %12, %8 ], [ %2, %23 ], [ %21, %17 ]
ret i64 %30
}