#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*/ outb(unsigned short, unsigned int);
int /*<<< orphan*/ serial_disable_dlab(unsigned short);
int /*<<< orphan*/ serial_enable_dlab(unsigned short);
void serial_set_baud_rate(unsigned short com, unsigned int frequency) {
  serial_enable_dlab(com);
  if (frequency == 0 || frequency > 115200)
    frequency = 115200;
  unsigned int divisor = 115200 / frequency;
  outb((com), divisor & 0x00FF);
  outb((com + 1), (divisor >> 8) & 0x00FF);
  serial_disable_dlab(com);
}


x86 assembly (clang, -O0):
<serial_set_baud_rate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    %esi,-0x8(%rbp)
movzwl -0x2(%rbp),%edi
call   1030 <serial_enable_dlab@plt>
cmpl   $0x0,-0x8(%rbp)
je     1162 <serial_set_baud_rate+0x32>
cmpl   $0x1c200,-0x8(%rbp)
jbe    1169 <serial_set_baud_rate+0x39>
movl   $0x1c200,-0x8(%rbp)
mov    $0x1c200,%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %eax,-0xc(%rbp)
mov    -0x2(%rbp),%ax
mov    -0xc(%rbp),%esi
and    $0xff,%esi
movzwl %ax,%edi
call   1040 <outb@plt>
movzwl -0x2(%rbp),%eax
add    $0x1,%eax
mov    -0xc(%rbp),%esi
shr    $0x8,%esi
and    $0xff,%esi
movzwl %ax,%edi
call   1040 <outb@plt>
movzwl -0x2(%rbp),%edi
call   1050 <serial_disable_dlab@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_set_baud_rate>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%r14d
call   1030 <serial_enable_dlab@plt>
lea    -0x1c201(%rbx),%eax
cmp    $0xfffe3e00,%eax
mov    $0x1c200,%eax
cmovb  %eax,%ebx
mov    $0x1c200,%eax
xor    %edx,%edx
div    %ebx
mov    %eax,%ebx
movzbl %bl,%esi
mov    %r14d,%edi
call   1040 <outb@plt>
lea    0x1(%r14),%eax
movzbl %bh,%esi
movzwl %ax,%edi
call   1040 <outb@plt>
mov    %r14d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <serial_disable_dlab@plt>

LLVM IR (-O0):
define void @serial_set_baud_rate(i16 zeroext %0, i32 %1) {
%3 = alloca i16
%4 = alloca i32
%5 = alloca i32
store i16 %0, i16* %3
store i32 %1, i32* %4
%6 = load i16, i16* %3
%7 = call i32 @serial_enable_dlab(i16 zeroext %6)
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp ugt i32 %11, 115200
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i32 115200, i32* %4
br label %14
14: ; preds = %13, %10
%15 = load i32, i32* %4
%16 = udiv i32 115200, %15
store i32 %16, i32* %5
%17 = load i16, i16* %3
%18 = load i32, i32* %5
%19 = and i32 %18, 255
%20 = call i32 @outb(i16 zeroext %17, i32 %19)
%21 = load i16, i16* %3
%22 = zext i16 %21 to i32
%23 = add nsw i32 %22, 1
%24 = trunc i32 %23 to i16
%25 = load i32, i32* %5
%26 = lshr i32 %25, 8
%27 = and i32 %26, 255
%28 = call i32 @outb(i16 zeroext %24, i32 %27)
%29 = load i16, i16* %3
%30 = call i32 @serial_disable_dlab(i16 zeroext %29)
ret void
}

LLVM IR (-O2):
define void @serial_set_baud_rate(i16 zeroext %0, i32 %1) {
%3 = tail call i32 @serial_enable_dlab(i16 zeroext %0)
%4 = add i32 %1, -115201
%5 = icmp ult i32 %4, -115200
%6 = select i1 %5, i32 115200, i32 %1
%7 = udiv i32 115200, %6
%8 = and i32 %7, 255
%9 = tail call i32 @outb(i16 zeroext %0, i32 %8)
%10 = add i16 %0, 1
%11 = lshr i32 %7, 8
%12 = and i32 %11, 255
%13 = tail call i32 @outb(i16 zeroext %10, i32 %12)
%14 = tail call i32 @serial_disable_dlab(i16 zeroext %0)
ret void
}