#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;
  if (!out_buf) {
    return -1;
  }
  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)
cmpq   $0x0,-0x10(%rbp)
jne    116e <send_socket+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <send_socket+0xcf>
movl   $0x0,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1194 <send_socket+0x64>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
jmp    119a <send_socket+0x6a>
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    11f8 <send_socket+0xc8>
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     11ea <send_socket+0xba>
lea    0xe27(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <send_socket+0xcf>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a0 <send_socket+0x70>
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   %r12
push   %rbx
mov    $0xffffffff,%r14d
test   %rsi,%rsi
je     1190 <send_socket+0x60>
mov    %edx,%ebp
mov    %rsi,%r12
mov    %edi,%r15d
cmp    $0xffffffff,%edx
je     1156 <send_socket+0x26>
test   %ebp,%ebp
jg     1164 <send_socket+0x34>
jmp    118d <send_socket+0x5d>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
test   %ebp,%ebp
jle    118d <send_socket+0x5d>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r12,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
jle    119c <send_socket+0x6c>
add    %eax,%ebx
cmp    %ebx,%ebp
jg     1170 <send_socket+0x40>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
jmp    1190 <send_socket+0x60>

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
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 -1, i32* %4
br label %47
14: ; preds = %3
store i32 0, i32* %8
%15 = load i32, i32* %7
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
br label %22
20: ; preds = %14
%21 = load i32, i32* %7
br label %22
22: ; preds = %20, %17
%23 = phi i32 [ %19, %17 ], [ %21, %20 ]
store i32 %23, i32* %10
br label %24
24: ; preds = %42, %22
%25 = load i32, i32* %8
%26 = load i32, i32* %10
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %46
28: ; preds = %24
%29 = load i32, i32* %5
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %10
%35 = load i32, i32* %8
%36 = sub nsw i32 %34, %35
%37 = call i32 @write(i32 %29, i8* %33, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp sle i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %28
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
42: ; preds = %28
%43 = load i32, i32* %9
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %24
46: ; preds = %24
store i32 0, i32* %4
br label %47
47: ; preds = %46, %40, %13
%48 = load i32, i32* %4
ret i32 %48
}

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