#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 send(int, char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shout(char *, char const *);
int /*<<< orphan*/ socket_error(char *);
int send_cmd(int sock, const char *buffer, int buf_len) {
  int ret;
  int sended = 0;
  shout("[SEND CMD:] \n%s\n", buffer);
  while (buf_len > 0) {
    ret = send(sock, buffer + sended, buf_len, 0);
    if (ret > 0) {
      sended += ret;
      buf_len -= ret;
    } else {
      socket_error("send:");
      return -1;
    }
  }
  return sended;
}


x86 assembly (clang, -O0):
<send_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xeac(%rip),%rdi
call   1050 <shout@plt>
cmpl   $0x0,-0x14(%rbp)
jle    11be <send_cmd+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11a1 <send_cmd+0x71>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11b9 <send_cmd+0x89>
lea    0xe69(%rip),%rdi
call   1030 <socket_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c4 <send_cmd+0x94>
jmp    1159 <send_cmd+0x29>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_cmd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xeba(%rip),%rdi
call   1050 <shout@plt>
xor    %ebp,%ebp
test   %ebx,%ebx
jle    118f <send_cmd+0x5f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
test   %eax,%eax
jle    117e <send_cmd+0x4e>
add    %eax,%ebp
sub    %eax,%ebx
jg     1160 <send_cmd+0x30>
jmp    118f <send_cmd+0x5f>
lea    0xe8c(%rip),%rdi
call   1030 <socket_error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"[SEND CMD:] \0A%s\0A\00"
@.str.1 = [6 x i8] c"send:\00"
define i32 @send_cmd(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* %9
%10 = load i8*, i8** %6
%11 = call i32 @shout(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %34, %3
%13 = load i32, i32* %7
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %35
15: ; preds = %12
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %7
%22 = call i32 @send(i32 %16, i8* %20, i32 %21, i32 0)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %15
%26 = load i32, i32* %8
%27 = load i32, i32* %9
%28 = add nsw i32 %27, %26
store i32 %28, i32* %9
%29 = load i32, i32* %8
%30 = load i32, i32* %7
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %7
br label %34
32: ; preds = %15
%33 = call i32 @socket_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %37
34: ; preds = %25
br label %12
35: ; preds = %12
%36 = load i32, i32* %9
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %32
%38 = load i32, i32* %4
ret i32 %38
}

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