#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 write(int, char const *, int);
int write_socket(int fd, const char *buf, int len) {
  int done, todo = len;
  while (todo) {
    do {
      done = write(fd, buf, todo);
    } while (done < 0 && errno == EAGAIN);
    if (done <= 0)
      return (done);
    buf += done;
    todo -= done;
  }
  return (len);
}


x86 assembly (clang, -O0):
<write_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11b3 <write_socket+0xa3>
jmp    1137 <write_socket+0x27>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x1d(%rbp)
jge    1172 <write_socket+0x62>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1137 <write_socket+0x27>
cmpl   $0x0,-0x18(%rbp)
jg     1192 <write_socket+0x82>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <write_socket+0xa9>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1128 <write_socket+0x18>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     116a <write_socket+0x5a>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb1(%rip),%r12
mov    0x2e9a(%rip),%r13
mov    %edx,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    115b <write_socket+0x4b>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1140 <write_socket+0x30>
test   %eax,%eax
jle    116f <write_socket+0x5f>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1140 <write_socket+0x30>
jmp    1172 <write_socket+0x62>
xor    %r14d,%r14d
jmp    1172 <write_socket+0x62>
mov    %eax,%r14d
mov    %r14d,%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
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
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
store i32 %10, i32* %9
br label %11
11: ; preds = %34, %3
%12 = load i32, i32* %9
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %42
14: ; preds = %11
br label %15
15: ; preds = %27, %14
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %9
%19 = call i32 @write(i32 %16, i8* %17, i32 %18)
store i32 %19, i32* %8
br label %20
20: ; preds = %15
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %24, %25
br label %27
27: ; preds = %23, %20
%28 = phi i1 [ false, %20 ], [ %26, %23 ]
br i1 %28, label %15, label %29
29: ; preds = %27
%30 = load i32, i32* %8
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %44
34: ; preds = %29
%35 = load i32, i32* %8
%36 = load i8*, i8** %6
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %6
%39 = load i32, i32* %8
%40 = load i32, i32* %9
%41 = sub nsw i32 %40, %39
store i32 %41, i32* %9
br label %11
42: ; preds = %11
%43 = load i32, i32* %7
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %32
%45 = load i32, i32* %4
ret i32 %45
}

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