#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ int32_t;
typedef int int16_t;

/* Variables and functions */
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ perror(char *);
int send(int /*<<< orphan*/, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int16_t send_msg(int32_t sockfd, uint8_t *msg, uint16_t len) {
  int bytes_sent = 0, failed = 0, bytes, bytes_left = len;
  do {
    if ((bytes = send(sockfd, msg + bytes_sent, bytes_left, MSG_NOSIGNAL)) ==
        -1) {
      ++failed;
    } else {
      failed = 0;
      bytes_sent += bytes;
      bytes_left -= bytes;
    }
    if (failed >= 60) {
      perror("send");
      return -1;
    }
  } while (bytes_sent < len);
  return 0;
}


x86 assembly (clang, -O0):
<send_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
mov    0x2e7e(%rip),%rax
mov    (%rax),%ecx
call   1030 <send@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1183 <send_msg+0x63>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119e <send_msg+0x7e>
movl   $0x0,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x3c,-0x1c(%rbp)
jl     11c0 <send_msg+0xa0>
lea    0xe51(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <send_msg+0xb8>
jmp    11c5 <send_msg+0xa5>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1146 <send_msg+0x26>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,(%rsp)
mov    %edi,%ebp
xor    %r12d,%r12d
xor    %r13d,%r13d
xor    %r14d,%r14d
mov    %edx,%ebx
nop
movslq %r13d,%rax
mov    (%rsp),%rcx
lea    (%rcx,%rax,4),%rsi
mov    0x2e8e(%rip),%rax
mov    (%rax),%ecx
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <send@plt>
add    $0x1,%r14d
cmp    $0xffffffff,%eax
cmove  %r12d,%eax
cmovne %r12d,%r14d
cmp    $0x3c,%r14d
jge    118d <send_msg+0x6d>
add    %eax,%r13d
sub    %eax,%ebx
cmp    %r15d,%r13d
jl     1140 <send_msg+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe6c(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    117e <send_msg+0x5e>

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
@.str = [5 x i8] c"send\00"
define i32 @send_msg(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%12 = load i32, i32* %7
store i32 %12, i32* %11
br label %13
13: ; preds = %39, %3
%14 = load i32, i32* %5
%15 = load i32*, i32** %6
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %15, i64 %17
%19 = load i32, i32* %11
%20 = load i32, i32* @MSG_NOSIGNAL
%21 = call i32 @send(i32 %14, i32* %18, i32 %19, i32 %20)
store i32 %21, i32* %10
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
store i32 %25, i32* %9
br label %33
26: ; preds = %13
store i32 0, i32* %9
%27 = load i32, i32* %10
%28 = load i32, i32* %8
%29 = add nsw i32 %28, %27
store i32 %29, i32* %8
%30 = load i32, i32* %10
%31 = load i32, i32* %11
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %11
br label %33
33: ; preds = %26, %23
%34 = load i32, i32* %9
%35 = icmp sge i32 %34, 60
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
38: ; preds = %33
br label %39
39: ; preds = %38
%40 = load i32, i32* %8
%41 = load i32, i32* %7
%42 = icmp slt i32 %40, %41
br i1 %42, label %13, label %43
43: ; preds = %39
store i32 0, i32* %4
br label %44
44: ; preds = %43, %36
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@MSG_NOSIGNAL = global i32 0
@.str = [5 x i8] c"send\00"
define i32 @send_msg(i32 %0, i32* %1, i32 %2) {
br label %4
4: ; preds = %20, %3
%5 = phi i32 [ 0, %3 ], [ %15, %20 ]
%6 = phi i32 [ 0, %3 ], [ %16, %20 ]
%7 = phi i32 [ %2, %3 ], [ %21, %20 ]
%8 = sext i32 %5 to i64
%9 = getelementptr inbounds i32, i32* %1, i64 %8
%10 = load i32, i32* @MSG_NOSIGNAL
%11 = tail call i32 @send(i32 %0, i32* %9, i32 %7, i32 %10)
%12 = icmp eq i32 %11, -1
%13 = add nsw i32 %6, 1
%14 = select i1 %12, i32 0, i32 %11
%15 = add nsw i32 %14, %5
%16 = select i1 %12, i32 %13, i32 0
%17 = icmp sgt i32 %16, 59
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %23
20: ; preds = %4
%21 = sub nsw i32 %7, %14
%22 = icmp slt i32 %15, %2
br i1 %22, label %4, label %23
23: ; preds = %20, %18
%24 = phi i32 [ -1, %18 ], [ 0, %20 ]
ret i32 %24
}