#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(int);
int /*<<< orphan*/ outb(int, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int serial_write(const void *buffer, int len) {
  const char *p;
  for (p = buffer; len > 0; len--) {
    if (!(inb(0x3F8 + (5)) & 0x20))
      usleep(100);
    outb(0x3F8, *p++);
  }
  return 0;
}


x86 assembly (clang, -O0):
<serial_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    119b <serial_write+0x6b>
mov    $0x3fd,%edi
call   1040 <inb@plt>
and    $0x20,%eax
cmp    $0x0,%eax
jne    1171 <serial_write+0x41>
mov    $0x64,%edi
call   1050 <usleep@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%esi
mov    $0x3f8,%edi
call   1030 <outb@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1147 <serial_write+0x17>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_write>:
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    1184 <serial_write+0x54>
mov    %esi,%ebx
mov    %rdi,%r14
add    $0x1,%ebx
jmp    116a <serial_write+0x3a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r14),%esi
add    $0x1,%r14
mov    $0x3f8,%edi
call   1030 <outb@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jle    1184 <serial_write+0x54>
mov    $0x3fd,%edi
call   1040 <inb@plt>
test   $0x20,%al
jne    1150 <serial_write+0x20>
mov    $0x64,%edi
call   1050 <usleep@plt>
jmp    1150 <serial_write+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @serial_write(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
store i8* %6, i8** %5
br label %7
7: ; preds = %22, %2
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %7
%11 = call i32 @inb(i32 1021)
%12 = and i32 %11, 32
%13 = icmp ne i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = call i32 @usleep(i32 100)
br label %16
16: ; preds = %14, %10
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %5
%19 = load i8, i8* %17
%20 = sext i8 %19 to i32
%21 = call i32 @outb(i32 1016, i32 %20)
br label %22
22: ; preds = %16
%23 = load i32, i32* %4
%24 = add nsw i32 %23, -1
store i32 %24, i32* %4
br label %7
25: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
define i32 @serial_write(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %19
4: ; preds = %2, %12
%5 = phi i8* [ %13, %12 ], [ %0, %2 ]
%6 = phi i32 [ %17, %12 ], [ %1, %2 ]
%7 = tail call i32 @inb(i32 1021)
%8 = and i32 %7, 32
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @usleep(i32 100)
br label %12
12: ; preds = %10, %4
%13 = getelementptr inbounds i8, i8* %5, i64 1
%14 = load i8, i8* %5
%15 = sext i8 %14 to i32
%16 = tail call i32 @outb(i32 1016, i32 %15)
%17 = add nsw i32 %6, -1
%18 = icmp sgt i32 %6, 1
br i1 %18, label %4, label %19
19: ; preds = %12, %2
ret i32 0
}