#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int tcdrain(int);
scalar_t__ write(int, void const *, size_t);
int kwboot_tty_send(int fd, const void *buf, size_t len) {
  int rc;
  ssize_t n;
  if (!buf)
    return 0;
  rc = -1;
  do {
    n = write(fd, buf, len);
    if (n < 0)
      goto out;
    buf = (char *)buf + n;
    len -= n;
  } while (len > 0);
  rc = tcdrain(fd);
out:
  return rc;
}


x86 assembly (clang, -O0):
<kwboot_tty_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114a <kwboot_tty_send+0x2a>
movl   $0x0,-0x4(%rbp)
jmp    11ac <kwboot_tty_send+0x8c>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1175 <kwboot_tty_send+0x55>
jmp    11a6 <kwboot_tty_send+0x86>
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
ja     1151 <kwboot_tty_send+0x31>
mov    -0x8(%rbp),%edi
call   1030 <tcdrain@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_tty_send>:
push   %rbp
push   %r14
push   %rbx
test   %rsi,%rsi
je     1167 <kwboot_tty_send+0x47>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
test   %rax,%rax
js     116b <kwboot_tty_send+0x4b>
add    %rax,%rbp
sub    %rax,%rbx
jne    1140 <kwboot_tty_send+0x20>
mov    %r14d,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <tcdrain@plt>
xor    %eax,%eax
jmp    1170 <kwboot_tty_send+0x50>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @kwboot_tty_send(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
store i32 0, i32* %4
br label %37
13: ; preds = %3
store i32 -1, i32* %8
br label %14
14: ; preds = %29, %13
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i64 @write(i32 %15, i8* %16, i64 %17)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
br label %35
22: ; preds = %14
%23 = load i8*, i8** %6
%24 = load i64, i64* %9
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %6
%26 = load i64, i64* %9
%27 = load i64, i64* %7
%28 = sub i64 %27, %26
store i64 %28, i64* %7
br label %29
29: ; preds = %22
%30 = load i64, i64* %7
%31 = icmp ugt i64 %30, 0
br i1 %31, label %14, label %32
32: ; preds = %29
%33 = load i32, i32* %5
%34 = call i32 @tcdrain(i32 %33)
store i32 %34, i32* %8
br label %35
35: ; preds = %32, %21
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %12
%38 = load i32, i32* %4
ret i32 %38
}

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