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

/* Variables and functions */
int /*<<< orphan*/ ECONNRESET;
int /*<<< orphan*/ errno;
scalar_t__ sock_read(int, char *, size_t);
int sock_readwait(int s, void *buf, size_t size) {
  char *cp;
  ssize_t nbytes;
  size_t left;
  cp = buf;
  left = size;
  while (left > 0) {
    nbytes = sock_read(s, cp, left);
    if (nbytes == 0) {
      errno = ECONNRESET;
      return (-1);
    }
    if (nbytes < 0)
      return (-1);
    left -= nbytes;
    cp += nbytes;
  }
  return (0);
}


x86 assembly (clang, -O0):
<sock_readwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11b2 <sock_readwait+0xa2>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <sock_read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117b <sock_readwait+0x6b>
mov    0x2e7c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <sock_readwait+0xa9>
cmpq   $0x0,-0x28(%rbp)
jge    1192 <sock_readwait+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <sock_readwait+0xa9>
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1133 <sock_readwait+0x23>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_readwait>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     1167 <sock_readwait+0x57>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <sock_read@plt>
test   %rax,%rax
je     114f <sock_readwait+0x3f>
js     1161 <sock_readwait+0x51>
add    %rax,%rbp
sub    %rax,%rbx
jne    1130 <sock_readwait+0x20>
jmp    1167 <sock_readwait+0x57>
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    0x2e79(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ECONNRESET = global i32 0
@errno = global i32 0
define i32 @sock_readwait(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
%12 = load i64, i64* %7
store i64 %12, i64* %10
br label %13
13: ; preds = %29, %3
%14 = load i64, i64* %10
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %36
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i64, i64* %10
%20 = call i64 @sock_read(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = load i32, i32* @ECONNRESET
store i32 %24, i32* @errno
store i32 -1, i32* %4
br label %37
25: ; preds = %16
%26 = load i64, i64* %9
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 -1, i32* %4
br label %37
29: ; preds = %25
%30 = load i64, i64* %9
%31 = load i64, i64* %10
%32 = sub i64 %31, %30
store i64 %32, i64* %10
%33 = load i64, i64* %9
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i64 %33
store i8* %35, i8** %8
br label %13
36: ; preds = %13
store i32 0, i32* %4
br label %37
37: ; preds = %36, %28, %23
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@ECONNRESET = global i32 0
@errno = global i32 0
define i32 @sock_readwait(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %18, label %5
5: ; preds = %3, %14
%6 = phi i64 [ %15, %14 ], [ %2, %3 ]
%7 = phi i8* [ %16, %14 ], [ %1, %3 ]
%8 = tail call i64 @sock_read(i32 %0, i8* %7, i64 %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = load i32, i32* @ECONNRESET
store i32 %11, i32* @errno
br label %18
12: ; preds = %5
%13 = icmp slt i64 %8, 0
br i1 %13, label %18, label %14
14: ; preds = %12
%15 = sub i64 %6, %8
%16 = getelementptr inbounds i8, i8* %7, i64 %8
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %5
18: ; preds = %12, %14, %3, %10
%19 = phi i32 [ -1, %10 ], [ 0, %3 ], [ -1, %12 ], [ 0, %14 ]
ret i32 %19
}