#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*/ EINTR;
int /*<<< orphan*/ errno;
int recv(int, char *, int, int /*<<< orphan*/);
int ssgl_readsocket(int fd, void *buffer, int buflen) {
  char *c = buffer;
  int rc, len = buflen;
  do {
    rc = recv(fd, c, len, 0);
    if (rc == 0)
      return -1;
    if (rc == len)
      return 0;
    if (rc < 0) {
      if (errno == -EINTR)
        continue;
      else
        return rc;
    }
    len -= rc;
    c += rc;
  } while (1);
}


x86 assembly (clang, -O0):
<ssgl_readsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    115a <ssgl_readsocket+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <ssgl_readsocket+0xc3>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    1172 <ssgl_readsocket+0x62>
movl   $0x0,-0x4(%rbp)
jmp    11d3 <ssgl_readsocket+0xc3>
cmpl   $0x0,-0x24(%rbp)
jge    11a8 <ssgl_readsocket+0x98>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2e4a(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    119d <ssgl_readsocket+0x8d>
jmp    11c4 <ssgl_readsocket+0xb4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d3 <ssgl_readsocket+0xc3>
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    1130 <ssgl_readsocket+0x20>
jmp    11d3 <ssgl_readsocket+0xc3>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssgl_readsocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     116f <ssgl_readsocket+0x5f>
mov    0x2eae(%rip),%r15
mov    0x2e9f(%rip),%r12
jmp    115a <ssgl_readsocket+0x4a>
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
add    %rax,%rbx
mov    %ecx,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     116f <ssgl_readsocket+0x5f>
mov    %ebp,%ecx
sub    %eax,%ecx
je     1176 <ssgl_readsocket+0x66>
test   %eax,%eax
jns    1140 <ssgl_readsocket+0x30>
mov    (%r15),%ecx
add    (%r12),%ecx
je     1147 <ssgl_readsocket+0x37>
jmp    1178 <ssgl_readsocket+0x68>
mov    $0xffffffff,%eax
jmp    1178 <ssgl_readsocket+0x68>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
define i32 @ssgl_readsocket(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
%12 = load i32, i32* %7
store i32 %12, i32* %10
br label %13
13: ; preds = %45, %3
%14 = load i32, i32* %5
%15 = load i8*, i8** %8
%16 = load i32, i32* %10
%17 = call i32 @recv(i32 %14, i8* %15, i32 %16, i32 0)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 -1, i32* %4
br label %46
21: ; preds = %13
%22 = load i32, i32* %9
%23 = load i32, i32* %10
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %4
br label %46
26: ; preds = %21
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = load i32, i32* @EINTR
%32 = sub nsw i32 0, %31
%33 = icmp eq i32 %30, %32
br i1 %33, label %34, label %35
34: ; preds = %29
br label %45
35: ; preds = %29
%36 = load i32, i32* %9
store i32 %36, i32* %4
br label %46
37: ; preds = %26
%38 = load i32, i32* %9
%39 = load i32, i32* %10
%40 = sub nsw i32 %39, %38
store i32 %40, i32* %10
%41 = load i32, i32* %9
%42 = load i8*, i8** %8
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8* %44, i8** %8
br label %45
45: ; preds = %37, %34
br i1 true, label %13, label %46
46: ; preds = %20, %25, %35, %45
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
define i32 @ssgl_readsocket(i32 %0, i8* %1, i32 %2) {
%4 = tail call i32 @recv(i32 %0, i8* %1, i32 %2, i32 0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %27, label %6
6: ; preds = %3, %22
%7 = phi i32 [ %25, %22 ], [ %4, %3 ]
%8 = phi i32 [ %24, %22 ], [ %2, %3 ]
%9 = phi i8* [ %23, %22 ], [ %1, %3 ]
%10 = icmp eq i32 %8, %7
br i1 %10, label %27, label %11
11: ; preds = %6
%12 = icmp slt i32 %7, 0
br i1 %12, label %13, label %18
13: ; preds = %11
%14 = load i32, i32* @errno
%15 = load i32, i32* @EINTR
%16 = sub nsw i32 0, %15
%17 = icmp eq i32 %14, %16
br i1 %17, label %22, label %27
18: ; preds = %11
%19 = sub nsw i32 %8, %7
%20 = zext i32 %7 to i64
%21 = getelementptr inbounds i8, i8* %9, i64 %20
br label %22
22: ; preds = %13, %18
%23 = phi i8* [ %9, %13 ], [ %21, %18 ]
%24 = phi i32 [ %8, %13 ], [ %19, %18 ]
%25 = tail call i32 @recv(i32 %0, i8* %23, i32 %24, i32 0)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %6
27: ; preds = %22, %6, %13, %3
%28 = phi i32 [ -1, %3 ], [ %7, %13 ], [ 0, %6 ], [ -1, %22 ]
ret i32 %28
}