#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;

/* Variables and functions */
int read(int, int /*<<< orphan*/ *, int);
int read_socket(int client_fd, void *buf, int buf_size) {
  int ecode, to_read = buf_size;
  uint8_t *ptr = (uint8_t *)buf;
  while (to_read > 0) {
    ecode = read(client_fd, ptr, to_read);
    if (ecode <= 0)
      return -1;
    ptr += ecode;
    to_read -= ecode;
  }
  return buf_size;
}


x86 assembly (clang, -O0):
<read_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1187 <read_socket+0x77>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1162 <read_socket+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    118d <read_socket+0x7d>
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1130 <read_socket+0x20>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
test   %edx,%edx
jle    1153 <read_socket+0x43>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %r14d,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    114d <read_socket+0x3d>
mov    %eax,%ecx
lea    (%rbx,%rcx,4),%rbx
sub    %eax,%ebp
jg     1130 <read_socket+0x20>
jmp    1153 <read_socket+0x43>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_socket(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
store i32 %11, i32* %9
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %10
br label %14
14: ; preds = %25, %3
%15 = load i32, i32* %9
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = load i32, i32* %5
%19 = load i32*, i32** %10
%20 = load i32, i32* %9
%21 = call i32 @read(i32 %18, i32* %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %4
br label %35
25: ; preds = %17
%26 = load i32, i32* %8
%27 = load i32*, i32** %10
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i32, i32* %27, i64 %28
store i32* %29, i32** %10
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %9
br label %14
33: ; preds = %14
%34 = load i32, i32* %7
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %24
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
define i32 @read_socket(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %17
5: ; preds = %3
%6 = bitcast i8* %1 to i32*
br label %7
7: ; preds = %5, %12
%8 = phi i32* [ %14, %12 ], [ %6, %5 ]
%9 = phi i32 [ %15, %12 ], [ %2, %5 ]
%10 = tail call i32 @read(i32 %0, i32* %8, i32 %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %17, label %12
12: ; preds = %7
%13 = zext i32 %10 to i64
%14 = getelementptr inbounds i32, i32* %8, i64 %13
%15 = sub nsw i32 %9, %10
%16 = icmp sgt i32 %15, 0
br i1 %16, label %7, label %17
17: ; preds = %7, %12, %3
%18 = phi i32 [ %2, %3 ], [ %2, %12 ], [ -1, %7 ]
ret i32 %18
}