#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char const *, int);
int write_all(int conn_fd, const char *msg_buff, int nbytes) {
  int send_size;
  int tot_size = 0;
  if (nbytes <= 0)
    return 0;
  while ((send_size = write(conn_fd, msg_buff + tot_size, nbytes - tot_size)) >
         0) {
    tot_size += send_size;
    if (tot_size >= nbytes)
      break;
  }
  if (send_size == 0) {
    fprintf(stderr, "Server disconnected\n");
    return -1;
  }
  if (send_size < 0) {
    fprintf(stderr, "Failed to write data to the socket connection - (%s)\n",
            strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     115f <write_all+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    1220 <write_all+0xf0>
jmp    1164 <write_all+0x34>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11a8 <write_all+0x78>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11a3 <write_all+0x73>
jmp    11a8 <write_all+0x78>
jmp    1164 <write_all+0x34>
cmpl   $0x0,-0x18(%rbp)
jne    11d5 <write_all+0xa5>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <write_all+0xf0>
cmpl   $0x0,-0x18(%rbp)
jge    1219 <write_all+0xe9>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <write_all+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    11a3 <write_all+0x73>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
jle    116d <write_all+0x3d>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1150 <write_all+0x20>
test   %eax,%eax
je     11a7 <write_all+0x77>
xor    %ecx,%ecx
test   %eax,%eax
jns    11c3 <write_all+0x93>
mov    0x2e62(%rip),%rax
mov    (%rax),%ebx
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe80(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11be <write_all+0x8e>
xor    %ecx,%ecx
jmp    11c3 <write_all+0x93>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Server disconnected\0A\00"
@.str.1 = [54 x i8] c"Failed to write data to the socket connection - (%s)\0A\00"
@errno = global i32 0
define i32 @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
store i32 0, i32* %9
%10 = load i32, i32* %7
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 0, i32* %4
br label %49
13: ; preds = %3
br label %14
14: ; preds = %33, %13
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %9
%22 = sub nsw i32 %20, %21
%23 = call i32 @write(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %8
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %14
%26 = load i32, i32* %8
%27 = load i32, i32* %9
%28 = add nsw i32 %27, %26
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = load i32, i32* %7
%31 = icmp sge i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %25
br label %34
33: ; preds = %25
br label %14
34: ; preds = %32, %14
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %49
40: ; preds = %34
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %46)
store i32 -1, i32* %4
br label %49
48: ; preds = %40
store i32 0, i32* %4
br label %49
49: ; preds = %48, %43, %37, %12
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Server disconnected\0A\00"
@.str.1 = [54 x i8] c"Failed to write data to the socket connection - (%s)\0A\00"
@errno = global i32 0
define i32 @write_all(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp slt i32 %2, 1
br i1 %4, label %27, label %5
5: ; preds = %3, %5
%6 = phi i32 [ %12, %5 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @write(i32 %0, i8* %8, i32 %9)
%11 = icmp sgt i32 %10, 0
%12 = add nsw i32 %10, %6
%13 = icmp slt i32 %12, %2
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %5, label %15
15: ; preds = %5
%16 = icmp eq i32 %10, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %27
20: ; preds = %15
%21 = icmp slt i32 %10, 0
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %20, %3, %22, %17
%28 = phi i32 [ -1, %17 ], [ -1, %22 ], [ 0, %3 ], [ 0, %20 ]
ret i32 %28
}