#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 isa_read8(int);
int serial_read(void *_buf, int len) {
  unsigned char *buf = _buf;
  int i;
  for (i = 0; i < len; i++) {
    while ((isa_read8(0x2fd) & 1) == 0)
      ;
    buf[i] = isa_read8(0x2f8);
  }
  return i;
}


x86 assembly (clang, -O0):
<serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    117f <serial_read+0x6f>
jmp    113f <serial_read+0x2f>
mov    $0x2fd,%edi
call   1030 <isa_read8@plt>
and    $0x1,%eax
cmp    $0x0,%eax
jne    115a <serial_read+0x4a>
jmp    113f <serial_read+0x2f>
mov    $0x2f8,%edi
call   1030 <isa_read8@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112e <serial_read+0x1e>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    1157 <serial_read+0x47>
mov    %esi,%r14d
mov    %rdi,%r15
mov    %esi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x2fd,%edi
call   1030 <isa_read8@plt>
test   $0x1,%al
je     1130 <serial_read+0x20>
mov    $0x2f8,%edi
call   1030 <isa_read8@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbp,%rbx
jne    1130 <serial_read+0x20>
jmp    115a <serial_read+0x4a>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @serial_read(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
store i8* %7, i8** %5
store i32 0, i32* %6
br label %8
8: ; preds = %25, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %28
12: ; preds = %8
br label %13
13: ; preds = %17, %12
%14 = call i32 @isa_read8(i32 765)
%15 = and i32 %14, 1
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
br label %13
18: ; preds = %13
%19 = call i32 @isa_read8(i32 760)
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 %20, i8* %24
br label %25
25: ; preds = %18
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %8
28: ; preds = %8
%29 = load i32, i32* %6
ret i32 %29
}

LLVM IR (-O2):
define i32 @serial_read(i8* nocapture writeonly %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %18
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %12
%7 = phi i64 [ 0, %4 ], [ %16, %12 ]
br label %8
8: ; preds = %6, %8
%9 = tail call i32 @isa_read8(i32 765)
%10 = and i32 %9, 1
%11 = icmp eq i32 %10, 0
br i1 %11, label %8, label %12
12: ; preds = %8
%13 = tail call i32 @isa_read8(i32 760)
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 %14, i8* %15
%16 = add nuw nsw i64 %7, 1
%17 = icmp eq i64 %16, %5
br i1 %17, label %18, label %6
18: ; preds = %12, %2
%19 = phi i32 [ 0, %2 ], [ %1, %12 ]
ret i32 %19
}