int replaygain_decode(unsigned int field, int *gain) {
  unsigned int name_code, originator_code, sign_bit, val;
  name_code = (field >> 13) & 0x7;
  if (!name_code || name_code > 2)
    return 0;
  originator_code = (field >> 10) & 0x7;
  if (!originator_code)
    return 0;
  sign_bit = (field >> 9) & 0x1;
  val = field & 0x1ff;
  if (sign_bit && !val)
    return 0;
  *gain = (sign_bit ? -1 : 1) * val;
  return name_code;
}


x86 assembly (clang, -O0):
<replaygain_decode>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
shr    $0xd,%eax
and    $0x7,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     112b <replaygain_decode+0x2b>
cmpl   $0x2,-0x14(%rbp)
jbe    1137 <replaygain_decode+0x37>
movl   $0x0,-0x4(%rbp)
jmp    11b3 <replaygain_decode+0xb3>
mov    -0x8(%rbp),%eax
shr    $0xa,%eax
and    $0x7,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1159 <replaygain_decode+0x59>
movl   $0x0,-0x4(%rbp)
jmp    11b3 <replaygain_decode+0xb3>
mov    -0x8(%rbp),%eax
shr    $0x9,%eax
and    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1ff,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1190 <replaygain_decode+0x90>
cmpl   $0x0,-0x20(%rbp)
jne    1190 <replaygain_decode+0x90>
movl   $0x0,-0x4(%rbp)
jmp    11b3 <replaygain_decode+0xb3>
mov    -0x1c(%rbp),%edx
mov    $0x1,%ecx
mov    $0xffffffff,%eax
cmp    $0x0,%edx
cmovne %eax,%ecx
imul   -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<replaygain_decode>:
xor    %eax,%eax
test   $0x1c00,%edi
je     1142 <replaygain_decode+0x42>
mov    %edi,%ecx
shr    $0xd,%ecx
and    $0x7,%ecx
lea    -0x3(%rcx),%edx
cmp    $0xfffffffe,%edx
jb     1142 <replaygain_decode+0x42>
mov    %edi,%edx
and    $0x3ff,%edx
cmp    $0x200,%edx
je     1142 <replaygain_decode+0x42>
mov    %edi,%eax
and    $0x1ff,%eax
mov    %eax,%edx
neg    %edx
test   $0x200,%edi
cmove  %eax,%edx
mov    %edx,(%rsi)
mov    %ecx,%eax
ret

LLVM IR (-O0):
define i32 @replaygain_decode(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%10 = load i32, i32* %4
%11 = lshr i32 %10, 13
%12 = and i32 %11, 7
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* %6
%17 = icmp ugt i32 %16, 2
br i1 %17, label %18, label %19
18: ; preds = %15, %2
store i32 0, i32* %3
br label %47
19: ; preds = %15
%20 = load i32, i32* %4
%21 = lshr i32 %20, 10
%22 = and i32 %21, 7
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %19
store i32 0, i32* %3
br label %47
26: ; preds = %19
%27 = load i32, i32* %4
%28 = lshr i32 %27, 9
%29 = and i32 %28, 1
store i32 %29, i32* %8
%30 = load i32, i32* %4
%31 = and i32 %30, 511
store i32 %31, i32* %9
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %26
%35 = load i32, i32* %9
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %34
store i32 0, i32* %3
br label %47
38: ; preds = %34, %26
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 -1, i32 1
%43 = load i32, i32* %9
%44 = mul i32 %42, %43
%45 = load i32*, i32** %5
store i32 %44, i32* %45
%46 = load i32, i32* %6
store i32 %46, i32* %3
br label %47
47: ; preds = %38, %37, %25, %18
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
define i32 @replaygain_decode(i32 %0, i32* nocapture writeonly %1) {
%3 = lshr i32 %0, 13
%4 = and i32 %3, 7
%5 = add nsw i32 %4, -3
%6 = icmp ult i32 %5, -2
%7 = and i32 %0, 7168
%8 = icmp eq i32 %7, 0
%9 = or i1 %8, %6
br i1 %9, label %19, label %10
10: ; preds = %2
%11 = and i32 %0, 511
%12 = and i32 %0, 1023
%13 = icmp eq i32 %12, 512
br i1 %13, label %19, label %14
14: ; preds = %10
%15 = and i32 %0, 512
%16 = icmp eq i32 %15, 0
%17 = sub nsw i32 0, %11
%18 = select i1 %16, i32 %11, i32 %17
store i32 %18, i32* %1
br label %19
19: ; preds = %10, %2, %14
%20 = phi i32 [ %4, %14 ], [ 0, %2 ], [ 0, %10 ]
ret i32 %20
}