#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 ssize_t;
typedef int /*<<< orphan*/ nsize;

/* Variables and functions */
int htonl(size_t);
int /*<<< orphan*/ perror(char *);
int send(int, ...);
int strlen(char *);
int sendstr(int sockfd, char *str) {
  ssize_t numbytes = 0;
  int total = 0, nsize;
  size_t size = strlen(str) + 1;
  nsize = htonl(size);
  send(sockfd, &nsize, sizeof(nsize), 0);
  while (total < size) {
    if ((numbytes = send(sockfd, str + total, size - total, 0)) == -1) {
      perror("sendstr");
      return 0;
    }
    total += numbytes;
  }
  return total;
}


x86 assembly (clang, -O0):
<sendstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
movslq -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11f2 <sendstr+0xb2>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
movslq -0x18(%rbp),%rax
sub    %rax,%rdx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e4 <sendstr+0xa4>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <sendstr+0xb8>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1190 <sendstr+0x50>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
lea    0x1(%r12),%r13d
movslq %r13d,%rbx
mov    %rbx,%rdi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
xor    %ebp,%ebp
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
inc    %r12d
je     11c5 <sendstr+0x85>
xor    %eax,%eax
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r14,%rax,1),%rsi
mov    %rbx,%rdx
sub    %rax,%rdx
mov    %r15d,%edi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
cmp    $0xffffffff,%eax
je     11d6 <sendstr+0x96>
add    %eax,%ebp
movslq %ebp,%rax
cmp    %eax,%r13d
ja     11a0 <sendstr+0x60>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
xor    %ebp,%ebp
jmp    11c5 <sendstr+0x85>

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

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