#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*/ pabort(char *);
int tcdrain(int);
int write(int, void const *, int);
void writetty(int fd, const void *data, size_t length) {
  for (size_t i = 0; i < length; ++i) {
    if (-1 == write(fd, data + i, 1)) {
      pabort("writing data to tty");
    }
    if (-1 == tcdrain(fd)) {
      pabort("draining tty");
    }
  }
}


x86 assembly (clang, -O0):
<writetty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c2 <writetty+0x92>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1189 <writetty+0x59>
lea    0xe7c(%rip),%rdi
call   1050 <pabort@plt>
mov    -0x4(%rbp),%edi
call   1030 <tcdrain@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11ac <writetty+0x7c>
lea    0xe6d(%rip),%rdi
call   1050 <pabort@plt>
jmp    11b1 <writetty+0x81>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114b <writetty+0x1b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writetty>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     119c <writetty+0x6c>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xeb4(%rip),%r15
lea    0xec1(%rip),%r12
jmp    116a <writetty+0x3a>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbx
add    $0xffffffffffffffff,%r14
je     119c <writetty+0x6c>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1186 <writetty+0x56>
mov    %r15,%rdi
call   1050 <pabort@plt>
mov    %ebp,%edi
call   1030 <tcdrain@plt>
cmp    $0xffffffff,%eax
jne    1160 <writetty+0x30>
mov    %r12,%rdi
call   1050 <pabort@plt>
jmp    1160 <writetty+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"writing data to tty\00"
@.str.1 = [13 x i8] c"draining tty\00"
define void @writetty(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %8
8: ; preds = %28, %3
%9 = load i64, i64* %7
%10 = load i64, i64* %6
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %31
12: ; preds = %8
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i64, i64* %7
%16 = getelementptr i8, i8* %14, i64 %15
%17 = call i32 @write(i32 %13, i8* %16, i32 1)
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @pabort(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = load i32, i32* %4
%23 = call i32 @tcdrain(i32 %22)
%24 = icmp eq i32 -1, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @pabort(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
br label %28
28: ; preds = %27
%29 = load i64, i64* %7
%30 = add i64 %29, 1
store i64 %30, i64* %7
br label %8
31: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"writing data to tty\00"
@.str.1 = [13 x i8] c"draining tty\00"
define void @writetty(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %6
5: ; preds = %18, %3
ret void
6: ; preds = %3, %18
%7 = phi i64 [ %19, %18 ], [ 0, %3 ]
%8 = getelementptr i8, i8* %1, i64 %7
%9 = tail call i32 @write(i32 %0, i8* %8, i32 1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @pabort(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %6
%14 = tail call i32 @tcdrain(i32 %0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @pabort(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %13, %16
%19 = add nuw i64 %7, 1
%20 = icmp eq i64 %19, %2
br i1 %20, label %5, label %6
}