#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 strlen(char const *);
int write(int, char const *, int);
int send_socket(int sockfd, const char *out_buf, int size) {
  int count = 0;
  int ret = 0;
  int len = 0;
  count = 0;
  len = (-1 == size) ? strlen(out_buf) : size;
  while (count < len) {
    ret = write(sockfd, out_buf + count, len - count);
    if (ret <= 0) {
      perror("write");
      return -1;
    }
    count += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_socket>:
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)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    117d <send_socket+0x4d>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
jmp    1183 <send_socket+0x53>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11e1 <send_socket+0xb1>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11d3 <send_socket+0xa3>
lea    0xe3e(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <send_socket+0xb8>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1189 <send_socket+0x59>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r14d
cmp    $0xffffffff,%edx
je     114a <send_socket+0x1a>
test   %ebp,%ebp
jg     1158 <send_socket+0x28>
jmp    117d <send_socket+0x4d>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
test   %ebp,%ebp
jle    117d <send_socket+0x4d>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r15,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r14d,%edi
call   1030 <write@plt>
test   %eax,%eax
jle    118a <send_socket+0x5a>
add    %eax,%ebx
cmp    %ebx,%ebp
jg     1160 <send_socket+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    117f <send_socket+0x4f>

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

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