#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 INB(unsigned short);
int /*<<< orphan*/ OUTB(unsigned short, int);
int /*<<< orphan*/ gu1_detect_vsa2();
int gfx_vga_mode_switch(int active) {
  unsigned short crtcindex, crtcdata;
  crtcindex = (INB(0x3CC) & 0x01) ? 0x3D4 : 0x3B4;
  crtcdata = crtcindex + 1;
  OUTB(crtcindex, 0x30);
  OUTB(crtcdata, 0x57);
  OUTB(crtcdata, 0x4C);
  OUTB(crtcindex, 0x3F);
  active = active ? 1 : 0;
  OUTB(crtcdata, (unsigned char)active);
  if ((!active) && (!(gu1_detect_vsa2()))) {
    OUTB(crtcindex, 0x33);
    while (INB(crtcdata) & 0x80)
      ;
  }
  OUTB(crtcindex, 0x30);
  OUTB(crtcdata, 0x00);
  return (0);
}


x86 assembly (clang, -O0):
<gfx_vga_mode_switch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x3cc,%edi
call   1030 <INB@plt>
mov    %eax,%edx
and    $0x1,%edx
mov    $0x3b4,%eax
mov    $0x3d4,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %ax,-0x6(%rbp)
movzwl -0x6(%rbp),%eax
add    $0x1,%eax
mov    %ax,-0x8(%rbp)
mov    $0x30,%esi
movzwl -0x6(%rbp),%edi
call   1040 <OUTB@plt>
mov    $0x57,%esi
movzwl -0x8(%rbp),%edi
call   1040 <OUTB@plt>
mov    $0x4c,%esi
movzwl -0x8(%rbp),%edi
call   1040 <OUTB@plt>
mov    $0x3f,%esi
movzwl -0x6(%rbp),%edi
call   1040 <OUTB@plt>
mov    -0x4(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%ax
mov    -0x4(%rbp),%ecx
movzbl %cl,%esi
movzwl %ax,%edi
call   1040 <OUTB@plt>
cmpl   $0x0,-0x4(%rbp)
jne    120f <gfx_vga_mode_switch+0xdf>
mov    $0x0,%al
call   1050 <gu1_detect_vsa2@plt>
cmp    $0x0,%eax
jne    120f <gfx_vga_mode_switch+0xdf>
mov    $0x33,%esi
movzwl -0x6(%rbp),%edi
call   1040 <OUTB@plt>
movzwl -0x8(%rbp),%edi
call   1030 <INB@plt>
and    $0x80,%eax
cmp    $0x0,%eax
je     120a <gfx_vga_mode_switch+0xda>
jmp    11ee <gfx_vga_mode_switch+0xbe>
jmp    120f <gfx_vga_mode_switch+0xdf>
mov    $0x30,%esi
movzwl -0x6(%rbp),%edi
call   1040 <OUTB@plt>
xor    %esi,%esi
movzwl -0x8(%rbp),%edi
call   1040 <OUTB@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gfx_vga_mode_switch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x3cc,%edi
call   1030 <INB@plt>
mov    %eax,%ebx
shl    $0x5,%ebx
and    $0x20,%ebx
lea    0x3b5(%rbx),%ebp
add    $0x3b4,%ebx
mov    %ebx,%edi
mov    $0x30,%esi
call   1040 <OUTB@plt>
mov    %ebp,%edi
mov    $0x57,%esi
call   1040 <OUTB@plt>
mov    %ebp,%edi
mov    $0x4c,%esi
call   1040 <OUTB@plt>
mov    %ebx,%edi
mov    $0x3f,%esi
call   1040 <OUTB@plt>
xor    %esi,%esi
test   %r14d,%r14d
setne  %sil
mov    %ebp,%edi
call   1040 <OUTB@plt>
movzwl %bx,%r15d
movzwl %bp,%ebx
test   %r14d,%r14d
jne    11cb <gfx_vga_mode_switch+0x9b>
xor    %eax,%eax
call   1050 <gu1_detect_vsa2@plt>
test   %eax,%eax
jne    11cb <gfx_vga_mode_switch+0x9b>
mov    %r15d,%edi
mov    $0x33,%esi
call   1040 <OUTB@plt>
nopl   0x0(%rax)
mov    %ebx,%edi
call   1030 <INB@plt>
test   $0x80,%al
jne    11c0 <gfx_vga_mode_switch+0x90>
mov    %r15d,%edi
mov    $0x30,%esi
call   1040 <OUTB@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <OUTB@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @gfx_vga_mode_switch(i32 %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca i16
store i32 %0, i32* %2
%5 = call i32 @INB(i16 zeroext 972)
%6 = and i32 %5, 1
%7 = icmp ne i32 %6, 0
%8 = zext i1 %7 to i64
%9 = select i1 %7, i32 980, i32 948
%10 = trunc i32 %9 to i16
store i16 %10, i16* %3
%11 = load i16, i16* %3
%12 = zext i16 %11 to i32
%13 = add nsw i32 %12, 1
%14 = trunc i32 %13 to i16
store i16 %14, i16* %4
%15 = load i16, i16* %3
%16 = call i32 @OUTB(i16 zeroext %15, i32 48)
%17 = load i16, i16* %4
%18 = call i32 @OUTB(i16 zeroext %17, i32 87)
%19 = load i16, i16* %4
%20 = call i32 @OUTB(i16 zeroext %19, i32 76)
%21 = load i16, i16* %3
%22 = call i32 @OUTB(i16 zeroext %21, i32 63)
%23 = load i32, i32* %2
%24 = icmp ne i32 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i32 1, i32 0
store i32 %26, i32* %2
%27 = load i16, i16* %4
%28 = load i32, i32* %2
%29 = trunc i32 %28 to i8
%30 = zext i8 %29 to i32
%31 = call i32 @OUTB(i16 zeroext %27, i32 %30)
%32 = load i32, i32* %2
%33 = icmp ne i32 %32, 0
br i1 %33, label %47, label %34
34: ; preds = %1
%35 = call i32 (...) @gu1_detect_vsa2()
%36 = icmp ne i32 %35, 0
br i1 %36, label %47, label %37
37: ; preds = %34
%38 = load i16, i16* %3
%39 = call i32 @OUTB(i16 zeroext %38, i32 51)
br label %40
40: ; preds = %45, %37
%41 = load i16, i16* %4
%42 = call i32 @INB(i16 zeroext %41)
%43 = and i32 %42, 128
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
br label %40
46: ; preds = %40
br label %47
47: ; preds = %46, %34, %1
%48 = load i16, i16* %3
%49 = call i32 @OUTB(i16 zeroext %48, i32 48)
%50 = load i16, i16* %4
%51 = call i32 @OUTB(i16 zeroext %50, i32 0)
ret i32 0
}

LLVM IR (-O2):
define i32 @gfx_vga_mode_switch(i32 %0) {
%2 = tail call i32 @INB(i16 zeroext 972)
%3 = and i32 %2, 1
%4 = icmp eq i32 %3, 0
%5 = select i1 %4, i16 948, i16 980
%6 = or i16 %5, 1
%7 = tail call i32 @OUTB(i16 zeroext %5, i32 48)
%8 = tail call i32 @OUTB(i16 zeroext %6, i32 87)
%9 = tail call i32 @OUTB(i16 zeroext %6, i32 76)
%10 = tail call i32 @OUTB(i16 zeroext %5, i32 63)
%11 = icmp eq i32 %0, 0
%12 = xor i1 %11, true
%13 = zext i1 %12 to i32
%14 = tail call i32 @OUTB(i16 zeroext %6, i32 %13)
br i1 %11, label %15, label %24
15: ; preds = %1
%16 = tail call i32 (...) @gu1_detect_vsa2()
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = tail call i32 @OUTB(i16 zeroext %5, i32 51)
br label %20
20: ; preds = %20, %18
%21 = tail call i32 @INB(i16 zeroext %6)
%22 = and i32 %21, 128
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %20
24: ; preds = %20, %15, %1
%25 = tail call i32 @OUTB(i16 zeroext %5, i32 48)
%26 = tail call i32 @OUTB(i16 zeroext %6, i32 0)
ret i32 0
}