#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 char uint32_t;

/* Variables and functions */
unsigned char ntohl(unsigned char);
uint32_t to_word(unsigned char *buf, size_t len) {
  uint32_t word = 0;
  switch (len) {
  default:
  case 4:
    word |= (buf[3] & 0xff) << 24;
  case 3:
    word |= (buf[2] & 0xff) << 16;
  case 2:
    word |= (buf[1] & 0xff) << 8;
  case 1:
    word |= (buf[0] & 0xff);
  case 0:
    break;
  }
  return ntohl(word);
}


x86 assembly (clang, -O0):
<to_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
sub    $0x4,%rax
ja     114a <to_word+0x3a>
mov    -0x20(%rbp),%rax
lea    0xebf(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    114f <to_word+0x3f>
mov    -0x8(%rbp),%rax
movzbl 0x3(%rax),%ecx
and    $0xff,%ecx
shl    $0x18,%ecx
movzbl -0x11(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rax
movzbl 0x2(%rax),%ecx
and    $0xff,%ecx
shl    $0x10,%ecx
movzbl -0x11(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rax
movzbl 0x1(%rax),%ecx
and    $0xff,%ecx
shl    $0x8,%ecx
movzbl -0x11(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%ecx
and    $0xff,%ecx
movzbl -0x11(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x11(%rbp)
jmp    11b8 <to_word+0xa8>
movzbl -0x11(%rbp),%edi
call   1030 <ntohl@plt>
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_word>:
test   %rsi,%rsi
je     111f <to_word+0xf>
mov    (%rdi),%al
movzbl %al,%edi
jmp    1030 <ntohl@plt>
xor    %eax,%eax
movzbl %al,%edi
jmp    1030 <ntohl@plt>

LLVM IR (-O0):
define zeroext i8 @to_word(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8
store i8* %0, i8** %3
store i64 %1, i64* %4
store i8 0, i8* %5
%6 = load i64, i64* %4
switch i64 %6, label %7 [
i64 4, label %8
i64 3, label %19
i64 2, label %30
i64 1, label %41
i64 0, label %51
]
7: ; preds = %2
br label %8
8: ; preds = %2, %7
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 3
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = and i32 %12, 255
%14 = shl i32 %13, 24
%15 = load i8, i8* %5
%16 = zext i8 %15 to i32
%17 = or i32 %16, %14
%18 = trunc i32 %17 to i8
store i8 %18, i8* %5
br label %19
19: ; preds = %2, %8
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i64 2
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = and i32 %23, 255
%25 = shl i32 %24, 16
%26 = load i8, i8* %5
%27 = zext i8 %26 to i32
%28 = or i32 %27, %25
%29 = trunc i32 %28 to i8
store i8 %29, i8* %5
br label %30
30: ; preds = %2, %19
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = and i32 %34, 255
%36 = shl i32 %35, 8
%37 = load i8, i8* %5
%38 = zext i8 %37 to i32
%39 = or i32 %38, %36
%40 = trunc i32 %39 to i8
store i8 %40, i8* %5
br label %41
41: ; preds = %2, %30
%42 = load i8*, i8** %3
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = and i32 %45, 255
%47 = load i8, i8* %5
%48 = zext i8 %47 to i32
%49 = or i32 %48, %46
%50 = trunc i32 %49 to i8
store i8 %50, i8* %5
br label %51
51: ; preds = %2, %41
br label %52
52: ; preds = %51
%53 = load i8, i8* %5
%54 = call zeroext i8 @ntohl(i8 zeroext %53)
ret i8 %54
}

LLVM IR (-O2):
define zeroext i8 @to_word(i8* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = load i8, i8* %0
br label %6
6: ; preds = %2, %4
%7 = phi i8 [ %5, %4 ], [ 0, %2 ]
%8 = tail call zeroext i8 @ntohl(i8 zeroext %7)
ret i8 %8
}