#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*/ printf(char *);
int read(int, char *, int);
int read_all(int socket_id, char *buffer, int size) {
  int bytes_read = 0;
  int retval;
  if (size < 0) {
    printf("size is <= 0, please check.\n");
    return -1;
  }
  if (!buffer) {
    printf("buffer is null, please check.\n");
    return -1;
  }
  while (bytes_read < size) {
    retval = read(socket_id, buffer + bytes_read, size - bytes_read);
    if (retval >= 0) {
      bytes_read += retval;
    } else {
      printf("error in reading data.\n");
      return -1;
    }
  }
  return 0;
}


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,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    115b <read_all+0x3b>
lea    0xeb6(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <read_all+0xc7>
cmpq   $0x0,-0x10(%rbp)
jne    117e <read_all+0x5e>
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <read_all+0xc7>
jmp    1183 <read_all+0x63>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e0 <read_all+0xc0>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11c3 <read_all+0xa3>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11db <read_all+0xbb>
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <read_all+0xc7>
jmp    1183 <read_all+0x63>
movl   $0x0,-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   %r12
push   %rbx
test   %edx,%edx
js     1171 <read_all+0x51>
mov    %rsi,%r14
test   %rsi,%rsi
je     117a <read_all+0x5a>
mov    %edx,%ebx
test   %edx,%edx
je     116d <read_all+0x4d>
mov    %edi,%r12d
xor    %ebp,%ebp
lea    0xed8(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1040 <read@plt>
test   %eax,%eax
js     1181 <read_all+0x61>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1150 <read_all+0x30>
xor    %eax,%eax
jmp    118e <read_all+0x6e>
lea    0xebd(%rip),%r15
jmp    1181 <read_all+0x61>
lea    0xe7f(%rip),%r15
mov    %r15,%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"size is <= 0, please check.\0A\00"
@.str.1 = [31 x i8] c"buffer is null, please check.\0A\00"
@.str.2 = [24 x i8] c"error in reading data.\0A\00"
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* %8
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
19: ; preds = %14
br label %20
20: ; preds = %42, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %43
24: ; preds = %20
%25 = load i32, i32* %5
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = sub nsw i32 %30, %31
%33 = call i32 @read(i32 %25, i8* %29, i32 %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %24
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = add nsw i32 %38, %37
store i32 %39, i32* %8
br label %42
40: ; preds = %24
%41 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
42: ; preds = %36
br label %20
43: ; preds = %20
store i32 0, i32* %4
br label %44
44: ; preds = %43, %40, %17, %12
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@str = [30 x i8] c"buffer is null, please check.\00"
@str.3 = [23 x i8] c"error in reading data.\00"
@str.4 = [28 x i8] c"size is <= 0, please check.\00"
define i32 @read_all(i32 %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %2, 0
br i1 %4, label %19, label %5
5: ; preds = %3
%6 = icmp eq i8* %1, null
br i1 %6, label %19, label %7
7: ; preds = %5
%8 = icmp eq i32 %2, 0
br i1 %8, label %22, label %11
9: ; preds = %11
%10 = icmp slt i32 %18, %2
br i1 %10, label %11, label %22
11: ; preds = %7, %9
%12 = phi i32 [ %18, %9 ], [ 0, %7 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = sub nsw i32 %2, %12
%16 = tail call i32 @read(i32 %0, i8* %14, i32 %15)
%17 = icmp sgt i32 %16, -1
%18 = add nsw i32 %16, %12
br i1 %17, label %9, label %19
19: ; preds = %11, %5, %3
%20 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.4, i64 0, i64 0), %3 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @str.3, i64 0, i64 0), %11 ]
%21 = tail call i32 @puts(i8* dereferenceable(1) %20)
br label %22
22: ; preds = %9, %19, %7
%23 = phi i32 [ 0, %7 ], [ -1, %19 ], [ 0, %9 ]
ret i32 %23
}