#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 /*<<< orphan*/ assert(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tcdrain(int);
int write(int, char const *, size_t);
int UART_send(int portHandle, const char *buf, size_t len) {
  unsigned int count = 0;
  if (portHandle < 0) {
    printf("ERROR(UART_send): Port not open.\n");
    return -1;
  }
  tcdrain(portHandle);
  while (count < len) {
    int ret = write(portHandle, buf, len);
    assert(ret > 0);
    count += ret;
  }
  return count;
}


x86 assembly (clang, -O0):
<UART_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    117c <UART_send+0x3c>
lea    0xe95(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <UART_send+0x89>
mov    -0x8(%rbp),%edi
call   1030 <tcdrain@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    11c3 <UART_send+0x83>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1184 <UART_send+0x44>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UART_send>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edi,%edi
js     1196 <UART_send+0x56>
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r15d
call   1040 <tcdrain@plt>
xor    %ebp,%ebp
test   %r12,%r12
je     11a7 <UART_send+0x67>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1050 <write@plt>
mov    %eax,%ebx
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1060 <assert@plt>
add    %ebx,%ebp
cmp    %r12,%rbp
jb     1170 <UART_send+0x30>
jmp    11a7 <UART_send+0x67>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"ERROR(UART_send): Port not open.\0A\00"
define i32 @UART_send(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %36
14: ; preds = %3
%15 = load i32, i32* %5
%16 = call i32 @tcdrain(i32 %15)
br label %17
17: ; preds = %22, %14
%18 = load i32, i32* %8
%19 = zext i32 %18 to i64
%20 = load i64, i64* %7
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %34
22: ; preds = %17
%23 = load i32, i32* %5
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = call i32 @write(i32 %23, i8* %24, i64 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp sgt i32 %27, 0
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i32, i32* %9
%32 = load i32, i32* %8
%33 = add i32 %32, %31
store i32 %33, i32* %8
br label %17
34: ; preds = %17
%35 = load i32, i32* %8
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %12
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@str = [33 x i8] c"ERROR(UART_send): Port not open.\00"
define i32 @UART_send(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp slt i32 %0, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %19
7: ; preds = %3
%8 = tail call i32 @tcdrain(i32 %0)
%9 = icmp eq i64 %2, 0
br i1 %9, label %19, label %10
10: ; preds = %7, %10
%11 = phi i32 [ %16, %10 ], [ 0, %7 ]
%12 = tail call i32 @write(i32 %0, i8* %1, i64 %2)
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = add i32 %12, %11
%17 = zext i32 %16 to i64
%18 = icmp ult i64 %17, %2
br i1 %18, label %10, label %19
19: ; preds = %10, %7, %5
%20 = phi i32 [ -1, %5 ], [ 0, %7 ], [ %16, %10 ]
ret i32 %20
}