#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*/ checked_write(int, char *, int);
int read(int, char *, int);
void write_to_tty(int ttyfd, int fillfd, int totalsize, int buffsize,
                  char buf[], int buffmaxsize) {
  checked_write(ttyfd, buf, buffsize);
  totalsize -= buffsize;
  while (totalsize > 0) {
    int readmax;
    readmax = (((totalsize) < (buffmaxsize)) ? (totalsize) : (buffmaxsize));
    buffsize = read(fillfd, buf, readmax);
    checked_write(ttyfd, buf, buffsize);
    totalsize -= buffsize;
  }
  return;
}


x86 assembly (clang, -O0):
<write_to_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <checked_write@plt>
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11b4 <write_to_tty+0x94>
mov    -0xc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1177 <write_to_tty+0x57>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    117d <write_to_tty+0x5d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <checked_write@plt>
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1156 <write_to_tty+0x36>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_tty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8,%r13
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %esi,%r15d
mov    %edi,%r12d
mov    %r8,%rsi
mov    %ecx,%edx
call   1030 <checked_write@plt>
sub    %ebx,%ebp
jle    1169 <write_to_tty+0x49>
cmp    %r14d,%ebp
mov    %r14d,%edx
cmovl  %ebp,%edx
mov    %r15d,%edi
mov    %r13,%rsi
call   1040 <read@plt>
mov    %eax,%ebx
mov    %r12d,%edi
mov    %r13,%rsi
mov    %eax,%edx
jmp    1140 <write_to_tty+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @write_to_tty(i32 %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i32 %5, i32* %12
%14 = load i32, i32* %7
%15 = load i8*, i8** %11
%16 = load i32, i32* %10
%17 = call i32 @checked_write(i32 %14, i8* %15, i32 %16)
%18 = load i32, i32* %10
%19 = load i32, i32* %9
%20 = sub nsw i32 %19, %18
store i32 %20, i32* %9
br label %21
21: ; preds = %32, %6
%22 = load i32, i32* %9
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %45
24: ; preds = %21
%25 = load i32, i32* %9
%26 = load i32, i32* %12
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* %9
br label %32
30: ; preds = %24
%31 = load i32, i32* %12
br label %32
32: ; preds = %30, %28
%33 = phi i32 [ %29, %28 ], [ %31, %30 ]
store i32 %33, i32* %13
%34 = load i32, i32* %8
%35 = load i8*, i8** %11
%36 = load i32, i32* %13
%37 = call i32 @read(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* %7
%39 = load i8*, i8** %11
%40 = load i32, i32* %10
%41 = call i32 @checked_write(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %10
%43 = load i32, i32* %9
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %9
br label %21
45: ; preds = %21
ret void
}

LLVM IR (-O2):
define void @write_to_tty(i32 %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = tail call i32 @checked_write(i32 %0, i8* %4, i32 %3)
%8 = sub nsw i32 %2, %3
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %6, %10
%11 = phi i32 [ %16, %10 ], [ %8, %6 ]
%12 = icmp slt i32 %11, %5
%13 = select i1 %12, i32 %11, i32 %5
%14 = tail call i32 @read(i32 %1, i8* %4, i32 %13)
%15 = tail call i32 @checked_write(i32 %0, i8* %4, i32 %14)
%16 = sub nsw i32 %11, %14
%17 = icmp sgt i32 %16, 0
br i1 %17, label %10, label %18
18: ; preds = %10, %6
ret void
}