#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 recv(int, char *, int, int /*<<< orphan*/);
int ts_recv(int dc_socket, char *buffer, int dc_length, int flags) {
  int was_received, to_be_received, res2;
  was_received = 0;
  to_be_received = dc_length;
  for (;;) {
    res2 = recv(dc_socket, buffer + was_received, to_be_received, 0);
    if ((res2 == (-1)) || (res2 == 0))
      return res2;
    if (res2 == to_be_received)
      return dc_length;
    was_received = was_received + res2;
    to_be_received = to_be_received - res2;
  }
}


x86 assembly (clang, -O0):
<ts_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     1161 <ts_recv+0x51>
cmpl   $0x0,-0x24(%rbp)
jne    116c <ts_recv+0x5c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119a <ts_recv+0x8a>
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1183 <ts_recv+0x73>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119a <ts_recv+0x8a>
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1132 <ts_recv+0x22>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ts_recv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
add    $0x1,%ecx
cmp    $0x2,%ecx
jb     1167 <ts_recv+0x57>
mov    %r14d,%ebx
nopw   0x0(%rax,%rax,1)
sub    %eax,%ebx
je     1164 <ts_recv+0x54>
add    %eax,%ebp
movslq %ebp,%rsi
add    %r15,%rsi
mov    %r12d,%edi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
add    $0x1,%ecx
cmp    $0x1,%ecx
ja     1140 <ts_recv+0x30>
jmp    1167 <ts_recv+0x57>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @ts_recv(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @recv(i32 %0, i8* %1, i32 %2, i32 0)
%6 = add i32 %5, 1
%7 = icmp ult i32 %6, 2
br i1 %7, label %21, label %8
8: ; preds = %4, %13
%9 = phi i32 [ %18, %13 ], [ %5, %4 ]
%10 = phi i32 [ %15, %13 ], [ %2, %4 ]
%11 = phi i32 [ %14, %13 ], [ 0, %4 ]
%12 = icmp eq i32 %10, %9
br i1 %12, label %21, label %13
13: ; preds = %8
%14 = add nsw i32 %9, %11
%15 = sub nsw i32 %10, %9
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = tail call i32 @recv(i32 %0, i8* %17, i32 %15, i32 0)
%19 = add i32 %18, 1
%20 = icmp ult i32 %19, 2
br i1 %20, label %21, label %8
21: ; preds = %13, %8, %4
%22 = phi i32 [ %5, %4 ], [ %2, %8 ], [ %18, %13 ]
ret i32 %22
}