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


x86 assembly (clang, -O0):
<write_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    1188 <write_block+0x38>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x4(%rbp)
jmp    124a <write_block+0xfa>
cmpl   $0x2,-0x1c(%rbp)
jne    11de <write_block+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <flip2@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x1,%rdx
call   1040 <write@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <flip2@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <write_block+0xfa>
cmpl   $0x4,-0x1c(%rbp)
jne    1234 <write_block+0xe4>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <flip4@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
call   1040 <write@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <flip4@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <write_block+0xfa>
mov    -0x1c(%rbp),%esi
lea    0xdc2(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_block>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x4,%ecx
je     1180 <write_block+0x30>
cmp    $0x2,%ecx
je     11bb <write_block+0x6b>
cmp    $0x1,%ecx
jne    11f5 <write_block+0xa5>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <write@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <flip4@plt>
lea    0x0(,%r14,4),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
lea    0x3(%rax),%ebp
test   %eax,%eax
cmovns %eax,%ebp
sar    $0x2,%ebp
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <flip4@plt>
jmp    11ee <write_block+0x9e>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <flip2@plt>
lea    (%r14,%r14,1),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <flip2@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe04(%rip),%rdi
mov    %ecx,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

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