#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 unsigned int uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
unsigned int ntohl(int);
unsigned int ntohs(int /*<<< orphan*/);
unsigned as_uint(void *data, uint32_t bytes) {
  unsigned val = 0;
  switch (bytes) {
  case 1:
    val = *((uint8_t *)data);
    break;
  case 2:
    val = ntohs(*((uint16_t *)data));
    break;
  case 4:
    val = ntohl(*((uint32_t *)data));
    break;
  default:
    break;
  }
  return val;
}


x86 assembly (clang, -O0):
<as_uint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x1,%eax
je     116c <as_uint+0x4c>
jmp    114a <as_uint+0x2a>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
je     117a <as_uint+0x5a>
jmp    115b <as_uint+0x3b>
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
je     118d <as_uint+0x6d>
jmp    11a0 <as_uint+0x80>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11a5 <as_uint+0x85>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x10(%rbp)
jmp    11a5 <as_uint+0x85>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohl@plt>
mov    %eax,-0x10(%rbp)
jmp    11a5 <as_uint+0x85>
jmp    11a5 <as_uint+0x85>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<as_uint>:
cmp    $0x4,%esi
je     1134 <as_uint+0x14>
cmp    $0x2,%esi
je     113b <as_uint+0x1b>
xor    %eax,%eax
cmp    $0x1,%esi
jne    1133 <as_uint+0x13>
mov    (%rdi),%eax
ret
mov    (%rdi),%edi
jmp    1040 <ntohl@plt>
mov    (%rdi),%edi
jmp    1030 <ntohs@plt>

LLVM IR (-O0):
define i32 @as_uint(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%6 = load i32, i32* %4
switch i32 %6, label %21 [
i32 1, label %7
i32 2, label %11
i32 4, label %16
]
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %5
br label %22
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
%15 = call i32 @ntohs(i32 %14)
store i32 %15, i32* %5
br label %22
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = bitcast i8* %17 to i32*
%19 = load i32, i32* %18
%20 = call i32 @ntohl(i32 %19)
store i32 %20, i32* %5
br label %22
21: ; preds = %2
br label %22
22: ; preds = %21, %16, %11, %7
%23 = load i32, i32* %5
ret i32 %23
}

LLVM IR (-O2):
define i32 @as_uint(i8* nocapture %0, i32 %1) {
switch i32 %1, label %14 [
i32 1, label %3
i32 2, label %6
i32 4, label %10
]
3: ; preds = %2
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
br label %14
6: ; preds = %2
%7 = bitcast i8* %0 to i32*
%8 = load i32, i32* %7
%9 = tail call i32 @ntohs(i32 %8)
br label %14
10: ; preds = %2
%11 = bitcast i8* %0 to i32*
%12 = load i32, i32* %11
%13 = tail call i32 @ntohl(i32 %12)
br label %14
14: ; preds = %2, %10, %6, %3
%15 = phi i32 [ 0, %2 ], [ %13, %10 ], [ %9, %6 ], [ %5, %3 ]
ret i32 %15
}