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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ write(int, int /*<<< orphan*/ const *, size_t const);
void writeToClient(int s, const uint8_t *buf, const size_t buflen) {
  ssize_t actual;
  size_t written = 0;
  while (written < buflen) {
    actual = write(s, buf + written, buflen - written);
    if (actual <= 0) {
      exit(1);
    }
    written += actual;
  }
}


x86 assembly (clang, -O0):
<writeToClient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1192 <writeToClient+0x72>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     1181 <writeToClient+0x61>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    113b <writeToClient+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeToClient>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     115f <writeToClient+0x3f>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
test   %rax,%rax
jle    116a <writeToClient+0x4a>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1140 <writeToClient+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1040 <exit@plt>

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

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