#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 send(int, char *, int, int /*<<< orphan*/);
int tsend_recv(int targetfd, char *bytes, int *bytes_size) {
  int bytes_sent = 0;
  do {
    bytes_sent = send(targetfd, bytes, *bytes_size, 0);
    if (bytes_sent == -1)
      return -1;
  } while (bytes_sent < *bytes_size);
  if ((*bytes_size = recv(targetfd, bytes, *bytes_size, 0)) < 1) {
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tsend_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1167 <tsend_recv+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <tsend_recv+0x91>
jmp    116c <tsend_recv+0x4c>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jl     113a <tsend_recv+0x1a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    -0x18(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x1,%eax
jge    11aa <tsend_recv+0x8a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11b1 <tsend_recv+0x91>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tsend_recv>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rdx),%edx
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
je     1166 <tsend_recv+0x46>
mov    (%r14),%edx
cmp    %edx,%eax
jl     1130 <tsend_recv+0x10>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,(%r14)
xor    %ecx,%ecx
test   %eax,%eax
setg   %cl
lea    (%rcx,%rcx,1),%eax
add    $0xfffffffe,%eax
jmp    116b <tsend_recv+0x4b>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @tsend_recv(i32 %0, i8* %1, i32* nocapture %2) {
%4 = load i32, i32* %2
br label %5
5: ; preds = %9, %3
%6 = phi i32 [ %10, %9 ], [ %4, %3 ]
%7 = tail call i32 @send(i32 %0, i8* %1, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %16, label %9
9: ; preds = %5
%10 = load i32, i32* %2
%11 = icmp slt i32 %7, %10
br i1 %11, label %5, label %12
12: ; preds = %9
%13 = tail call i32 @recv(i32 %0, i8* %1, i32 %10, i32 0)
store i32 %13, i32* %2
%14 = icmp sgt i32 %13, 0
%15 = select i1 %14, i32 0, i32 -2
br label %16
16: ; preds = %5, %12
%17 = phi i32 [ %15, %12 ], [ -1, %5 ]
ret i32 %17
}