#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int send(int, char *, int, int /*<<< orphan*/);
int netsend(int sock, char *buf, size_t len) {
  char *ptr;
  int ntowrite, nwrote;
  ptr = buf;
  ntowrite = len;
  while (ntowrite > 0) {
    nwrote = send(sock, ptr, ntowrite, 0);
    if (nwrote == -1) {
      if (errno == EINTR)
        continue;
      perror("Error writing to network");
      return (1);
    } else if (nwrote == 0) {
      printf("Warning: Did not write any bytes!\n");
      return (1);
    } else {
      ntowrite -= nwrote;
      ptr += nwrote;
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<netsend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11f9 <netsend+0xc9>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11b1 <netsend+0x81>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <netsend+0x69>
jmp    1152 <netsend+0x22>
lea    0xe60(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1200 <netsend+0xd0>
cmpl   $0x0,-0x28(%rbp)
jne    11d3 <netsend+0xa3>
lea    0xe57(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1200 <netsend+0xd0>
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    11f4 <netsend+0xc4>
jmp    1152 <netsend+0x22>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netsend>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
test   %ebx,%ebx
jle    118a <netsend+0x5a>
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e95(%rip),%r15
mov    0x2e86(%rip),%r12
jmp    1169 <netsend+0x39>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11a8 <netsend+0x78>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
je     1160 <netsend+0x30>
test   %eax,%eax
je     118e <netsend+0x5e>
movslq %eax,%rcx
add    %rcx,%r14
sub    %eax,%ebx
jg     1169 <netsend+0x39>
xor    %eax,%eax
jmp    119f <netsend+0x6f>
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe51(%rip),%rdi
call   1050 <perror@plt>
jmp    119a <netsend+0x6a>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [25 x i8] c"Error writing to network\00"
@.str.1 = [35 x i8] c"Warning: Did not write any bytes!\0A\00"
define i32 @netsend(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
%12 = load i64, i64* %7
%13 = trunc i64 %12 to i32
store i32 %13, i32* %9
br label %14
14: ; preds = %45, %28, %3
%15 = load i32, i32* %9
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %46
17: ; preds = %14
%18 = load i32, i32* %5
%19 = load i8*, i8** %8
%20 = load i32, i32* %9
%21 = call i32 @send(i32 %18, i8* %19, i32 %20, i32 0)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
br label %14
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %47
31: ; preds = %17
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %47
36: ; preds = %31
%37 = load i32, i32* %10
%38 = load i32, i32* %9
%39 = sub nsw i32 %38, %37
store i32 %39, i32* %9
%40 = load i32, i32* %10
%41 = load i8*, i8** %8
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %8
br label %44
44: ; preds = %36
br label %45
45: ; preds = %44
br label %14
46: ; preds = %14
store i32 0, i32* %4
br label %47
47: ; preds = %46, %34, %29
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [25 x i8] c"Error writing to network\00"
@str = [34 x i8] c"Warning: Did not write any bytes!\00"
define i32 @netsend(i32 %0, i8* %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %24
6: ; preds = %3, %19
%7 = phi i32 [ %20, %19 ], [ %4, %3 ]
%8 = phi i8* [ %22, %19 ], [ %1, %3 ]
br label %9
9: ; preds = %6, %11
%10 = tail call i32 @send(i32 %0, i8* %8, i32 %7, i32 0)
switch i32 %10, label %19 [
i32 -1, label %11
i32 0, label %17
]
11: ; preds = %9
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %9, label %15
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %24
17: ; preds = %9
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %24
19: ; preds = %9
%20 = sub nsw i32 %7, %10
%21 = sext i32 %10 to i64
%22 = getelementptr inbounds i8, i8* %8, i64 %21
%23 = icmp sgt i32 %20, 0
br i1 %23, label %6, label %24
24: ; preds = %19, %3, %17, %15
%25 = phi i32 [ 1, %15 ], [ 1, %17 ], [ 0, %3 ], [ 0, %19 ]
ret i32 %25
}