#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 */

/* Variables and functions */
int /*<<< orphan*/ bbm_word_read(void *, int, unsigned short *);
int /*<<< orphan*/ bbm_write(void *, int, int);
scalar_t__ fc8080_channel_select(void *, unsigned char, unsigned char);
int fc8080_video_select(void *handle, unsigned char subch_id,
                        unsigned char buf_id, unsigned char cdi_id) {
  unsigned short fec_en = 0;
  if (cdi_id == 0) {
    bbm_write(handle, 0x0e00, 0x1c);
    bbm_write(handle, 0x0e00, 0x00);
  }
  bbm_word_read(handle, 0x0e0a, &fec_en);
  if ((fec_en & 0x00ff) && (fec_en & 0xff00) && cdi_id == 0) {
    bbm_write(handle, 0x0e02, 0x00);
    bbm_write(handle, 0x0e0a + cdi_id, 0x00);
    bbm_write(handle, 0x0e0a + cdi_id, fec_en & 0x00ff);
    bbm_write(handle, 0x0e02, 0x01);
  }
  if (fc8080_channel_select(handle, subch_id, buf_id) != 0)
    return 1;
  bbm_write(handle, 0x0e0a + cdi_id, 0x40 | subch_id);
  bbm_write(handle, 0x0910 + buf_id, 0x40 | subch_id);
  return 0;
}


