#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 */
scalar_t__ read(int, char *, size_t);
int read_socket(int fd, char *buffer, int len) {
  int ret = 0;
  int count = 0;
  while (count < len) {
    ret = (int)read(fd, buffer + count, (size_t)(len - count));
    if (ret <= 0) {
      return (-1);
    }
    count += ret;
  }
  return count;
}


x86 assembly (clang, -O0):
<read_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117f <read_socket+0x6f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
movslq %eax,%rdx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1171 <read_socket+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1185 <read_socket+0x75>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1130 <read_socket+0x20>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1152 <read_socket+0x42>
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,%ecx
sub    %eax,%ecx
movslq %ecx,%rdx
mov    %r15d,%edi
call   1030 <read@plt>
test   %eax,%eax
jle    1156 <read_socket+0x46>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1130 <read_socket+0x20>
jmp    115b <read_socket+0x4b>
xor    %ebx,%ebx
jmp    115b <read_socket+0x4b>
mov    $0xffffffff,%ebx
mov    %ebx,%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
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %29, %3
%11 = load i32, i32* %9
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %33
14: ; preds = %10
%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 = sext i32 %22 to i64
%24 = call i64 @read(i32 %15, i8* %19, i64 %23)
%25 = trunc i64 %24 to i32
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %14
store i32 -1, i32* %4
br label %35
29: ; preds = %14
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = add nsw i32 %31, %30
store i32 %32, i32* %9
br label %10
33: ; preds = %10
%34 = load i32, i32* %9
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %28
%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 %7, label %17
5: ; preds = %7
%6 = icmp slt i32 %16, %2
br i1 %6, label %7, label %17
7: ; preds = %3, %5
%8 = phi i32 [ %16, %5 ], [ 0, %3 ]
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = sub nsw i32 %2, %8
%12 = sext i32 %11 to i64
%13 = tail call i64 @read(i32 %0, i8* %10, i64 %12)
%14 = trunc i64 %13 to i32
%15 = icmp slt i32 %14, 1
%16 = add nsw i32 %8, %14
br i1 %15, label %17, label %5
17: ; preds = %7, %5, %3
%18 = phi i32 [ 0, %3 ], [ %16, %5 ], [ -1, %7 ]
ret i32 %18
}