#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 /*<<< orphan*/ u_int32_t;
typedef int /*<<< orphan*/ u_int16_t;

/* Variables and functions */
int /*<<< orphan*/ bswap_16(int /*<<< orphan*/);
int /*<<< orphan*/ bswap_32(int /*<<< orphan*/);
void bswap_block(void *ptr, size_t membsize, size_t nmemb) {
  int i;
  for (i = 0; i < nmemb; i++)
    switch (membsize) {
    case 2: {
      u_int16_t *r = (u_int16_t *)ptr;
      for (i = 0; i < nmemb; i++)
        r[i] = bswap_16(r[i]);
      break;
    }
    case 4: {
      u_int32_t *r = (u_int32_t *)ptr;
      for (i = 0; i < nmemb; i++)
        r[i] = bswap_32(r[i]);
      break;
    }
    }
}


x86 assembly (clang, -O0):
<bswap_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1220 <bswap_block+0x100>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
sub    $0x2,%rax
je     1173 <bswap_block+0x53>
jmp    1160 <bswap_block+0x40>
mov    -0x38(%rbp),%rax
sub    $0x4,%rax
je     11c0 <bswap_block+0xa0>
jmp    120d <bswap_block+0xed>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11bb <bswap_block+0x9b>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <bswap_16@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1182 <bswap_block+0x62>
jmp    120d <bswap_block+0xed>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1208 <bswap_block+0xe8>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <bswap_32@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cf <bswap_block+0xaf>
jmp    120d <bswap_block+0xed>
jmp    1212 <bswap_block+0xf2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113b <bswap_block+0x1b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bswap_block>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11a8 <bswap_block+0x88>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
xor    %eax,%eax
jmp    114e <bswap_block+0x2e>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%eax
add    $0x1,%eax
movslq %eax,%rcx
cmp    %r15,%rcx
jae    11a8 <bswap_block+0x88>
cmp    $0x2,%r14
je     1180 <bswap_block+0x60>
cmp    $0x4,%r14
jne    1143 <bswap_block+0x23>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r12,%rbx,4),%edi
call   1030 <bswap_32@plt>
mov    %eax,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1160 <bswap_block+0x40>
jmp    1140 <bswap_block+0x20>
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12,%rbx,4),%edi
call   1040 <bswap_16@plt>
mov    %eax,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1190 <bswap_block+0x70>
jmp    1140 <bswap_block+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @bswap_block(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
store i32 0, i32* %7
br label %10
10: ; preds = %64, %3
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = load i64, i64* %6
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %67
15: ; preds = %10
%16 = load i64, i64* %5
switch i64 %16, label %63 [
i64 2, label %17
i64 4, label %40
]
17: ; preds = %15
%18 = load i8*, i8** %4
%19 = bitcast i8* %18 to i32*
store i32* %19, i32** %8
store i32 0, i32* %7
br label %20
20: ; preds = %36, %17
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = load i64, i64* %6
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %39
25: ; preds = %20
%26 = load i32*, i32** %8
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %29
%31 = call i32 @bswap_16(i32 %30)
%32 = load i32*, i32** %8
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
store i32 %31, i32* %35
br label %36
36: ; preds = %25
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %20
39: ; preds = %20
br label %63
40: ; preds = %15
%41 = load i8*, i8** %4
%42 = bitcast i8* %41 to i32*
store i32* %42, i32** %9
store i32 0, i32* %7
br label %43
43: ; preds = %59, %40
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = load i64, i64* %6
%47 = icmp ult i64 %45, %46
br i1 %47, label %48, label %62
48: ; preds = %43
%49 = load i32*, i32** %9
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
%54 = call i32 @bswap_32(i32 %53)
%55 = load i32*, i32** %9
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
store i32 %54, i32* %58
br label %59
59: ; preds = %48
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %43
62: ; preds = %43
br label %63
63: ; preds = %15, %62, %39
br label %64
64: ; preds = %63
%65 = load i32, i32* %7
%66 = add nsw i32 %65, 1
store i32 %66, i32* %7
br label %10
67: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @bswap_block(i8* nocapture %0, i64 %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %30, label %5
5: ; preds = %3
%6 = bitcast i8* %0 to i32*
%7 = trunc i64 %2 to i32
%8 = trunc i64 %2 to i32
br label %9
9: ; preds = %5, %25
%10 = phi i32 [ 0, %5 ], [ %27, %25 ]
switch i64 %1, label %25 [
i64 2, label %11
i64 4, label %18
]
11: ; preds = %9, %11
%12 = phi i64 [ %16, %11 ], [ 0, %9 ]
%13 = getelementptr inbounds i32, i32* %6, i64 %12
%14 = load i32, i32* %13
%15 = tail call i32 @bswap_16(i32 %14)
store i32 %15, i32* %13
%16 = add nuw i64 %12, 1
%17 = icmp eq i64 %16, %2
br i1 %17, label %25, label %11
18: ; preds = %9, %18
%19 = phi i64 [ %23, %18 ], [ 0, %9 ]
%20 = getelementptr inbounds i32, i32* %6, i64 %19
%21 = load i32, i32* %20
%22 = tail call i32 @bswap_32(i32 %21)
store i32 %22, i32* %20
%23 = add nuw i64 %19, 1
%24 = icmp eq i64 %23, %2
br i1 %24, label %25, label %18
25: ; preds = %18, %11, %9
%26 = phi i32 [ %10, %9 ], [ %8, %11 ], [ %7, %18 ]
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %28, %2
br i1 %29, label %9, label %30
30: ; preds = %25, %3
ret void
}