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

/* Variables and functions */
int ntohl(int);
int ntohs(int /*<<< orphan*/);
int unpack_size(const char *src, uint32_t *dst) {
  unsigned char c = (unsigned char)*src;
  if (c <= 0x80) {
    *dst = *((uint8_t *)(src));
    return sizeof(uint8_t);
  }
  if (c == 0x82) {
    *dst = ntohs(*((uint16_t *)(src + 1))) & 0x0000ffff;
    return sizeof(uint16_t) + 1;
  }
  if (c == 0x84) {
    *dst = ntohl(*((uint32_t *)(src + 1)));
    return sizeof(uint32_t) + 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<unpack_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
cmp    $0x80,%eax
jg     1160 <unpack_size+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x4,-0x4(%rbp)
jmp    11cb <unpack_size+0xab>
movzbl -0x19(%rbp),%eax
cmp    $0x82,%eax
jne    1195 <unpack_size+0x75>
mov    -0x10(%rbp),%rax
mov    0x1(%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,%ecx
and    $0xffff,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x5,-0x4(%rbp)
jmp    11cb <unpack_size+0xab>
movzbl -0x19(%rbp),%eax
cmp    $0x84,%eax
jne    11c4 <unpack_size+0xa4>
mov    -0x10(%rbp),%rax
mov    0x1(%rax),%edi
call   1040 <ntohl@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x5,-0x4(%rbp)
jmp    11cb <unpack_size+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unpack_size>:
push   %rbx
mov    %rsi,%rbx
mov    (%rdi),%cl
cmp    $0x80,%cl
ja     1134 <unpack_size+0x14>
mov    (%rdi),%ecx
mov    $0x4,%eax
jmp    115c <unpack_size+0x3c>
cmp    $0x84,%cl
je     114d <unpack_size+0x2d>
xor    %eax,%eax
cmp    $0x82,%cl
jne    115e <unpack_size+0x3e>
mov    0x1(%rdi),%edi
call   1030 <ntohs@plt>
movzwl %ax,%ecx
jmp    1157 <unpack_size+0x37>
mov    0x1(%rdi),%edi
call   1040 <ntohl@plt>
mov    %eax,%ecx
mov    $0x5,%eax
mov    %ecx,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
define i32 @unpack_size(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8
store i8* %0, i8** %4
store i32* %1, i32** %5
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
store i8 %8, i8* %6
%9 = load i8, i8* %6
%10 = zext i8 %9 to i32
%11 = icmp sle i32 %10, 128
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = bitcast i8* %13 to i32*
%15 = load i32, i32* %14
%16 = load i32*, i32** %5
store i32 %15, i32* %16
store i32 4, i32* %3
br label %41
17: ; preds = %2
%18 = load i8, i8* %6
%19 = zext i8 %18 to i32
%20 = icmp eq i32 %19, 130
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = call i32 @ntohs(i32 %25)
%27 = and i32 %26, 65535
%28 = load i32*, i32** %5
store i32 %27, i32* %28
store i32 5, i32* %3
br label %41
29: ; preds = %17
%30 = load i8, i8* %6
%31 = zext i8 %30 to i32
%32 = icmp eq i32 %31, 132
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = bitcast i8* %35 to i32*
%37 = load i32, i32* %36
%38 = call i32 @ntohl(i32 %37)
%39 = load i32*, i32** %5
store i32 %38, i32* %39
store i32 5, i32* %3
br label %41
40: ; preds = %29
store i32 0, i32* %3
br label %41
41: ; preds = %40, %33, %21, %12
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
define i32 @unpack_size(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = load i8, i8* %0
%4 = icmp ult i8 %3, -127
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = bitcast i8* %0 to i32*
%7 = load i32, i32* %6
br label %20
8: ; preds = %2
switch i8 %3, label %23 [
i8 -126, label %9
i8 -124, label %15
]
9: ; preds = %8
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
%13 = tail call i32 @ntohs(i32 %12)
%14 = and i32 %13, 65535
br label %20
15: ; preds = %8
%16 = getelementptr inbounds i8, i8* %0, i64 1
%17 = bitcast i8* %16 to i32*
%18 = load i32, i32* %17
%19 = tail call i32 @ntohl(i32 %18)
br label %20
20: ; preds = %5, %9, %15
%21 = phi i32 [ %19, %15 ], [ %14, %9 ], [ %7, %5 ]
%22 = phi i32 [ 5, %15 ], [ 5, %9 ], [ 4, %5 ]
store i32 %21, i32* %1
br label %23
23: ; preds = %20, %8
%24 = phi i32 [ 0, %8 ], [ %22, %20 ]
ret i32 %24
}