#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*/ printf(char *, int);
int send(int, char const *, int, int /*<<< orphan*/);
int gdp_io_thread_internal_send(int sock, const char *data, int length) {
  int sent = 0;
  while (sent < length) {
    int sent_now = send(sock, data + sent, length - sent, 0);
    if (sent_now == 0) {
      return 0;
    } else if (sent_now > 0) {
      sent += sent_now;
    } else {
      printf("Send retornou menos que 0 = %d. SOCKET fechado.\n", sent_now);
      return 0;
    }
  }
  return sent;
}


x86 assembly (clang, -O0):
<gdp_io_thread_internal_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b6 <gdp_io_thread_internal_send+0x96>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1179 <gdp_io_thread_internal_send+0x59>
movl   $0x0,-0x4(%rbp)
jmp    11bc <gdp_io_thread_internal_send+0x9c>
cmpl   $0x0,-0x1c(%rbp)
jle    1191 <gdp_io_thread_internal_send+0x71>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11ac <gdp_io_thread_internal_send+0x8c>
mov    -0x1c(%rbp),%esi
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11bc <gdp_io_thread_internal_send+0x9c>
jmp    11b1 <gdp_io_thread_internal_send+0x91>
jmp    1139 <gdp_io_thread_internal_send+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdp_io_thread_internal_send>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1171 <gdp_io_thread_internal_send+0x51>
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,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
je     1171 <gdp_io_thread_internal_send+0x51>
jle    1163 <gdp_io_thread_internal_send+0x43>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1140 <gdp_io_thread_internal_send+0x20>
jmp    1173 <gdp_io_thread_internal_send+0x53>
lea    0xe96(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"Send retornou menos que 0 = %d. SOCKET fechado.\0A\00"
define i32 @gdp_io_thread_internal_send(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
br label %10
10: ; preds = %38, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %39
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @send(i32 %15, i8* %19, i32 %22, i32 0)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %14
store i32 0, i32* %4
br label %41
27: ; preds = %14
%28 = load i32, i32* %9
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* %9
%32 = load i32, i32* %8
%33 = add nsw i32 %32, %31
store i32 %33, i32* %8
br label %37
34: ; preds = %27
%35 = load i32, i32* %9
%36 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %35)
store i32 0, i32* %4
br label %41
37: ; preds = %30
br label %38
38: ; preds = %37
br label %10
39: ; preds = %10
%40 = load i32, i32* %8
store i32 %40, i32* %4
br label %41
41: ; preds = %39, %34, %26
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [49 x i8] c"Send retornou menos que 0 = %d. SOCKET fechado.\0A\00"
define i32 @gdp_io_thread_internal_send(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %19
5: ; preds = %3, %16
%6 = phi i32 [ %17, %16 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @send(i32 %0, i8* %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %5
%13 = icmp sgt i32 %10, 0
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %19
16: ; preds = %12
%17 = add nsw i32 %10, %6
%18 = icmp slt i32 %17, %2
br i1 %18, label %5, label %19
19: ; preds = %16, %5, %3, %14
%20 = phi i32 [ 0, %14 ], [ 0, %3 ], [ %17, %16 ], [ 0, %5 ]
ret i32 %20
}