#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 x;
typedef int ulong;

/* Variables and functions */
ulong rev_endian(ulong x, int bytes) {
  if (bytes == 1)
    return x;
  if (bytes == 2)
    return ((x >> 8) & 0xFF) | ((x << 8) & 0xFF00);
  if (bytes == 3)
    return ((x >> 16) & 0xFF) | ((x)&0xFF00) | ((x << 16) & 0xFF0000);
  return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) |
         ((x << 24) & 0xFF000000);
}


x86 assembly (clang, -O0):
<rev_endian>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    111f <rev_endian+0x1f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b8 <rev_endian+0xb8>
cmpl   $0x2,-0xc(%rbp)
jne    114a <rev_endian+0x4a>
mov    -0x8(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    -0x8(%rbp),%ecx
shl    $0x8,%ecx
and    $0xff00,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11b8 <rev_endian+0xb8>
cmpl   $0x3,-0xc(%rbp)
jne    1180 <rev_endian+0x80>
mov    -0x8(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    -0x8(%rbp),%ecx
and    $0xff00,%ecx
or     %ecx,%eax
mov    -0x8(%rbp),%ecx
shl    $0x10,%ecx
and    $0xff0000,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11b8 <rev_endian+0xb8>
mov    -0x8(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    -0x8(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff00,%ecx
or     %ecx,%eax
mov    -0x8(%rbp),%ecx
shl    $0x8,%ecx
and    $0xff0000,%ecx
or     %ecx,%eax
mov    -0x8(%rbp),%ecx
shl    $0x18,%ecx
and    $0xff000000,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<rev_endian>:
mov    %edi,%eax
cmp    $0x1,%esi
je     1136 <rev_endian+0x36>
cmp    $0x3,%esi
je     1119 <rev_endian+0x19>
cmp    $0x2,%esi
jne    1134 <rev_endian+0x34>
rol    $0x8,%ax
movzwl %ax,%eax
ret
mov    %eax,%ecx
shr    $0x10,%ecx
movzbl %cl,%ecx
mov    %eax,%edx
and    $0xff00,%edx
or     %ecx,%edx
movzbl %al,%eax
shl    $0x10,%eax
or     %edx,%eax
ret
bswap  %eax
ret

LLVM IR (-O0):
define i32 @rev_endian(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* %4
store i32 %9, i32* %3
br label %51
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = load i32, i32* %4
%15 = ashr i32 %14, 8
%16 = and i32 %15, 255
%17 = load i32, i32* %4
%18 = shl i32 %17, 8
%19 = and i32 %18, 65280
%20 = or i32 %16, %19
store i32 %20, i32* %3
br label %51
21: ; preds = %10
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 3
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = load i32, i32* %4
%26 = ashr i32 %25, 16
%27 = and i32 %26, 255
%28 = load i32, i32* %4
%29 = and i32 %28, 65280
%30 = or i32 %27, %29
%31 = load i32, i32* %4
%32 = shl i32 %31, 16
%33 = and i32 %32, 16711680
%34 = or i32 %30, %33
store i32 %34, i32* %3
br label %51
35: ; preds = %21
%36 = load i32, i32* %4
%37 = ashr i32 %36, 24
%38 = and i32 %37, 255
%39 = load i32, i32* %4
%40 = ashr i32 %39, 8
%41 = and i32 %40, 65280
%42 = or i32 %38, %41
%43 = load i32, i32* %4
%44 = shl i32 %43, 8
%45 = and i32 %44, 16711680
%46 = or i32 %42, %45
%47 = load i32, i32* %4
%48 = shl i32 %47, 24
%49 = and i32 %48, -16777216
%50 = or i32 %46, %49
store i32 %50, i32* %3
br label %51
51: ; preds = %35, %24, %13, %8
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
define i32 @rev_endian(i32 %0, i32 %1) {
switch i32 %1, label %15 [
i32 1, label %17
i32 2, label %3
i32 3, label %7
]
3: ; preds = %2
%4 = trunc i32 %0 to i16
%5 = tail call i16 @llvm.bswap.i16(i16 %4)
%6 = zext i16 %5 to i32
br label %17
7: ; preds = %2
%8 = lshr i32 %0, 16
%9 = and i32 %8, 255
%10 = and i32 %0, 65280
%11 = or i32 %9, %10
%12 = shl i32 %0, 16
%13 = and i32 %12, 16711680
%14 = or i32 %11, %13
br label %17
15: ; preds = %2
%16 = tail call i32 @llvm.bswap.i32(i32 %0)
br label %17
17: ; preds = %2, %15, %7, %3
%18 = phi i32 [ %6, %3 ], [ %14, %7 ], [ %16, %15 ], [ %0, %2 ]
ret i32 %18
}