#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 *, int, int /*<<< orphan*/);
int strlen(char *);
int send_msg(int sockfd, char *buffer) {
  int sent, to_send;
  to_send = strlen(buffer);
  while (to_send > 0) {
    sent = send(sockfd, buffer, to_send, 0);
    if (sent == -1)
      return -1;
    to_send -= sent;
    buffer += sent;
  }
  return sent;
}


x86 assembly (clang, -O0):
<send_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1190 <send_msg+0x70>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    116f <send_msg+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <send_msg+0x76>
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    113b <send_msg+0x1b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_msg>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1160 <send_msg+0x40>
mov    %eax,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
je     1162 <send_msg+0x42>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1140 <send_msg+0x20>
jmp    1167 <send_msg+0x47>
jmp    1167 <send_msg+0x47>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @send_msg(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
br label %10
10: ; preds = %21, %2
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i32, i32* %7
%17 = call i32 @send(i32 %14, i8* %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 -1, i32* %3
br label %31
21: ; preds = %13
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, %22
store i32 %24, i32* %7
%25 = load i32, i32* %6
%26 = load i8*, i8** %5
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %5
br label %10
29: ; preds = %10
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %20
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
define i32 @send_msg(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %15
5: ; preds = %2, %10
%6 = phi i32 [ %11, %10 ], [ %3, %2 ]
%7 = phi i8* [ %13, %10 ], [ %1, %2 ]
%8 = tail call i32 @send(i32 %0, i8* %7, i32 %6, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %15, label %10
10: ; preds = %5
%11 = sub nsw i32 %6, %8
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %7, i64 %12
%14 = icmp sgt i32 %11, 0
br i1 %14, label %5, label %15
15: ; preds = %5, %10, %2
%16 = phi i32 [ undef, %2 ], [ %8, %10 ], [ -1, %5 ]
ret i32 %16
}