#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*/ assert(int);
int write(int, char *, int);
int tcp_write_fully(int fd, void *buf_, int count) {
  int tw = 0;
  char *buf = buf_;
  assert(fd > 0);
  assert(buf != NULL);
  assert(count >= 0);
  while (tw < count) {
    int nw = write(fd, buf + tw, count - tw);
    if (nw > 0) {
      tw += nw;
    } else if (nw == 0) {
      return -1;
    } else {
      return -1;
    }
  }
  return tw;
}


x86 assembly (clang, -O0):
<tcp_write_fully>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0x14(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11dc <tcp_write_fully+0xbc>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11b5 <tcp_write_fully+0x95>
mov    -0x24(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11d7 <tcp_write_fully+0xb7>
cmpl   $0x0,-0x24(%rbp)
jne    11cb <tcp_write_fully+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <tcp_write_fully+0xc2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <tcp_write_fully+0xc2>
jmp    1175 <tcp_write_fully+0x55>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_write_fully>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %edi,%edi
test   %r15d,%r15d
setg   %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
mov    %ebx,%edi
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
xor    %ebp,%ebp
nopl   0x0(%rax)
cmp    %ebx,%ebp
jge    1181 <tcp_write_fully+0x61>
movslq %ebp,%rsi
add    %r14,%rsi
mov    %ebx,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1030 <write@plt>
add    %eax,%ebp
test   %eax,%eax
jg     1160 <tcp_write_fully+0x40>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @tcp_write_fully(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %6
store i8* %11, i8** %9
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %7
%21 = icmp sge i32 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
br label %24
24: ; preds = %49, %3
%25 = load i32, i32* %8
%26 = load i32, i32* %7
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %50
28: ; preds = %24
%29 = load i32, i32* %5
%30 = load i8*, i8** %9
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %7
%35 = load i32, i32* %8
%36 = sub nsw i32 %34, %35
%37 = call i32 @write(i32 %29, i8* %33, i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %28
%41 = load i32, i32* %10
%42 = load i32, i32* %8
%43 = add nsw i32 %42, %41
store i32 %43, i32* %8
br label %49
44: ; preds = %28
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 -1, i32* %4
br label %52
48: ; preds = %44
store i32 -1, i32* %4
br label %52
49: ; preds = %40
br label %24
50: ; preds = %24
%51 = load i32, i32* %8
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %48, %47
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
define i32 @tcp_write_fully(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %0, 0
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8* %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = xor i32 %2, -1
%11 = lshr i32 %10, 31
%12 = tail call i32 @assert(i32 %11)
br label %13
13: ; preds = %16, %3
%14 = phi i32 [ 0, %3 ], [ %22, %16 ]
%15 = icmp slt i32 %14, %2
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = sext i32 %14 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = sub nsw i32 %2, %14
%20 = tail call i32 @write(i32 %0, i8* %18, i32 %19)
%21 = icmp sgt i32 %20, 0
%22 = add nsw i32 %20, %14
br i1 %21, label %13, label %23
23: ; preds = %13, %16
%24 = phi i32 [ -1, %16 ], [ %14, %13 ]
ret i32 %24
}