#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*/ 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) {
    return write(f, data, elements * 2) / 2;
  } else if (elsize == 4) {
    return write(f, data, elements * 4) / 4;
  } 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    1166 <write_block+0x36>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x4(%rbp)
jmp    11d4 <write_block+0xa4>
cmpl   $0x2,-0x18(%rbp)
jne    1192 <write_block+0x62>
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,-0x4(%rbp)
jmp    11d4 <write_block+0xa4>
cmpl   $0x4,-0x18(%rbp)
jne    11be <write_block+0x8e>
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,-0x4(%rbp)
jmp    11d4 <write_block+0xa4>
mov    -0x18(%rbp),%esi
lea    0xe38(%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   %rax
cmp    $0x4,%ecx
je     1146 <write_block+0x16>
cmp    $0x2,%ecx
je     115d <write_block+0x2d>
cmp    $0x1,%ecx
jne    116f <write_block+0x3f>
pop    %rax
jmp    1040 <write@plt>
shl    $0x2,%edx
call   1040 <write@plt>
mov    %eax,%ecx
lea    0x3(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0x2,%eax
pop    %rcx
ret
add    %edx,%edx
call   1040 <write@plt>
mov    %eax,%ecx
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
pop    %rcx
ret
lea    0xe8a(%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
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%10 = load i32, i32* %9
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %4
%13 = load i32, i32* %6
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %5
br label %41
17: ; preds = %4
%18 = load i32, i32* %9
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i32, i32* %6
%22 = load i8*, i8** %7
%23 = load i32, i32* %8
%24 = mul nsw i32 %23, 2
%25 = call i32 @write(i32 %21, i8* %22, i32 %24)
%26 = sdiv i32 %25, 2
store i32 %26, i32* %5
br label %41
27: ; preds = %17
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 4
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i32, i32* %6
%32 = load i8*, i8** %7
%33 = load i32, i32* %8
%34 = mul nsw i32 %33, 4
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
%36 = sdiv i32 %35, 4
store i32 %36, i32* %5
br label %41
37: ; preds = %27
%38 = load i32, i32* %9
%39 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %38)
%40 = call i32 (...) @abort()
unreachable
41: ; preds = %30, %20, %12
%42 = load i32, i32* %5
ret i32 %42
}

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* nocapture %1, i32 %2, i32 %3) {
switch i32 %3, label %15 [
i32 1, label %5
i32 2, label %7
i32 4, label %11
]
5: ; preds = %4
%6 = tail call i32 @write(i32 %0, i8* %1, i32 %2)
br label %18
7: ; preds = %4
%8 = shl nsw i32 %2, 1
%9 = tail call i32 @write(i32 %0, i8* %1, i32 %8)
%10 = sdiv i32 %9, 2
br label %18
11: ; preds = %4
%12 = shl nsw i32 %2, 2
%13 = tail call i32 @write(i32 %0, i8* %1, i32 %12)
%14 = sdiv i32 %13, 4
br label %18
15: ; preds = %4
%16 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %3)
%17 = tail call i32 (...) @abort()
unreachable
18: ; preds = %11, %7, %5
%19 = phi i32 [ %6, %5 ], [ %10, %7 ], [ %14, %11 ]
ret i32 %19
}