#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, char *, int);
int write_socket(int fd, char *x_msg, int size) {
  int bytes_sent = 0;
  int retries = 0;
  int total_sent = 0;
  while (retries < 1 && size > 0) {
    bytes_sent = write(fd, x_msg, size);
    if (bytes_sent > 0) {
      x_msg += bytes_sent;
      size -= bytes_sent;
      total_sent += bytes_sent;
    } else {
      retries++;
    }
  }
  if (bytes_sent >= 0) {
    return total_sent;
  }
  return -1;
}


x86 assembly (clang, -O0):
<write_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)
xor    %eax,%eax
cmpl   $0x1,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
jge    1150 <write_socket+0x40>
cmpl   $0x0,-0x14(%rbp)
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1160 <write_socket+0x50>
jmp    11b4 <write_socket+0xa4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11a6 <write_socket+0x96>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11af <write_socket+0x9f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1137 <write_socket+0x27>
cmpl   $0x0,-0x18(%rbp)
jl     11c9 <write_socket+0xb9>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d0 <write_socket+0xc0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    115a <write_socket+0x4a>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    114d <write_socket+0x3d>
mov    %eax,%ecx
add    %rcx,%rbx
add    %eax,%r15d
sub    %eax,%ebp
jg     1130 <write_socket+0x20>
test   %eax,%eax
mov    $0xffffffff,%eax
cmovns %r15d,%eax
jmp    115c <write_socket+0x4c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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