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


x86 assembly (clang, -O0):
<uart_write>:
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
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
mov    $0x2,%edi
call   1030 <usb_bulk@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    115f <uart_write+0x4f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1181 <uart_write+0x71>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    112f <uart_write+0x1f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_write>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rsi
add    %r14,%rsi
mov    $0x2,%edi
call   1030 <usb_bulk@plt>
test   %eax,%eax
js     113c <uart_write+0x2c>
add    %eax,%ebx
mov    %ebp,%edx
sub    %ebx,%edx
jne    1120 <uart_write+0x10>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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