#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 /*<<< orphan*/ u_int8_t;
typedef int /*<<< orphan*/ u_int32_t;

/* Variables and functions */
int /*<<< orphan*/ decode_16(int /*<<< orphan*/ *);
int /*<<< orphan*/ decode_32(int /*<<< orphan*/ *);
int extract_val(u_int8_t *buf, size_t len, u_int32_t *val) {
  switch (len) {
  case 1:
    *val = *buf;
    break;
  case 2:
    *val = decode_16(buf);
    break;
  case 4:
    *val = decode_32(buf);
    break;
  default:
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<extract_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
sub    $0x1,%rax
je     1171 <extract_val+0x51>
jmp    114b <extract_val+0x2b>
mov    -0x28(%rbp),%rax
sub    $0x2,%rax
je     1182 <extract_val+0x62>
jmp    115e <extract_val+0x3e>
mov    -0x28(%rbp),%rax
sub    $0x4,%rax
je     1198 <extract_val+0x78>
jmp    11ae <extract_val+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ba <extract_val+0x9a>
mov    -0x10(%rbp),%rdi
call   1030 <decode_16@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ba <extract_val+0x9a>
mov    -0x10(%rbp),%rdi
call   1040 <decode_32@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ba <extract_val+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <extract_val+0xa1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_val>:
push   %rbx
mov    %rdx,%rbx
cmp    $0x4,%rsi
je     113f <extract_val+0x1f>
cmp    $0x2,%rsi
je     1146 <extract_val+0x26>
mov    $0xffffffff,%eax
cmp    $0x1,%rsi
jne    114f <extract_val+0x2f>
mov    (%rdi),%eax
jmp    114b <extract_val+0x2b>
call   1040 <decode_32@plt>
jmp    114b <extract_val+0x2b>
call   1030 <decode_16@plt>
mov    %eax,(%rbx)
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @extract_val(i32* %0, i64 %1, i32* nocapture writeonly %2) {
switch i64 %1, label %12 [
i64 1, label %4
i64 2, label %6
i64 4, label %8
]
4: ; preds = %3
%5 = load i32, i32* %0
br label %10
6: ; preds = %3
%7 = tail call i32 @decode_16(i32* %0)
br label %10
8: ; preds = %3
%9 = tail call i32 @decode_32(i32* %0)
br label %10
10: ; preds = %8, %6, %4
%11 = phi i32 [ %5, %4 ], [ %7, %6 ], [ %9, %8 ]
store i32 %11, i32* %2
br label %12
12: ; preds = %10, %3
%13 = phi i32 [ -1, %3 ], [ 0, %10 ]
ret i32 %13
}