#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 read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int serialport_readbytes(int fd, char *buf, int len) {
  char b[1];
  int i = 0;
  do {
    int n = read(fd, b, 1);
    if (n == -1)
      return -1;
    if (n == 0) {
      usleep(1 * 1000);
      continue;
    }
    buf[i] = b[0];
    i++;
  } while (i < len);
  return 0;
}


x86 assembly (clang, -O0):
<serialport_readbytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1163 <serialport_readbytes+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <serialport_readbytes+0x86>
cmpl   $0x0,-0x20(%rbp)
jne    117c <serialport_readbytes+0x5c>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
jmp    1193 <serialport_readbytes+0x73>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1139 <serialport_readbytes+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_readbytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
jmp    114f <serialport_readbytes+0x2f>
nopl   (%rax)
mov    $0x3e8,%edi
call   1040 <usleep@plt>
cmp    %r12d,%ebx
jge    117b <serialport_readbytes+0x5b>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
je     1140 <serialport_readbytes+0x20>
cmp    $0xffffffff,%eax
je     117d <serialport_readbytes+0x5d>
movzbl 0xf(%rsp),%eax
movslq %ebx,%rbx
mov    %al,(%r14,%rbx,1)
add    $0x1,%ebx
cmp    %r12d,%ebx
jl     114f <serialport_readbytes+0x2f>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @serialport_readbytes(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [1 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %11
11: ; preds = %32, %3
%12 = load i32, i32* %5
%13 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 1)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %11
store i32 -1, i32* %4
br label %37
18: ; preds = %11
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @usleep(i32 1000)
br label %32
23: ; preds = %18
%24 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%25 = load i8, i8* %24
%26 = load i8*, i8** %6
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %32
32: ; preds = %23, %21
%33 = load i32, i32* %9
%34 = load i32, i32* %7
%35 = icmp slt i32 %33, %34
br i1 %35, label %11, label %36
36: ; preds = %32
store i32 0, i32* %4
br label %37
37: ; preds = %36, %17
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @serialport_readbytes(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca [1 x i8]
%5 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
br label %6
6: ; preds = %16, %3
%7 = phi i32 [ 0, %3 ], [ %17, %16 ]
%8 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %8, label %11 [
i32 -1, label %19
i32 0, label %9
]
9: ; preds = %6
%10 = tail call i32 @usleep(i32 1000)
br label %16
11: ; preds = %6
%12 = load i8, i8* %5
%13 = sext i32 %7 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 %12, i8* %14
%15 = add nsw i32 %7, 1
br label %16
16: ; preds = %9, %11
%17 = phi i32 [ %15, %11 ], [ %7, %9 ]
%18 = icmp slt i32 %17, %2
br i1 %18, label %6, label %19
19: ; preds = %6, %16
%20 = phi i32 [ 0, %16 ], [ %8, %6 ]
ret i32 %20
}