#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, char);
scalar_t__ serial_is_transmit_fifo_empty(unsigned short);
int serial_write(unsigned short com, char *buf, unsigned int len) {
  unsigned int i;
  for (i = 0; i < len; i++) {
    while (serial_is_transmit_fifo_empty(com) == 0)
      ;
    outb(com, buf[i]);
  }
  return len;
}


x86 assembly (clang, -O0):
<serial_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    118b <serial_write+0x6b>
jmp    114e <serial_write+0x2e>
movzwl -0x2(%rbp),%edi
call   1040 <serial_is_transmit_fifo_empty@plt>
cmp    $0x0,%rax
jne    1166 <serial_write+0x46>
jmp    114e <serial_write+0x2e>
mov    -0x2(%rbp),%dx
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movzwl %dx,%edi
movsbl (%rax,%rcx,1),%esi
call   1030 <outb@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113d <serial_write+0x1d>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
test   %edx,%edx
je     1161 <serial_write+0x41>
mov    %rsi,%r15
mov    %r14d,%r12d
xor    %ebx,%ebx
movzwl %di,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <serial_is_transmit_fifo_empty@plt>
test   %rax,%rax
je     1140 <serial_write+0x20>
movsbl (%r15,%rbx,1),%esi
mov    %ebp,%edi
call   1030 <outb@plt>
add    $0x1,%rbx
cmp    %r12,%rbx
jne    1140 <serial_write+0x20>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @serial_write(i16 zeroext %0, i8* %1, i32 %2) {
%4 = alloca i16
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i16 %0, i16* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %26, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = icmp ult i32 %9, %10
br i1 %11, label %12, label %29
12: ; preds = %8
br label %13
13: ; preds = %17, %12
%14 = load i16, i16* %4
%15 = call i64 @serial_is_transmit_fifo_empty(i16 zeroext %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
br label %13
18: ; preds = %13
%19 = load i16, i16* %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = call i32 @outb(i16 zeroext %19, i8 signext %24)
br label %26
26: ; preds = %18
%27 = load i32, i32* %7
%28 = add i32 %27, 1
store i32 %28, i32* %7
br label %8
29: ; preds = %8
%30 = load i32, i32* %6
ret i32 %30
}

LLVM IR (-O2):
define i32 @serial_write(i16 zeroext %0, i8* nocapture %1, i32 returned %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %18, label %5
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %12
%8 = phi i64 [ 0, %5 ], [ %16, %12 ]
br label %9
9: ; preds = %7, %9
%10 = tail call i64 @serial_is_transmit_fifo_empty(i16 zeroext %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %9, label %12
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %1, i64 %8
%14 = load i8, i8* %13
%15 = tail call i32 @outb(i16 zeroext %0, i8 signext %14)
%16 = add nuw nsw i64 %8, 1
%17 = icmp eq i64 %16, %6
br i1 %17, label %18, label %7
18: ; preds = %12, %3
ret i32 %2
}