#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 write(int, void const *, int);
int write_block(int f, const void *data, int elements, int elsize) {
  if (elsize == 1) {
    return write(f, data, elements);
  } else if (elsize == 2) {
    int 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) {
    int 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    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    1186 <write_block+0x36>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x4(%rbp)
jmp    1240 <write_block+0xf0>
cmpl   $0x2,-0x18(%rbp)
jne    11d8 <write_block+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <flip2@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
shl    $0x1,%edx
call   1040 <write@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
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    1240 <write_block+0xf0>
cmpl   $0x4,-0x18(%rbp)
jne    122a <write_block+0xda>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <flip4@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
shl    $0x2,%edx
call   1040 <write@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
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    1240 <write_block+0xf0>
mov    -0x18(%rbp),%esi
lea    0xdcc(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_block>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
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    %r14d,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <write@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1070 <flip4@plt>
lea    0x0(,%r14,4),%edx
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    %r14d,%edx
call   1070 <flip4@plt>
jmp    11ee <write_block+0x9e>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <flip2@plt>
lea    (%r14,%r14,1),%edx
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    %r14d,%edx
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, 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 @write(i32 %15, i8* %16, i32 %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 i32, i32* %8
%28 = call i32 @flip2(i16* %24, i16* %26, i32 %27)
%29 = load i32, i32* %6
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = mul nsw i32 %31, 2
%33 = call i32 @write(i32 %29, i8* %30, i32 %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 i32, i32* %8
%40 = call i32 @flip2(i16* %36, i16* %38, i32 %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 i32, i32* %8
%51 = call i32 @flip4(i32* %47, i32* %49, i32 %50)
%52 = load i32, i32* %6
%53 = load i8*, i8** %7
%54 = load i32, i32* %8
%55 = mul nsw i32 %54, 4
%56 = call i32 @write(i32 %52, i8* %53, i32 %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 i32, i32* %8
%63 = call i32 @flip4(i32* %59, i32* %61, i32 %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, i32 %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, i32 %2)
br label %24
7: ; preds = %4
%8 = bitcast i8* %1 to i16*
%9 = tail call i32 @flip2(i16* %8, i16* %8, i32 %2)
%10 = shl nsw i32 %2, 1
%11 = tail call i32 @write(i32 %0, i8* %1, i32 %10)
%12 = sdiv i32 %11, 2
%13 = tail call i32 @flip2(i16* %8, i16* %8, i32 %2)
br label %24
14: ; preds = %4
%15 = bitcast i8* %1 to i32*
%16 = tail call i32 @flip4(i32* %15, i32* %15, i32 %2)
%17 = shl nsw i32 %2, 2
%18 = tail call i32 @write(i32 %0, i8* %1, i32 %17)
%19 = sdiv i32 %18, 4
%20 = tail call i32 @flip4(i32* %15, i32* %15, i32 %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
}