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


x86 assembly (clang, -O0):
<read_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 <read_all+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    121f <read_all+0xef>
jmp    1164 <read_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 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11a8 <read_all+0x78>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11a3 <read_all+0x73>
jmp    11a8 <read_all+0x78>
jmp    1164 <read_all+0x34>
cmpl   $0x0,-0x18(%rbp)
jne    11d5 <read_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    121f <read_all+0xef>
cmpl   $0x0,-0x18(%rbp)
jge    1219 <read_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    121f <read_all+0xef>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    116f <read_all+0x3f>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <read@plt>
test   %eax,%eax
jle    1173 <read_all+0x43>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1150 <read_all+0x20>
jmp    11b6 <read_all+0x86>
xor    %ebx,%ebx
jmp    11b6 <read_all+0x86>
mov    0x2e66(%rip),%rax
mov    (%rax),%ebx
je     11a1 <read_all+0x71>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe82(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11b1 <read_all+0x81>
lea    0xe58(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%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 = [55 x i8] c"Failed to read data from the socket connection - (%s)\0A\00"
@errno = global i32 0
define i32 @read_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 %50
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 @read(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 %50
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 ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %46)
store i32 -1, i32* %4
br label %50
48: ; preds = %40
%49 = load i32, i32* %9
store i32 %49, i32* %4
br label %50
50: ; preds = %48, %43, %37, %12
%51 = load i32, i32* %4
ret i32 %51
}

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