unsigned int old_format_packet_length(const unsigned char *buf,
                                      unsigned int *length_type,
                                      int *indeterminate_length) {
  switch (buf[0] & 0x3) {
  case 0:
    *length_type = 2;
    return buf[1];
  case 1:
    *length_type = 3;
    return (buf[1] << 8) | buf[2];
  case 2:
    *length_type = 5;
    return (buf[1] << 24) | (buf[2] << 16) | (buf[3] << 8) | buf[4];
  default:
    *length_type = 1;
    *indeterminate_length = 1;
    return 0;
  }
}


x86 assembly (clang, -O0):
<old_format_packet_length>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0x3,%eax
mov    %eax,-0x24(%rbp)
je     114a <old_format_packet_length+0x4a>
jmp    1128 <old_format_packet_length+0x28>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
je     1164 <old_format_packet_length+0x64>
jmp    1139 <old_format_packet_length+0x39>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
je     118b <old_format_packet_length+0x8b>
jmp    11cc <old_format_packet_length+0xcc>
mov    -0x18(%rbp),%rax
movl   $0x2,(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <old_format_packet_length+0xe7>
mov    -0x18(%rbp),%rax
movl   $0x3,(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <old_format_packet_length+0xe7>
mov    -0x18(%rbp),%rax
movl   $0x5,(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
shl    $0x18,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x3(%rcx),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x4(%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <old_format_packet_length+0xe7>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<old_format_packet_length>:
movzbl (%rdi),%eax
and    $0x3,%eax
cmp    $0x2,%eax
je     111f <old_format_packet_length+0x1f>
cmp    $0x1,%eax
je     112b <old_format_packet_length+0x2b>
test   %eax,%eax
jne    113b <old_format_packet_length+0x3b>
movl   $0x2,(%rsi)
movzbl 0x1(%rdi),%eax
ret
movl   $0x5,(%rsi)
mov    0x1(%rdi),%eax
bswap  %eax
ret
movl   $0x3,(%rsi)
movzwl 0x1(%rdi),%eax
shl    $0x10,%eax
bswap  %eax
ret
movl   $0x1,(%rsi)
movl   $0x1,(%rdx)
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define i32 @old_format_packet_length(i8* nocapture %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = load i8, i8* %0
%5 = and i8 %4, 3
%6 = zext i8 %5 to i32
switch i32 %6, label %39 [
i32 0, label %7
i32 1, label %11
i32 2, label %20
]
7: ; preds = %3
store i32 2, i32* %1
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
br label %40
11: ; preds = %3
store i32 3, i32* %1
%12 = getelementptr inbounds i8, i8* %0, i64 1
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = shl nuw nsw i32 %14, 8
%16 = getelementptr inbounds i8, i8* %0, i64 2
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = or i32 %15, %18
br label %40
20: ; preds = %3
store i32 5, i32* %1
%21 = getelementptr inbounds i8, i8* %0, i64 1
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = shl nuw i32 %23, 24
%25 = getelementptr inbounds i8, i8* %0, i64 2
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = shl nuw nsw i32 %27, 16
%29 = or i32 %28, %24
%30 = getelementptr inbounds i8, i8* %0, i64 3
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = shl nuw nsw i32 %32, 8
%34 = or i32 %29, %33
%35 = getelementptr inbounds i8, i8* %0, i64 4
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = or i32 %34, %37
br label %40
39: ; preds = %3
store i32 1, i32* %1
store i32 1, i32* %2
br label %40
40: ; preds = %39, %20, %11, %7
%41 = phi i32 [ 0, %39 ], [ %38, %20 ], [ %19, %11 ], [ %10, %7 ]
ret i32 %41
}