#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 can_send_to_socket(int);
int send_data(int, void *, int);
int /*<<< orphan*/ usleep(int);
int send_all_data(int sock, void *buffer, size_t buffer_size, int use_select) {
  size_t pos = 0;
  int sel, packet_size, data_sent = 0;
  while (pos < buffer_size) {
    if (use_select) {
      sel = can_send_to_socket(sock);
      if (sel < 0) {
        return -1;
      }
      if (sel == 0) {
        usleep(500);
        continue;
      }
    }
    packet_size = buffer_size - pos;
    data_sent =
        send_data(sock, buffer + pos,
                  (packet_size < (1536 - 66)) ? packet_size : (1536 - 66));
    if (data_sent == -1) {
      return -1;
    }
    pos += data_sent;
  }
  return buffer_size;
}


x86 assembly (clang, -O0):
<send_all_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1227 <send_all_data+0xf7>
cmpl   $0x0,-0x1c(%rbp)
je     11ac <send_all_data+0x7c>
mov    -0x8(%rbp),%edi
call   1040 <can_send_to_socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    118e <send_all_data+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <send_all_data+0xfe>
cmpl   $0x0,-0x2c(%rbp)
jne    11a7 <send_all_data+0x77>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
jmp    1155 <send_all_data+0x25>
jmp    11ac <send_all_data+0x7c>
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x5be,-0x30(%rbp)
jge    11e1 <send_all_data+0xb1>
mov    -0x30(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    11ee <send_all_data+0xbe>
mov    $0x5be,%eax
mov    %eax,-0x48(%rbp)
jmp    11ee <send_all_data+0xbe>
mov    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edi
mov    -0x48(%rbp),%edx
call   1030 <send_data@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1216 <send_all_data+0xe6>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <send_all_data+0xfe>
movslq -0x34(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1155 <send_all_data+0x25>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_all_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
test   %rdx,%rdx
je     11ad <send_all_data+0x7d>
mov    %ecx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
xor    %ebx,%ebx
mov    $0xffffffff,%r13d
test   %r15d,%r15d
je     1180 <send_all_data+0x50>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <can_send_to_socket@plt>
test   %eax,%eax
js     11b0 <send_all_data+0x80>
test   %eax,%eax
jne    1180 <send_all_data+0x50>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
jmp    1160 <send_all_data+0x30>
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edx
sub    %ebx,%edx
lea    (%r14,%rbx,1),%rsi
cmp    $0x5be,%edx
mov    $0x5be,%eax
cmovge %eax,%edx
mov    %ebp,%edi
call   1030 <send_data@plt>
cmp    $0xffffffff,%eax
je     11c2 <send_all_data+0x92>
cltq
add    %rax,%rbx
cmp    %r12,%rbx
jb     1153 <send_all_data+0x23>
mov    %r12d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r13d
jmp    11b0 <send_all_data+0x80>

LLVM IR (-O0):
define i32 @send_all_data(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i64 0, i64* %10
store i32 0, i32* %13
br label %14
14: ; preds = %53, %30, %4
%15 = load i64, i64* %10
%16 = load i64, i64* %8
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %58
18: ; preds = %14
%19 = load i32, i32* %9
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = load i32, i32* %6
%23 = call i32 @can_send_to_socket(i32 %22)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -1, i32* %5
br label %61
27: ; preds = %21
%28 = load i32, i32* %11
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @usleep(i32 500)
br label %14
32: ; preds = %27
br label %33
33: ; preds = %32, %18
%34 = load i64, i64* %8
%35 = load i64, i64* %10
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
store i32 %37, i32* %12
%38 = load i32, i32* %6
%39 = load i8*, i8** %7
%40 = load i64, i64* %10
%41 = getelementptr i8, i8* %39, i64 %40
%42 = load i32, i32* %12
%43 = icmp slt i32 %42, 1470
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = load i32, i32* %12
br label %47
46: ; preds = %33
br label %47
47: ; preds = %46, %44
%48 = phi i32 [ %45, %44 ], [ 1470, %46 ]
%49 = call i32 @send_data(i32 %38, i8* %41, i32 %48)
store i32 %49, i32* %13
%50 = load i32, i32* %13
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %47
store i32 -1, i32* %5
br label %61
53: ; preds = %47
%54 = load i32, i32* %13
%55 = sext i32 %54 to i64
%56 = load i64, i64* %10
%57 = add i64 %56, %55
store i64 %57, i64* %10
br label %14
58: ; preds = %14
%59 = load i64, i64* %8
%60 = trunc i64 %59 to i32
store i32 %60, i32* %5
br label %61
61: ; preds = %58, %52, %26
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
define i32 @send_all_data(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %32, label %6
6: ; preds = %4
%7 = icmp eq i32 %3, 0
br label %10
8: ; preds = %22
%9 = icmp ult i64 %31, %2
br i1 %9, label %10, label %32
10: ; preds = %6, %8
%11 = phi i64 [ 0, %6 ], [ %31, %8 ]
br i1 %7, label %22, label %12
12: ; preds = %10
%13 = tail call i32 @can_send_to_socket(i32 %0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %34, label %15
15: ; preds = %12, %18
%16 = phi i32 [ %20, %18 ], [ %13, %12 ]
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = tail call i32 @usleep(i32 500)
%20 = tail call i32 @can_send_to_socket(i32 %0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %34, label %15
22: ; preds = %15, %10
%23 = sub i64 %2, %11
%24 = trunc i64 %23 to i32
%25 = getelementptr i8, i8* %1, i64 %11
%26 = icmp slt i32 %24, 1470
%27 = select i1 %26, i32 %24, i32 1470
%28 = tail call i32 @send_data(i32 %0, i8* %25, i32 %27)
%29 = icmp eq i32 %28, -1
%30 = sext i32 %28 to i64
%31 = add i64 %11, %30
br i1 %29, label %34, label %8
32: ; preds = %8, %4
%33 = trunc i64 %2 to i32
br label %34
34: ; preds = %22, %12, %18, %32
%35 = phi i32 [ %33, %32 ], [ -1, %18 ], [ -1, %12 ], [ -1, %22 ]
ret i32 %35
}