#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 write(int, double *, int);
int write_double_array_to_socket(int sockfd, double *elements, int size) {
  int total = 0;
  int bytesremaining = size;
  int n;
  n = write(sockfd, elements, size);
  if (n != size) {
    if (n > 0) {
      total += n;
      bytesremaining -= n;
    }
    while (bytesremaining > 0) {
      n = write(sockfd, elements + total, bytesremaining);
      if (n == -1) {
        break;
      }
      total += n;
      bytesremaining -= n;
    }
  }
  return n;
}


x86 assembly (clang, -O0):
<write_double_array_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11c4 <write_double_array_to_socket+0xb4>
cmpl   $0x0,-0x20(%rbp)
jle    116b <write_double_array_to_socket+0x5b>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1170 <write_double_array_to_socket+0x60>
cmpl   $0x0,-0x1c(%rbp)
jle    11bf <write_double_array_to_socket+0xaf>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11a6 <write_double_array_to_socket+0x96>
jmp    11bf <write_double_array_to_socket+0xaf>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1170 <write_double_array_to_socket+0x60>
jmp    11c4 <write_double_array_to_socket+0xb4>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_double_array_to_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
call   1030 <write@plt>
cmp    %ebx,%eax
je     1165 <write_double_array_to_socket+0x55>
xor    %ebp,%ebp
test   %eax,%eax
cmovns %eax,%ebp
sub    %ebp,%ebx
jle    115c <write_double_array_to_socket+0x4c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %ebp,%rax
lea    (%r14,%rax,8),%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1160 <write_double_array_to_socket+0x50>
add    %eax,%ebp
sub    %eax,%ebx
jg     1140 <write_double_array_to_socket+0x30>
mov    %eax,%ebx
jmp    1165 <write_double_array_to_socket+0x55>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_double_array_to_socket(i32 %0, double* %1, i32 %2) {
%4 = alloca i32
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store double* %1, double** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%10 = load i32, i32* %6
store i32 %10, i32* %8
%11 = load i32, i32* %4
%12 = load double*, double** %5
%13 = load i32, i32* %6
%14 = call i32 @write(i32 %11, double* %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = load i32, i32* %6
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %51
18: ; preds = %3
%19 = load i32, i32* %9
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i32, i32* %9
%23 = load i32, i32* %7
%24 = add nsw i32 %23, %22
store i32 %24, i32* %7
%25 = load i32, i32* %9
%26 = load i32, i32* %8
%27 = sub nsw i32 %26, %25
store i32 %27, i32* %8
br label %28
28: ; preds = %21, %18
br label %29
29: ; preds = %43, %28
%30 = load i32, i32* %8
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %50
32: ; preds = %29
%33 = load i32, i32* %4
%34 = load double*, double** %5
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds double, double* %34, i64 %36
%38 = load i32, i32* %8
%39 = call i32 @write(i32 %33, double* %37, i32 %38)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %32
br label %50
43: ; preds = %32
%44 = load i32, i32* %9
%45 = load i32, i32* %7
%46 = add nsw i32 %45, %44
store i32 %46, i32* %7
%47 = load i32, i32* %9
%48 = load i32, i32* %8
%49 = sub nsw i32 %48, %47
store i32 %49, i32* %8
br label %29
50: ; preds = %42, %29
br label %51
51: ; preds = %50, %3
%52 = load i32, i32* %9
ret i32 %52
}

LLVM IR (-O2):
define i32 @write_double_array_to_socket(i32 %0, double* nocapture %1, i32 %2) {
%4 = tail call i32 @write(i32 %0, double* %1, i32 %2)
%5 = icmp eq i32 %4, %2
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = icmp sgt i32 %4, 0
%8 = select i1 %7, i32 %4, i32 0
%9 = sub nsw i32 %2, %8
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %6, %18
%12 = phi i32 [ %20, %18 ], [ %9, %6 ]
%13 = phi i32 [ %19, %18 ], [ %8, %6 ]
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds double, double* %1, i64 %14
%16 = tail call i32 @write(i32 %0, double* %15, i32 %12)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = add nsw i32 %16, %13
%20 = sub nsw i32 %12, %16
%21 = icmp sgt i32 %20, 0
br i1 %21, label %11, label %22
22: ; preds = %11, %18, %6, %3
%23 = phi i32 [ %2, %3 ], [ %4, %6 ], [ -1, %11 ], [ %16, %18 ]
ret i32 %23
}