#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 /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ hdlc_getuint16_addr(int const *);
int hdlc_get_addr(const uint8_t *buf, uint8_t addr_size, uint16_t *upper,
                  uint16_t *lower) {
  int ret = 0;
  switch (addr_size) {
  case 1:
    *upper = (uint16_t)(buf[0] >> 1);
    break;
  case 2:
    *upper = (uint16_t)(buf[0] >> 1);
    *lower = (uint16_t)(buf[1] >> 1);
    break;
  case 4:
    *upper = hdlc_getuint16_addr(&buf[0]);
    *lower = hdlc_getuint16_addr(&buf[2]);
    break;
  default:
    ret = -5;
  }
  return ret;
}


x86 assembly (clang, -O0):
<hdlc_get_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x1,%eax
je     1164 <hdlc_get_addr+0x54>
jmp    1142 <hdlc_get_addr+0x32>
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
je     1178 <hdlc_get_addr+0x68>
jmp    1153 <hdlc_get_addr+0x43>
mov    -0x28(%rbp),%eax
sub    $0x4,%eax
je     119c <hdlc_get_addr+0x8c>
jmp    11c7 <hdlc_get_addr+0xb7>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sar    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ce <hdlc_get_addr+0xbe>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sar    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
sar    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ce <hdlc_get_addr+0xbe>
mov    -0x8(%rbp),%rdi
call   1030 <hdlc_getuint16_addr@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
add    $0x8,%rdi
call   1030 <hdlc_getuint16_addr@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ce <hdlc_get_addr+0xbe>
movl   $0xfffffffb,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hdlc_get_addr>:
push   %r15
push   %r14
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%rbx
cmp    $0x4,%esi
je     113b <hdlc_get_addr+0x2b>
cmp    $0x2,%esi
je     1154 <hdlc_get_addr+0x44>
mov    $0xfffffffb,%eax
cmp    $0x1,%esi
jne    1165 <hdlc_get_addr+0x55>
mov    (%rbx),%eax
sar    %eax
mov    %eax,(%r15)
jmp    1163 <hdlc_get_addr+0x53>
mov    %rbx,%rdi
call   1030 <hdlc_getuint16_addr@plt>
mov    %eax,(%r15)
add    $0x8,%rbx
mov    %rbx,%rdi
call   1030 <hdlc_getuint16_addr@plt>
jmp    1160 <hdlc_get_addr+0x50>
mov    (%rbx),%eax
sar    %eax
mov    %eax,(%r15)
mov    0x4(%rbx),%eax
sar    %eax
mov    %eax,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @hdlc_get_addr(i32* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %9
%10 = load i32, i32* %6
switch i32 %10, label %37 [
i32 1, label %11
i32 2, label %17
i32 4, label %28
]
11: ; preds = %4
%12 = load i32*, i32** %5
%13 = getelementptr inbounds i32, i32* %12, i64 0
%14 = load i32, i32* %13
%15 = ashr i32 %14, 1
%16 = load i32*, i32** %7
store i32 %15, i32* %16
br label %38
17: ; preds = %4
%18 = load i32*, i32** %5
%19 = getelementptr inbounds i32, i32* %18, i64 0
%20 = load i32, i32* %19
%21 = ashr i32 %20, 1
%22 = load i32*, i32** %7
store i32 %21, i32* %22
%23 = load i32*, i32** %5
%24 = getelementptr inbounds i32, i32* %23, i64 1
%25 = load i32, i32* %24
%26 = ashr i32 %25, 1
%27 = load i32*, i32** %8
store i32 %26, i32* %27
br label %38
28: ; preds = %4
%29 = load i32*, i32** %5
%30 = getelementptr inbounds i32, i32* %29, i64 0
%31 = call i32 @hdlc_getuint16_addr(i32* %30)
%32 = load i32*, i32** %7
store i32 %31, i32* %32
%33 = load i32*, i32** %5
%34 = getelementptr inbounds i32, i32* %33, i64 2
%35 = call i32 @hdlc_getuint16_addr(i32* %34)
%36 = load i32*, i32** %8
store i32 %35, i32* %36
br label %38
37: ; preds = %4
store i32 -5, i32* %9
br label %38
38: ; preds = %37, %28, %17, %11
%39 = load i32, i32* %9
ret i32 %39
}

LLVM IR (-O2):
define i32 @hdlc_get_addr(i32* %0, i32 %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
switch i32 %1, label %18 [
i32 1, label %5
i32 2, label %8
i32 4, label %14
]
5: ; preds = %4
%6 = load i32, i32* %0
%7 = ashr i32 %6, 1
store i32 %7, i32* %2
br label %18
8: ; preds = %4
%9 = load i32, i32* %0
%10 = ashr i32 %9, 1
store i32 %10, i32* %2
%11 = getelementptr inbounds i32, i32* %0, i64 1
%12 = load i32, i32* %11
%13 = ashr i32 %12, 1
store i32 %13, i32* %3
br label %18
14: ; preds = %4
%15 = tail call i32 @hdlc_getuint16_addr(i32* %0)
store i32 %15, i32* %2
%16 = getelementptr inbounds i32, i32* %0, i64 2
%17 = tail call i32 @hdlc_getuint16_addr(i32* %16)
store i32 %17, i32* %3
br label %18
18: ; preds = %4, %14, %8, %5
%19 = phi i32 [ 0, %14 ], [ 0, %8 ], [ 0, %5 ], [ -5, %4 ]
ret i32 %19
}