#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 usb_bulk(int, unsigned char *, int);
int uart_read(int com, unsigned char *data, int len) {
  int recv;
  int remain;
  int n;
  recv = 0;
  remain = len;
  do {
    n = usb_bulk(0x81, &data[len - remain], remain);
    if (n < 0) {
      return recv;
    }
    recv += n;
    remain -= n;
  } while (remain > 0);
  return recv;
}


x86 assembly (clang, -O0):
<uart_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
mov    $0x81,%edi
call   1030 <usb_bulk@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1163 <uart_read+0x53>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1187 <uart_read+0x77>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     112f <uart_read+0x1f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
xor    %ebx,%ebx
mov    %edx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%eax
sub    %ebp,%eax
movslq %eax,%rsi
add    %r14,%rsi
mov    $0x81,%edi
mov    %ebp,%edx
call   1030 <usb_bulk@plt>
test   %eax,%eax
js     1151 <uart_read+0x41>
add    %eax,%ebx
sub    %eax,%ebp
jg     1130 <uart_read+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @uart_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%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* %8
%11 = load i32, i32* %7
store i32 %11, i32* %9
br label %12
12: ; preds = %32, %3
%13 = load i8*, i8** %6
%14 = load i32, i32* %7
%15 = load i32, i32* %9
%16 = sub nsw i32 %14, %15
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %13, i64 %17
%19 = load i32, i32* %9
%20 = call i32 @usb_bulk(i32 129, i8* %18, i32 %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %37
25: ; preds = %12
%26 = load i32, i32* %10
%27 = load i32, i32* %8
%28 = add nsw i32 %27, %26
store i32 %28, i32* %8
%29 = load i32, i32* %10
%30 = load i32, i32* %9
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %9
br label %32
32: ; preds = %25
%33 = load i32, i32* %9
%34 = icmp sgt i32 %33, 0
br i1 %34, label %12, label %35
35: ; preds = %32
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %23
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @uart_read(i32 %0, i8* %1, i32 %2) {
br label %4
4: ; preds = %12, %3
%5 = phi i32 [ 0, %3 ], [ %13, %12 ]
%6 = phi i32 [ %2, %3 ], [ %14, %12 ]
%7 = sub nsw i32 %2, %6
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = tail call i32 @usb_bulk(i32 129, i8* %9, i32 %6)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %4
%13 = add nsw i32 %10, %5
%14 = sub nsw i32 %6, %10
%15 = icmp sgt i32 %14, 0
br i1 %15, label %4, label %16
16: ; preds = %12, %4
%17 = phi i32 [ %5, %4 ], [ %13, %12 ]
ret i32 %17
}