#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 write(int, char const *, int);
int guac_terminal_write_all(int fd, const char *buffer, int size) {
  int remaining = size;
  while (remaining > 0) {
    int ret_val = write(fd, buffer, remaining);
    if (ret_val <= 0)
      return -1;
    remaining -= ret_val;
    buffer += ret_val;
  }
  return size;
}


x86 assembly (clang, -O0):
<guac_terminal_write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    117b <guac_terminal_write_all+0x6b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     115a <guac_terminal_write_all+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1181 <guac_terminal_write_all+0x71>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1128 <guac_terminal_write_all+0x18>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guac_terminal_write_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %edx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebp,%ebp
jle    1152 <guac_terminal_write_all+0x42>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
sub    %eax,%ebp
cltq
add    %rax,%rbx
test   %eax,%eax
jg     1130 <guac_terminal_write_all+0x20>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @guac_terminal_write_all(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
store i32 %10, i32* %8
br label %11
11: ; preds = %22, %3
%12 = load i32, i32* %8
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = call i32 @write(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 -1, i32* %4
br label %32
22: ; preds = %14
%23 = load i32, i32* %9
%24 = load i32, i32* %8
%25 = sub nsw i32 %24, %23
store i32 %25, i32* %8
%26 = load i32, i32* %9
%27 = load i8*, i8** %6
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8* %29, i8** %6
br label %11
30: ; preds = %11
%31 = load i32, i32* %7
store i32 %31, i32* %4
br label %32
32: ; preds = %30, %21
%33 = load i32, i32* %4
ret i32 %33
}

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