#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;
int /*<<< orphan*/ errmsg(char *);
scalar_t__ errno;
int write(int, char const *, int);
void internal_tcp_raw_write(int fd, const char *data, int len) {
  while (len > 0) {
    int i;
    while ((i = write(fd, data, len)) < 0 && errno == EINTR)
      ;
    if (i < 0) {
      errmsg("write to socket");
      break;
    }
    data += i;
    len -= i;
  }
}


x86 assembly (clang, -O0):
<internal_tcp_raw_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11ce <internal_tcp_raw_write+0xae>
jmp    1141 <internal_tcp_raw_write+0x21>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
jge    117d <internal_tcp_raw_write+0x5d>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    118d <internal_tcp_raw_write+0x6d>
jmp    1192 <internal_tcp_raw_write+0x72>
jmp    1141 <internal_tcp_raw_write+0x21>
cmpl   $0x0,-0x18(%rbp)
jge    11ad <internal_tcp_raw_write+0x8d>
lea    0xe5d(%rip),%rdi
call   1040 <errmsg@plt>
jmp    11ce <internal_tcp_raw_write+0xae>
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)
jmp    1132 <internal_tcp_raw_write+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<internal_tcp_raw_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    1177 <internal_tcp_raw_write+0x57>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2ea5(%rip),%r15
mov    0x2e96(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    116a <internal_tcp_raw_write+0x4a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1150 <internal_tcp_raw_write+0x30>
test   %eax,%eax
js     1180 <internal_tcp_raw_write+0x60>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1150 <internal_tcp_raw_write+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe79(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <errmsg@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [16 x i8] c"write to socket\00"
define void @internal_tcp_raw_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %30, %3
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %8
br label %12
12: ; preds = %24, %11
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br label %22
22: ; preds = %18, %12
%23 = phi i1 [ false, %12 ], [ %21, %18 ]
br i1 %23, label %24, label %25
24: ; preds = %22
br label %12
25: ; preds = %22
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @errmsg(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %38
30: ; preds = %25
%31 = load i32, i32* %7
%32 = load i8*, i8** %5
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %5
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %6
br label %8
38: ; preds = %28, %8
ret void
}

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