#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ report(int, char *, ...);
int write(int, char *, size_t);
int sock_send(int fd, void *src, size_t size) {
  int offset = 0;
  if (!src)
    return -1;
  while (offset != size) {
    int sent = write(fd, ((char *)src) + offset, size - offset);
    if (sent == -1) {
      if (errno != EAGAIN) {
        report(1, "sock_send: socket write error");
        report(5, "Message was: '%.*s'", size - offset, (char *)src);
        return sent;
      }
      continue;
    } else if (sent == 0) {
      return sent + offset;
    }
    offset += sent;
  }
  return offset;
}


x86 assembly (clang, -O0):
<sock_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1151 <sock_send+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <sock_send+0xff>
jmp    1156 <sock_send+0x36>
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1219 <sock_send+0xf9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
sub    %rax,%rdx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ee <sock_send+0xce>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
je     11e9 <sock_send+0xc9>
mov    $0x1,%edi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1040 <report@plt>
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
sub    %rax,%rdx
mov    -0x10(%rbp),%rcx
mov    $0x5,%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1040 <report@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121f <sock_send+0xff>
jmp    1156 <sock_send+0x36>
cmpl   $0x0,-0x20(%rbp)
jne    1206 <sock_send+0xe6>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121f <sock_send+0xff>
jmp    120b <sock_send+0xeb>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1156 <sock_send+0x36>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rsi,%rsi
je     11cf <sock_send+0xaf>
mov    %rdx,%r12
test   %rdx,%rdx
je     119a <sock_send+0x7a>
mov    %rsi,%r14
mov    %edi,%r15d
xor    %eax,%eax
mov    0x2e80(%rip),%r13
xor    %ebp,%ebp
jmp    1178 <sock_send+0x58>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    119e <sock_send+0x7e>
movslq %ebp,%rax
cmp    %r12,%rax
je     11cf <sock_send+0xaf>
lea    (%r14,%rax,1),%rsi
mov    %r12,%rbx
sub    %rax,%rbx
mov    %r15d,%edi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1160 <sock_send+0x40>
test   %eax,%eax
je     11cf <sock_send+0xaf>
add    %eax,%ebp
jmp    1170 <sock_send+0x50>
xor    %ebp,%ebp
jmp    11cf <sock_send+0xaf>
lea    0xe5b(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <report@plt>
lea    0xe66(%rip),%rsi
mov    $0x5,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <report@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [30 x i8] c"sock_send: socket write error\00"
@.str.1 = [20 x i8] c"Message was: '%.*s'\00"
define i32 @sock_send(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
store i32 -1, i32* %4
br label %60
13: ; preds = %3
br label %14
14: ; preds = %54, %45, %13
%15 = load i32, i32* %8
%16 = sext i32 %15 to i64
%17 = load i64, i64* %7
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %58
19: ; preds = %14
%20 = load i32, i32* %5
%21 = load i8*, i8** %6
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i64, i64* %7
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = sub i64 %25, %27
%29 = call i32 @write(i32 %20, i8* %24, i64 %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %46
32: ; preds = %19
%33 = load i64, i64* @errno
%34 = load i64, i64* @EAGAIN
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %45
36: ; preds = %32
%37 = call i32 (i32, i8*, ...) @report(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%38 = load i64, i64* %7
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = sub i64 %38, %40
%42 = load i8*, i8** %6
%43 = call i32 (i32, i8*, ...) @report(i32 5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 %41, i8* %42)
%44 = load i32, i32* %9
store i32 %44, i32* %4
br label %60
45: ; preds = %32
br label %14
46: ; preds = %19
%47 = load i32, i32* %9
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32, i32* %9
%51 = load i32, i32* %8
%52 = add nsw i32 %50, %51
store i32 %52, i32* %4
br label %60
53: ; preds = %46
br label %54
54: ; preds = %53
%55 = load i32, i32* %9
%56 = load i32, i32* %8
%57 = add nsw i32 %56, %55
store i32 %57, i32* %8
br label %14
58: ; preds = %14
%59 = load i32, i32* %8
store i32 %59, i32* %4
br label %60
60: ; preds = %58, %49, %36, %12
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [30 x i8] c"sock_send: socket write error\00"
@.str.1 = [20 x i8] c"Message was: '%.*s'\00"
define i32 @sock_send(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %26, label %5
5: ; preds = %3
%6 = icmp eq i64 %2, 0
br i1 %6, label %26, label %7
7: ; preds = %5, %22
%8 = phi i64 [ %24, %22 ], [ 0, %5 ]
%9 = phi i32 [ %23, %22 ], [ 0, %5 ]
%10 = getelementptr inbounds i8, i8* %1, i64 %8
%11 = sub i64 %2, %8
%12 = tail call i32 @write(i32 %0, i8* %10, i64 %11)
switch i32 %12, label %20 [
i32 -1, label %13
i32 0, label %26
]
13: ; preds = %7
%14 = load i64, i64* @errno
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %14, %15
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = tail call i32 (i32, i8*, ...) @report(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 (i32, i8*, ...) @report(i32 5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 %11, i8* %1)
br label %26
20: ; preds = %7
%21 = add nsw i32 %12, %9
br label %22
22: ; preds = %13, %20
%23 = phi i32 [ %21, %20 ], [ %9, %13 ]
%24 = sext i32 %23 to i64
%25 = icmp eq i64 %24, %2
br i1 %25, label %26, label %7
26: ; preds = %22, %7, %5, %17, %3
%27 = phi i32 [ -1, %3 ], [ -1, %17 ], [ 0, %5 ], [ %23, %22 ], [ %9, %7 ]
ret i32 %27
}