#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 */
typedef size_t ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ usleep(int);
size_t write(int, unsigned char *, size_t);
int bsdsock_write(size_t count, void const *buf, void *data) {
  int fd = *((int *)data);
  ssize_t wb = 0;
  ssize_t w = 0;
  do {
    w = write(fd, (unsigned char *)buf + wb, count - wb);
    if (0 < w) {
      wb += w;
      continue;
    }
    if (!w) {
      break;
    }
    if (EAGAIN == errno || EWOULDBLOCK == errno) {
      usleep(100);
      continue;
    }
    return -3 + errno;
  } while (wb < count);
  return wb;
}


x86 assembly (clang, -O0):
<bsdsock_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jae    1186 <bsdsock_write+0x66>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11ef <bsdsock_write+0xcf>
cmpq   $0x0,-0x38(%rbp)
jne    1196 <bsdsock_write+0x76>
jmp    11fd <bsdsock_write+0xdd>
mov    0x2e2b(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <bsdsock_write+0xaa>
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <bsdsock_write+0xb9>
mov    $0x64,%edi
call   1040 <usleep@plt>
jmp    11ef <bsdsock_write+0xcf>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
add    $0xfffffffffffffffd,%rax
mov    %eax,-0x4(%rbp)
jmp    1204 <bsdsock_write+0xe4>
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     114d <bsdsock_write+0x2d>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsdsock_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdx),%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%rsi
mov    %r15,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
test   %rax,%rax
je     114e <bsdsock_write+0x3e>
add    %rax,%rbx
cmp    %r15,%rbx
jb     1130 <bsdsock_write+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@EAGAIN = global i64 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @bsdsock_write(i64 %0, i8* nocapture %1, i8* nocapture %2) {
%4 = bitcast i8* %2 to i32*
%5 = load i32, i32* %4
br label %6
6: ; preds = %12, %3
%7 = phi i64 [ 0, %3 ], [ %13, %12 ]
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub i64 %0, %7
%10 = tail call i64 @write(i32 %5, i8* %8, i64 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %6
%13 = add i64 %10, %7
%14 = icmp ult i64 %13, %0
br i1 %14, label %6, label %15
15: ; preds = %12, %6
%16 = phi i64 [ %13, %12 ], [ %7, %6 ]
%17 = trunc i64 %16 to i32
ret i32 %17
}