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


x86 assembly (clang, -O0):
<write_int_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x4,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     11b3 <write_int_to_socket+0xa3>
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11ae <write_int_to_socket+0x9e>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1195 <write_int_to_socket+0x85>
jmp    11ae <write_int_to_socket+0x9e>
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    115f <write_int_to_socket+0x4f>
jmp    11b3 <write_int_to_socket+0xa3>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_int_to_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0x4,%r12d
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     116c <write_int_to_socket+0x5c>
mov    %eax,%ebx
jge    1161 <write_int_to_socket+0x51>
mov    $0x4,%ebp
sub    %ebx,%ebp
xchg   %ax,%ax
movslq %ebx,%rax
lea    (%r14,%rax,4),%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1166 <write_int_to_socket+0x56>
mov    %eax,%r12d
add    %eax,%ebx
sub    %eax,%ebp
jg     1140 <write_int_to_socket+0x30>
jmp    116c <write_int_to_socket+0x5c>
mov    %ebx,%r12d
jmp    116c <write_int_to_socket+0x5c>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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