#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 */

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ flip2(unsigned short const *, unsigned short *, int);
int /*<<< orphan*/ flip4(unsigned int const *, unsigned int *, int);
int /*<<< orphan*/ printf(char *, int);
int read(int, void *, int);
int read_block(int f, void *data, int elements, int elsize) {
  if (elsize == 1) {
    return read(f, data, elements);
  } else if (elsize == 2) {
    int 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) {
    int 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    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    1186 <read_block+0x36>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x4(%rbp)
jmp    1238 <read_block+0xe8>
cmpl   $0x2,-0x18(%rbp)
jne    11d4 <read_block+0x84>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
shl    $0x1,%edx
call   1050 <read@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11c9 <read_block+0x79>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <flip2@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <read_block+0xe8>
cmpl   $0x4,-0x18(%rbp)
jne    1222 <read_block+0xd2>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
shl    $0x2,%edx
call   1050 <read@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1217 <read_block+0xc7>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <flip4@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <read_block+0xe8>
mov    -0x18(%rbp),%esi
lea    0xdd4(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_block>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
cmp    $0x4,%ecx
je     1179 <read_block+0x29>
cmp    $0x2,%ecx
je     11a8 <read_block+0x58>
cmp    $0x1,%ecx
jne    11d7 <read_block+0x87>
mov    %r14,%rsi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <read@plt>
lea    0x0(,%rbx,4),%edx
mov    %r14,%rsi
call   1050 <read@plt>
mov    %eax,%ecx
lea    0x3(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0x2,%eax
cmp    %ebx,%eax
jne    11cf <read_block+0x7f>
mov    %r14,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1070 <flip4@plt>
jmp    11cd <read_block+0x7d>
lea    (%rbx,%rbx,1),%edx
mov    %r14,%rsi
call   1050 <read@plt>
mov    %eax,%ecx
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
cmp    %ebx,%eax
jne    11cf <read_block+0x7f>
mov    %r14,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <flip2@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe22(%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 i32 @read_block(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = load i32, i32* %8
%18 = call i32 @read(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %5
br label %67
19: ; preds = %4
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %41
22: ; preds = %19
%23 = load i32, i32* %6
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = mul nsw i32 %25, 2
%27 = call i32 @read(i32 %23, i8* %24, i32 %26)
%28 = sdiv i32 %27, 2
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = load i32, i32* %8
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %39
32: ; preds = %22
%33 = load i8*, i8** %7
%34 = bitcast i8* %33 to i16*
%35 = load i8*, i8** %7
%36 = bitcast i8* %35 to i16*
%37 = load i32, i32* %8
%38 = call i32 @flip2(i16* %34, i16* %36, i32 %37)
br label %39
39: ; preds = %32, %22
%40 = load i32, i32* %10
store i32 %40, i32* %5
br label %67
41: ; preds = %19
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 4
br i1 %43, label %44, label %63
44: ; preds = %41
%45 = load i32, i32* %6
%46 = load i8*, i8** %7
%47 = load i32, i32* %8
%48 = mul nsw i32 %47, 4
%49 = call i32 @read(i32 %45, i8* %46, i32 %48)
%50 = sdiv i32 %49, 4
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = load i32, i32* %8
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %61
54: ; preds = %44
%55 = load i8*, i8** %7
%56 = bitcast i8* %55 to i32*
%57 = load i8*, i8** %7
%58 = bitcast i8* %57 to i32*
%59 = load i32, i32* %8
%60 = call i32 @flip4(i32* %56, i32* %58, i32 %59)
br label %61
61: ; preds = %54, %44
%62 = load i32, i32* %11
store i32 %62, i32* %5
br label %67
63: ; preds = %41
%64 = load i32, i32* %9
%65 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %64)
%66 = call i32 (...) @abort()
unreachable
67: ; preds = %61, %39, %14
%68 = load i32, i32* %5
ret i32 %68
}

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