x86 assembly (clang, -O0):
<fc8080_video_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %sil,%dl
mov    %rdi,-0x10(%rbp)
mov    %dl,-0x11(%rbp)
mov    %cl,-0x12(%rbp)
mov    %al,-0x13(%rbp)
movw   $0x0,-0x16(%rbp)
movzbl -0x13(%rbp),%eax
cmp    $0x0,%eax
jne    1182 <fc8080_video_select+0x52>
mov    -0x10(%rbp),%rdi
mov    $0xe00,%esi
mov    $0x1c,%edx
call   1030 <bbm_write@plt>
mov    -0x10(%rbp),%rdi
mov    $0xe00,%esi
xor    %edx,%edx
call   1030 <bbm_write@plt>
mov    -0x10(%rbp),%rdi
mov    $0xe0a,%esi
lea    -0x16(%rbp),%rdx
call   1050 <bbm_word_read@plt>
movzwl -0x16(%rbp),%eax
and    $0xff,%eax
cmp    $0x0,%eax
je     121a <fc8080_video_select+0xea>
movzwl -0x16(%rbp),%eax
and    $0xff00,%eax
cmp    $0x0,%eax
je     121a <fc8080_video_select+0xea>
movzbl -0x13(%rbp),%eax
cmp    $0x0,%eax
jne    121a <fc8080_video_select+0xea>
mov    -0x10(%rbp),%rdi
mov    $0xe02,%esi
xor    %edx,%edx
call   1030 <bbm_write@plt>
mov    -0x10(%rbp),%rdi
movzbl -0x13(%rbp),%esi
add    $0xe0a,%esi
xor    %edx,%edx
call   1030 <bbm_write@plt>
mov    -0x10(%rbp),%rdi
movzbl -0x13(%rbp),%esi
add    $0xe0a,%esi
movzwl -0x16(%rbp),%edx
and    $0xff,%edx
call   1030 <bbm_write@plt>
mov    -0x10(%rbp),%rdi
mov    $0xe02,%esi
mov    $0x1,%edx
call   1030 <bbm_write@plt>
mov    -0x10(%rbp),%rdi
mov    -0x11(%rbp),%al
movzbl %al,%esi
movzbl -0x12(%rbp),%edx
call   1040 <fc8080_channel_select@plt>
cmp    $0x0,%rax
je     1243 <fc8080_video_select+0x113>
movl   $0x1,-0x4(%rbp)
jmp    127e <fc8080_video_select+0x14e>
mov    -0x10(%rbp),%rdi
movzbl -0x13(%rbp),%esi
add    $0xe0a,%esi
movzbl -0x11(%rbp),%edx
or     $0x40,%edx
call   1030 <bbm_write@plt>
mov    -0x10(%rbp),%rdi
movzbl -0x12(%rbp),%esi
add    $0x910,%esi
movzbl -0x11(%rbp),%edx
or     $0x40,%edx
call   1030 <bbm_write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fc8080_video_select>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%rbx
movw   $0x0,0xe(%rsp)
test   %ecx,%ecx
jne    1174 <fc8080_video_select+0x44>
mov    %rbx,%rdi
mov    $0xe00,%esi
mov    $0x1c,%edx
call   1030 <bbm_write@plt>
mov    %rbx,%rdi
mov    $0xe00,%esi
xor    %edx,%edx
call   1030 <bbm_write@plt>
lea    0xe(%rsp),%rdx
mov    %rbx,%rdi
mov    $0xe0a,%esi
call   1050 <bbm_word_read@plt>
movzwl 0xe(%rsp),%eax
test   %al,%al
je     11dd <fc8080_video_select+0xad>
test   %r12b,%r12b
jne    11dd <fc8080_video_select+0xad>
and    $0xff00,%eax
je     11dd <fc8080_video_select+0xad>
mov    %rbx,%rdi
mov    $0xe02,%esi
xor    %edx,%edx
call   1030 <bbm_write@plt>
mov    %rbx,%rdi
mov    $0xe0a,%esi
xor    %edx,%edx
call   1030 <bbm_write@plt>
movzbl 0xe(%rsp),%edx
mov    %rbx,%rdi
mov    $0xe0a,%esi
call   1030 <bbm_write@plt>
mov    %rbx,%rdi
mov    $0xe02,%esi
mov    $0x1,%edx
call   1030 <bbm_write@plt>
movzbl %r14b,%esi
movzbl %r15b,%ebp
mov    %rbx,%rdi
mov    %ebp,%edx
call   1040 <fc8080_channel_select@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    122e <fc8080_video_select+0xfe>
add    $0xe0a,%r12d
or     $0x40,%r14b
movzbl %r14b,%r14d
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14d,%edx
call   1030 <bbm_write@plt>
add    $0x910,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1030 <bbm_write@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fc8080_video_select(i8* %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i16
store i8* %0, i8** %6
store i8 %1, i8* %7
store i8 %2, i8* %8
store i8 %3, i8* %9
store i16 0, i16* %10
%11 = load i8, i8* %9
%12 = zext i8 %11 to i32
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = load i8*, i8** %6
%16 = call i32 @bbm_write(i8* %15, i32 3584, i32 28)
%17 = load i8*, i8** %6
%18 = call i32 @bbm_write(i8* %17, i32 3584, i32 0)
br label %19
19: ; preds = %14, %4
%20 = load i8*, i8** %6
%21 = call i32 @bbm_word_read(i8* %20, i32 3594, i16* %10)
%22 = load i16, i16* %10
%23 = zext i16 %22 to i32
%24 = and i32 %23, 255
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %53
26: ; preds = %19
%27 = load i16, i16* %10
%28 = zext i16 %27 to i32
%29 = and i32 %28, 65280
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %53
31: ; preds = %26
%32 = load i8, i8* %9
%33 = zext i8 %32 to i32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %53
35: ; preds = %31
%36 = load i8*, i8** %6
%37 = call i32 @bbm_write(i8* %36, i32 3586, i32 0)
%38 = load i8*, i8** %6
%39 = load i8, i8* %9
%40 = zext i8 %39 to i32
%41 = add nsw i32 3594, %40
%42 = call i32 @bbm_write(i8* %38, i32 %41, i32 0)
%43 = load i8*, i8** %6
%44 = load i8, i8* %9
%45 = zext i8 %44 to i32
%46 = add nsw i32 3594, %45
%47 = load i16, i16* %10
%48 = zext i16 %47 to i32
%49 = and i32 %48, 255
%50 = call i32 @bbm_write(i8* %43, i32 %46, i32 %49)
%51 = load i8*, i8** %6
%52 = call i32 @bbm_write(i8* %51, i32 3586, i32 1)
br label %53
53: ; preds = %35, %31, %26, %19
%54 = load i8*, i8** %6
%55 = load i8, i8* %7
%56 = load i8, i8* %8
%57 = call i64 @fc8080_channel_select(i8* %54, i8 zeroext %55, i8 zeroext %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %53
store i32 1, i32* %5
br label %77
60: ; preds = %53
%61 = load i8*, i8** %6
%62 = load i8, i8* %9
%63 = zext i8 %62 to i32
%64 = add nsw i32 3594, %63
%65 = load i8, i8* %7
%66 = zext i8 %65 to i32
%67 = or i32 64, %66
%68 = call i32 @bbm_write(i8* %61, i32 %64, i32 %67)
%69 = load i8*, i8** %6
%70 = load i8, i8* %8
%71 = zext i8 %70 to i32
%72 = add nsw i32 2320, %71
%73 = load i8, i8* %7
%74 = zext i8 %73 to i32
%75 = or i32 64, %74
%76 = call i32 @bbm_write(i8* %69, i32 %72, i32 %75)
store i32 0, i32* %5
br label %77
77: ; preds = %60, %59
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
define i32 @fc8080_video_select(i8* %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3) {
%5 = alloca i16
%6 = bitcast i16* %5 to i8*
store i16 0, i16* %5
%7 = zext i8 %3 to i32
%8 = icmp eq i8 %3, 0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @bbm_write(i8* %0, i32 3584, i32 28)
%11 = tail call i32 @bbm_write(i8* %0, i32 3584, i32 0)
br label %12
12: ; preds = %9, %4
%13 = call i32 @bbm_word_read(i8* %0, i32 3594, i16* %5)
%14 = load i16, i16* %5
%15 = zext i16 %14 to i32
%16 = and i32 %15, 255
%17 = icmp eq i32 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %12
%19 = and i32 %15, 65280
%20 = icmp ne i32 %19, 0
%21 = and i1 %8, %20
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = call i32 @bbm_write(i8* %0, i32 3586, i32 0)
%24 = call i32 @bbm_write(i8* %0, i32 3594, i32 0)
%25 = load i16, i16* %5
%26 = and i16 %25, 255
%27 = zext i16 %26 to i32
%28 = call i32 @bbm_write(i8* %0, i32 3594, i32 %27)
%29 = call i32 @bbm_write(i8* %0, i32 3586, i32 1)
br label %30
30: ; preds = %22, %18, %12
%31 = call i64 @fc8080_channel_select(i8* %0, i8 zeroext %1, i8 zeroext %2)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = add nuw nsw i32 %7, 3594
%35 = or i8 %1, 64
%36 = zext i8 %35 to i32
%37 = call i32 @bbm_write(i8* %0, i32 %34, i32 %36)
%38 = zext i8 %2 to i32
%39 = add nuw nsw i32 %38, 2320
%40 = call i32 @bbm_write(i8* %0, i32 %39, i32 %36)
br label %41
41: ; preds = %30, %33
%42 = phi i32 [ 0, %33 ], [ 1, %30 ]
ret i32 %42
}