long calculate_num_channels(signed char *module_header) {
  long numchan;
  switch ((module_header[1082] << 8) | module_header[1083]) {
  case 0x4b2e:
  case 0x4b21:
  case 0x542e:
  case 0x5434:
    numchan = 4;
    break;
  case 0x484e:
    numchan = module_header[1080] - 48;
    break;
  case 0x4348:
    numchan = ((module_header[1080] - 48) * 10) + (module_header[1081] - 48);
    break;
  default:
    numchan = 0;
    break;
  }
  if (numchan > 16)
    numchan = 0;
  return numchan;
}


x86 assembly (clang, -O0):
<calculate_num_channels>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rcx
movsbl 0x43a(%rcx),%eax
shl    $0x8,%eax
movsbl 0x43b(%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
sub    $0x4348,%eax
je     11b7 <calculate_num_channels+0xb7>
jmp    1132 <calculate_num_channels+0x32>
mov    -0x14(%rbp),%eax
sub    $0x484e,%eax
je     119e <calculate_num_channels+0x9e>
jmp    1145 <calculate_num_channels+0x45>
mov    -0x14(%rbp),%eax
sub    $0x4b21,%eax
je     1191 <calculate_num_channels+0x91>
jmp    1158 <calculate_num_channels+0x58>
mov    -0x14(%rbp),%eax
sub    $0x4b2e,%eax
je     1191 <calculate_num_channels+0x91>
jmp    116b <calculate_num_channels+0x6b>
mov    -0x14(%rbp),%eax
sub    $0x542e,%eax
je     1191 <calculate_num_channels+0x91>
jmp    117e <calculate_num_channels+0x7e>
mov    -0x14(%rbp),%eax
sub    $0x5434,%eax
jne    11e3 <calculate_num_channels+0xe3>
jmp    1191 <calculate_num_channels+0x91>
movq   $0x4,-0x10(%rbp)
jmp    11eb <calculate_num_channels+0xeb>
mov    -0x8(%rbp),%rax
movsbl 0x438(%rax),%eax
sub    $0x30,%eax
cltq
mov    %rax,-0x10(%rbp)
jmp    11eb <calculate_num_channels+0xeb>
mov    -0x8(%rbp),%rax
movsbl 0x438(%rax),%eax
sub    $0x30,%eax
imul   $0xa,%eax,%eax
mov    -0x8(%rbp),%rcx
movsbl 0x439(%rcx),%ecx
sub    $0x30,%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x10(%rbp)
jmp    11eb <calculate_num_channels+0xeb>
movq   $0x0,-0x10(%rbp)
cmpq   $0x10,-0x10(%rbp)
jle    11fe <calculate_num_channels+0xfe>
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_num_channels>:
movsbl 0x43a(%rdi),%eax
shl    $0x8,%eax
movsbl 0x43b(%rdi),%ecx
or     %eax,%ecx
mov    $0x4,%eax
cmp    $0x4b2d,%ecx
jg     1139 <calculate_num_channels+0x39>
cmp    $0x4348,%ecx
je     1154 <calculate_num_channels+0x54>
cmp    $0x484e,%ecx
je     1175 <calculate_num_channels+0x75>
cmp    $0x4b21,%ecx
jne    1151 <calculate_num_channels+0x51>
ret
cmp    $0x4b2e,%ecx
je     1138 <calculate_num_channels+0x38>
cmp    $0x542e,%ecx
je     1138 <calculate_num_channels+0x38>
cmp    $0x5434,%ecx
je     1138 <calculate_num_channels+0x38>
xor    %eax,%eax
ret
movsbq 0x438(%rdi),%rax
lea    (%rax,%rax,4),%rax
movsbq 0x439(%rdi),%rcx
lea    (%rcx,%rax,2),%rcx
add    $0xfffffffffffffdf0,%rcx
jmp    1181 <calculate_num_channels+0x81>
movsbq 0x438(%rdi),%rcx
add    $0xffffffffffffffd0,%rcx
xor    %eax,%eax
cmp    $0x11,%rcx
cmovl  %rcx,%rax
ret

LLVM IR (-O0):
define i64 @calculate_num_channels(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = getelementptr inbounds i8, i8* %4, i64 1082
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
%8 = shl i32 %7, 8
%9 = load i8*, i8** %2
%10 = getelementptr inbounds i8, i8* %9, i64 1083
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = or i32 %8, %12
switch i32 %13, label %36 [
i32 19246, label %14
i32 19233, label %14
i32 21550, label %14
i32 21556, label %14
i32 18510, label %15
i32 17224, label %22
]
14: ; preds = %1, %1, %1, %1
store i64 4, i64* %3
br label %37
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = getelementptr inbounds i8, i8* %16, i64 1080
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = sub nsw i32 %19, 48
%21 = sext i32 %20 to i64
store i64 %21, i64* %3
br label %37
22: ; preds = %1
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i64 1080
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = sub nsw i32 %26, 48
%28 = mul nsw i32 %27, 10
%29 = load i8*, i8** %2
%30 = getelementptr inbounds i8, i8* %29, i64 1081
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = sub nsw i32 %32, 48
%34 = add nsw i32 %28, %33
%35 = sext i32 %34 to i64
store i64 %35, i64* %3
br label %37
36: ; preds = %1
store i64 0, i64* %3
br label %37
37: ; preds = %36, %22, %15, %14
%38 = load i64, i64* %3
%39 = icmp sgt i64 %38, 16
br i1 %39, label %40, label %41
40: ; preds = %37
store i64 0, i64* %3
br label %41
41: ; preds = %40, %37
%42 = load i64, i64* %3
ret i64 %42
}

LLVM IR (-O2):
define i64 @calculate_num_channels(i8* nocapture %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 1082
%3 = load i8, i8* %2
%4 = sext i8 %3 to i32
%5 = shl nsw i32 %4, 8
%6 = getelementptr inbounds i8, i8* %0, i64 1083
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
%9 = or i32 %5, %8
switch i32 %9, label %25 [
i32 19246, label %30
i32 19233, label %30
i32 21550, label %30
i32 21556, label %30
i32 18510, label %10
i32 17224, label %15
]
10: ; preds = %1
%11 = getelementptr inbounds i8, i8* %0, i64 1080
%12 = load i8, i8* %11
%13 = sext i8 %12 to i64
%14 = add nsw i64 %13, -48
br label %26
15: ; preds = %1
%16 = getelementptr inbounds i8, i8* %0, i64 1080
%17 = load i8, i8* %16
%18 = sext i8 %17 to i64
%19 = mul nsw i64 %18, 10
%20 = getelementptr inbounds i8, i8* %0, i64 1081
%21 = load i8, i8* %20
%22 = sext i8 %21 to i64
%23 = add nsw i64 %22, -528
%24 = add nsw i64 %23, %19
br label %26
25: ; preds = %1
br label %30
26: ; preds = %15, %10
%27 = phi i64 [ %24, %15 ], [ %14, %10 ]
%28 = icmp sgt i64 %27, 16
%29 = select i1 %28, i64 0, i64 %27
br label %30
30: ; preds = %26, %1, %1, %1, %1, %25
%31 = phi i64 [ 4, %1 ], [ 4, %1 ], [ 4, %1 ], [ 4, %1 ], [ 0, %25 ], [ %29, %26 ]
ret i64 %31
}