#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*/ thread_nanosleep(int /*<<< orphan*/, int);
int write(int, char const *, int);
void do_write(int sock, const char *data, int size) {
  int sts;
  do {
    sts = write(sock, data, size);
    if (sts < 0) {
      if (errno == EINTR) {
        continue;
      }
      thread_nanosleep(0, 1000);
    }
    data += sts;
    size -= sts;
  } while (size > 0);
  return;
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1179 <do_write+0x59>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
jne    116d <do_write+0x4d>
jmp    1195 <do_write+0x75>
xor    %edi,%edi
mov    $0x3e8,%esi
call   1030 <thread_nanosleep@plt>
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)
cmpl   $0x0,-0x14(%rbp)
jg     1132 <do_write+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2ea6(%rip),%r12
mov    0x2e97(%rip),%r13
jmp    115c <do_write+0x3c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %ebp,%rax
add    %rax,%r14
sub    %ebp,%ebx
test   %ebx,%ebx
jle    1187 <do_write+0x67>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1150 <do_write+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1158 <do_write+0x38>
xor    %edi,%edi
mov    $0x3e8,%esi
call   1030 <thread_nanosleep@plt>
jmp    1150 <do_write+0x30>
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
define void @do_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* %4
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call i32 @write(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %8
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
br label %30
20: ; preds = %15
%21 = call i32 @thread_nanosleep(i32 0, i32 1000)
br label %22
22: ; preds = %20, %8
%23 = load i32, i32* %7
%24 = load i8*, i8** %5
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8* %26, i8** %5
%27 = load i32, i32* %7
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, %27
store i32 %29, i32* %6
br label %30
30: ; preds = %22, %19
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 0
br i1 %32, label %8, label %33
33: ; preds = %30
ret void
}

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