#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int write(int, char *, int);
int write_socket(int fd, char *buf, int len) {
  int wb = 0;
  int ofx = 0;
  do {
    len -= wb;
    ofx += wb;
    wb = write(fd, buf + ofx, len);
    if (wb == -1) {
      if (errno != EINTR && errno != EAGAIN) {
        fprintf(stderr, "write on fd %d failed: %s", fd, strerror(errno));
        return -1;
      }
      wb = 0;
    } else if (wb == 0) {
      break;
    }
  } while (wb != len);
  return wb;
}


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)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1209 <write_socket+0xd9>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
je     11fd <write_socket+0xcd>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
je     11fd <write_socket+0xcd>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe14(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <write_socket+0x104>
movl   $0x0,-0x18(%rbp)
jmp    121d <write_socket+0xed>
cmpl   $0x0,-0x18(%rbp)
jne    1218 <write_socket+0xe8>
jmp    122e <write_socket+0xfe>
jmp    121d <write_socket+0xed>
jmp    1222 <write_socket+0xf2>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1150 <write_socket+0x20>
mov    -0x18(%rbp),%eax
mov    %eax,-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   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %eax,%eax
mov    0x2e8c(%rip),%r15
mov    0x2e7d(%rip),%r12
xor    %r13d,%r13d
jmp    116c <write_socket+0x3c>
nopl   0x0(%rax,%rax,1)
mov    %eax,%ecx
test   %eax,%eax
je     11c4 <write_socket+0x94>
mov    %ebp,%ecx
cmp    %ebp,%eax
je     11c4 <write_socket+0x94>
sub    %eax,%ebp
add    %eax,%r13d
movslq %r13d,%rsi
add    %rbx,%rsi
mov    %r14d,%edi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1160 <write_socket+0x30>
mov    (%r15),%rdi
xor    %eax,%eax
cmp    (%r12),%rdi
je     1166 <write_socket+0x36>
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rdi
je     1166 <write_socket+0x36>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe4e(%rip),%rsi
mov    %ebx,%edi
mov    %r14d,%edx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [26 x i8] c"write on fd %d failed: %s\00"
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
store i32 0, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %47, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, %11
store i32 %13, i32* %7
%14 = load i32, i32* %8
%15 = load i32, i32* %9
%16 = add nsw i32 %15, %14
store i32 %16, i32* %9
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %7
%23 = call i32 @write(i32 %17, i8* %21, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %41
26: ; preds = %10
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %40
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i32, i32* %5
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %36, i8* %38)
store i32 -1, i32* %4
br label %53
40: ; preds = %30, %26
store i32 0, i32* %8
br label %46
41: ; preds = %10
%42 = load i32, i32* %8
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
br label %51
45: ; preds = %41
br label %46
46: ; preds = %45, %40
br label %47
47: ; preds = %46
%48 = load i32, i32* %8
%49 = load i32, i32* %7
%50 = icmp ne i32 %48, %49
br i1 %50, label %10, label %51
51: ; preds = %47, %44
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %53
53: ; preds = %51, %34
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [26 x i8] c"write on fd %d failed: %s\00"
define i32 @write_socket(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %24, %3
%5 = phi i32 [ %2, %3 ], [ %8, %24 ]
%6 = phi i32 [ 0, %3 ], [ %25, %24 ]
%7 = phi i32 [ 0, %3 ], [ %9, %24 ]
%8 = sub nsw i32 %5, %6
%9 = add nsw i32 %7, %6
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = tail call i32 @write(i32 %0, i8* %11, i32 %8)
switch i32 %12, label %24 [
i32 -1, label %13
i32 0, label %27
]
13: ; preds = %4
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = load i64, i64* @EAGAIN
%18 = icmp eq i64 %14, %17
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %24, label %20
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = tail call i8* @strerror(i64 %14)
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %22)
br label %27
24: ; preds = %4, %13
%25 = phi i32 [ 0, %13 ], [ %12, %4 ]
%26 = icmp eq i32 %25, %8
br i1 %26, label %27, label %4
27: ; preds = %24, %4, %20
%28 = phi i32 [ -1, %20 ], [ %12, %4 ], [ %8, %24 ]
ret i32 %28
}