#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
size_t write(int, void const *, unsigned short);
size_t SockWrite(int fd, const void *buf, unsigned short len) {
  size_t nleft = len;
  size_t n;
  while (nleft > 0) {
    if ((n = write(fd, buf, len)) <= 0) {
      if (n < 0 && errno == EINTR)
        n = 0;
      else {
        fprintf(stderr, "write error: %s\n", (char *)buf);
        return (-1);
      }
    }
    nleft -= n;
    buf += n;
  }
  return (n);
}


x86 assembly (clang, -O0):
<SockWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dx,%ax
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %ax,-0x1a(%rbp)
movzwl -0x1a(%rbp),%eax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11e4 <SockWrite+0xc4>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movzwl -0x1a(%rbp),%edx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
ja     11c4 <SockWrite+0xa4>
cmpq   $0x0,-0x30(%rbp)
jae    1199 <SockWrite+0x79>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <SockWrite+0x79>
movq   $0x0,-0x30(%rbp)
jmp    11bf <SockWrite+0x9f>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe53(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ec <SockWrite+0xcc>
jmp    11c4 <SockWrite+0xa4>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    113e <SockWrite+0x1e>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SockWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     115d <SockWrite+0x3d>
mov    %rsi,%rbx
mov    %edi,%r14d
movzwl %dx,%r15d
mov    %r15,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
test   %rax,%rax
je     115f <SockWrite+0x3f>
add    %rax,%rbx
sub    %rax,%rbp
jne    1140 <SockWrite+0x20>
jmp    117e <SockWrite+0x5e>
jmp    117e <SockWrite+0x5e>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [17 x i8] c"write error: %s\0A\00"
define i64 @SockWrite(i32 %0, i8* %1, i16 zeroext %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i16
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i16 %2, i16* %7
%10 = load i16, i16* %7
%11 = zext i16 %10 to i64
store i64 %11, i64* %8
br label %12
12: ; preds = %34, %3
%13 = load i64, i64* %8
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %41
15: ; preds = %12
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i16, i16* %7
%19 = call i64 @write(i32 %16, i8* %17, i16 zeroext %18)
store i64 %19, i64* %9
%20 = icmp ule i64 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %15
%22 = load i64, i64* %9
%23 = icmp ult i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i64 0, i64* %9
br label %33
29: ; preds = %24, %21
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %6
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %31)
store i64 -1, i64* %4
br label %43
33: ; preds = %28
br label %34
34: ; preds = %33, %15
%35 = load i64, i64* %9
%36 = load i64, i64* %8
%37 = sub i64 %36, %35
store i64 %37, i64* %8
%38 = load i64, i64* %9
%39 = load i8*, i8** %6
%40 = getelementptr i8, i8* %39, i64 %38
store i8* %40, i8** %6
br label %12
41: ; preds = %12
%42 = load i64, i64* %9
store i64 %42, i64* %4
br label %43
43: ; preds = %41, %29
%44 = load i64, i64* %4
ret i64 %44
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [17 x i8] c"write error: %s\0A\00"
define i64 @SockWrite(i32 %0, i8* %1, i16 zeroext %2) {
%4 = icmp eq i16 %2, 0
br i1 %4, label %19, label %5
5: ; preds = %3
%6 = zext i16 %2 to i64
br label %7
7: ; preds = %5, %15
%8 = phi i64 [ %16, %15 ], [ %6, %5 ]
%9 = phi i8* [ %17, %15 ], [ %1, %5 ]
%10 = tail call i64 @write(i32 %0, i8* %9, i16 zeroext %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %19
15: ; preds = %7
%16 = sub i64 %8, %10
%17 = getelementptr i8, i8* %9, i64 %10
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %7
19: ; preds = %15, %3, %12
%20 = phi i64 [ -1, %12 ], [ undef, %3 ], [ %10, %15 ]
ret i64 %20
}