#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*/ perror(char *);
int send(int, char *, int, int /*<<< orphan*/);
int send_fix_len(int sockfd, char *buf, int len) {
  int ret = 0;
  int send_len = 0;
  while (send_len < len) {
    ret = send(sockfd, buf + send_len, len - send_len, 0);
    if (-1 == ret) {
      perror("Fail to send");
      goto exit;
    }
    send_len += ret;
  }
  ret = send_len;
exit:
  return ret;
}


x86 assembly (clang, -O0):
<send_fix_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1197 <send_fix_len+0x77>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    1189 <send_fix_len+0x69>
lea    0xe81(%rip),%rdi
call   1040 <perror@plt>
jmp    119d <send_fix_len+0x7d>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1140 <send_fix_len+0x20>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fix_len>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1162 <send_fix_len+0x42>
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>
cmp    $0xffffffff,%eax
je     1171 <send_fix_len+0x51>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1140 <send_fix_len+0x20>
jmp    1164 <send_fix_len+0x44>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe88(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1164 <send_fix_len+0x44>

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

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