#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 */
struct tcb {
  int dummy;
};

/* Variables and functions */
int block_ioctl(struct tcb *, long, long);
int loop_ioctl(struct tcb *, long, long);
int mtd_ioctl(struct tcb *, long, long);
int rtc_ioctl(struct tcb *, long, long);
int scsi_ioctl(struct tcb *, long, long);
int sock_ioctl(struct tcb *, long, long);
int term_ioctl(struct tcb *, long, long);
int ioctl_decode(struct tcb *tcp, long code, long arg) {
  switch ((code >> 8) & 0xff) {
  case 0x54:
    return term_ioctl(tcp, code, arg);
  case 0x89:
    return sock_ioctl(tcp, code, arg);
  case 'p':
    return rtc_ioctl(tcp, code, arg);
  case 0x03:
  case 0x12:
    return block_ioctl(tcp, code, arg);
  case 0x22:
    return scsi_ioctl(tcp, code, arg);
  case 'L':
    return loop_ioctl(tcp, code, arg);
  case 'M':
    return mtd_ioctl(tcp, code, arg);
  default:
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ioctl_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movzbl -0x17(%rbp),%eax
mov    %rax,-0x28(%rbp)
sub    $0x3,%rax
je     126d <ioctl_decode+0xfd>
jmp    119b <ioctl_decode+0x2b>
mov    -0x28(%rbp),%rax
sub    $0x12,%rax
je     126d <ioctl_decode+0xfd>
jmp    11ae <ioctl_decode+0x3e>
mov    -0x28(%rbp),%rax
sub    $0x22,%rax
je     1286 <ioctl_decode+0x116>
jmp    11c1 <ioctl_decode+0x51>
mov    -0x28(%rbp),%rax
sub    $0x4c,%rax
je     129f <ioctl_decode+0x12f>
jmp    11d4 <ioctl_decode+0x64>
mov    -0x28(%rbp),%rax
sub    $0x4d,%rax
je     12b8 <ioctl_decode+0x148>
jmp    11e7 <ioctl_decode+0x77>
mov    -0x28(%rbp),%rax
sub    $0x54,%rax
je     1222 <ioctl_decode+0xb2>
jmp    11fa <ioctl_decode+0x8a>
mov    -0x28(%rbp),%rax
sub    $0x70,%rax
je     1254 <ioctl_decode+0xe4>
jmp    120d <ioctl_decode+0x9d>
mov    -0x28(%rbp),%rax
sub    $0x89,%rax
je     123b <ioctl_decode+0xcb>
jmp    12d1 <ioctl_decode+0x161>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <term_ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    12dd <ioctl_decode+0x16d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <sock_ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    12dd <ioctl_decode+0x16d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <rtc_ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    12dd <ioctl_decode+0x16d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <block_ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    12dd <ioctl_decode+0x16d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <scsi_ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    12dd <ioctl_decode+0x16d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <loop_ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    12dd <ioctl_decode+0x16d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <mtd_ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    12dd <ioctl_decode+0x16d>
jmp    12d6 <ioctl_decode+0x166>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ioctl_decode>:
mov    %rsi,%rax
shr    $0x8,%rax
cmp    $0x21,%al
jg     118c <ioctl_decode+0x1c>
cmp    $0x89,%al
je     11ac <ioctl_decode+0x3c>
cmp    $0x3,%al
je     1187 <ioctl_decode+0x17>
cmp    $0x12,%al
jne    11ba <ioctl_decode+0x4a>
jmp    1080 <block_ioctl@plt>
lea    -0x4c(%rax),%ecx
cmp    $0x24,%cl
ja     11b1 <ioctl_decode+0x41>
movzbl %cl,%eax
lea    0xe62(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    1030 <loop_ioctl@plt>
jmp    1040 <sock_ioctl@plt>
cmp    $0x22,%al
jne    11ba <ioctl_decode+0x4a>
jmp    1050 <scsi_ioctl@plt>
xor    %eax,%eax
ret
jmp    1090 <mtd_ioctl@plt>
jmp    1060 <term_ioctl@plt>
jmp    1070 <rtc_ioctl@plt>

LLVM IR (-O0):
%struct.tcb = type { i32 }
define i32 @ioctl_decode(%struct.tcb* %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.tcb*
%6 = alloca i64
%7 = alloca i64
store %struct.tcb* %0, %struct.tcb** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%8 = load i64, i64* %6
%9 = ashr i64 %8, 8
%10 = and i64 %9, 255
switch i64 %10, label %46 [
i64 84, label %11
i64 137, label %16
i64 112, label %21
i64 3, label %26
i64 18, label %26
i64 34, label %31
i64 76, label %36
i64 77, label %41
]
11: ; preds = %3
%12 = load %struct.tcb*, %struct.tcb** %5
%13 = load i64, i64* %6
%14 = load i64, i64* %7
%15 = call i32 @term_ioctl(%struct.tcb* %12, i64 %13, i64 %14)
store i32 %15, i32* %4
br label %48
16: ; preds = %3
%17 = load %struct.tcb*, %struct.tcb** %5
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = call i32 @sock_ioctl(%struct.tcb* %17, i64 %18, i64 %19)
store i32 %20, i32* %4
br label %48
21: ; preds = %3
%22 = load %struct.tcb*, %struct.tcb** %5
%23 = load i64, i64* %6
%24 = load i64, i64* %7
%25 = call i32 @rtc_ioctl(%struct.tcb* %22, i64 %23, i64 %24)
store i32 %25, i32* %4
br label %48
26: ; preds = %3, %3
%27 = load %struct.tcb*, %struct.tcb** %5
%28 = load i64, i64* %6
%29 = load i64, i64* %7
%30 = call i32 @block_ioctl(%struct.tcb* %27, i64 %28, i64 %29)
store i32 %30, i32* %4
br label %48
31: ; preds = %3
%32 = load %struct.tcb*, %struct.tcb** %5
%33 = load i64, i64* %6
%34 = load i64, i64* %7
%35 = call i32 @scsi_ioctl(%struct.tcb* %32, i64 %33, i64 %34)
store i32 %35, i32* %4
br label %48
36: ; preds = %3
%37 = load %struct.tcb*, %struct.tcb** %5
%38 = load i64, i64* %6
%39 = load i64, i64* %7
%40 = call i32 @loop_ioctl(%struct.tcb* %37, i64 %38, i64 %39)
store i32 %40, i32* %4
br label %48
41: ; preds = %3
%42 = load %struct.tcb*, %struct.tcb** %5
%43 = load i64, i64* %6
%44 = load i64, i64* %7
%45 = call i32 @mtd_ioctl(%struct.tcb* %42, i64 %43, i64 %44)
store i32 %45, i32* %4
br label %48
46: ; preds = %3
br label %47
47: ; preds = %46
store i32 0, i32* %4
br label %48
48: ; preds = %47, %41, %36, %31, %26, %21, %16, %11
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.tcb = type { i32 }
define i32 @ioctl_decode(%struct.tcb* %0, i64 %1, i64 %2) {
%4 = lshr i64 %1, 8
%5 = trunc i64 %4 to i8
switch i8 %5, label %20 [
i8 84, label %6
i8 -119, label %8
i8 112, label %10
i8 3, label %12
i8 18, label %12
i8 34, label %14
i8 76, label %16
i8 77, label %18
]
6: ; preds = %3
%7 = tail call i32 @term_ioctl(%struct.tcb* %0, i64 %1, i64 %2)
br label %20
8: ; preds = %3
%9 = tail call i32 @sock_ioctl(%struct.tcb* %0, i64 %1, i64 %2)
br label %20
10: ; preds = %3
%11 = tail call i32 @rtc_ioctl(%struct.tcb* %0, i64 %1, i64 %2)
br label %20
12: ; preds = %3, %3
%13 = tail call i32 @block_ioctl(%struct.tcb* %0, i64 %1, i64 %2)
br label %20
14: ; preds = %3
%15 = tail call i32 @scsi_ioctl(%struct.tcb* %0, i64 %1, i64 %2)
br label %20
16: ; preds = %3
%17 = tail call i32 @loop_ioctl(%struct.tcb* %0, i64 %1, i64 %2)
br label %20
18: ; preds = %3
%19 = tail call i32 @mtd_ioctl(%struct.tcb* %0, i64 %1, i64 %2)
br label %20
20: ; preds = %3, %18, %16, %14, %12, %10, %8, %6
%21 = phi i32 [ %19, %18 ], [ %17, %16 ], [ %15, %14 ], [ %13, %12 ], [ %11, %10 ], [ %9, %8 ], [ %7, %6 ], [ 0, %3 ]
ret i32 %21
}