#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 long ptrdiff_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
long write(int, char const *, size_t);
void do_write(int sd, const char *raw, size_t len) {
  size_t written = 0;
  ptrdiff_t n = -1;
  while (written < len) {
    do {
      ptrdiff_t __result;
      do {
        __result = (write(sd, raw + written, len - written));
      } while (__result == -1L && errno == EINTR);
      n = __result;
    } while (0);
    if (n == -1) {
      close(sd);
      do {
        perror("write() failed");
        exit(EXIT_FAILURE);
      } while (0);
    }
    written += (size_t)n;
  }
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1212 <do_write+0xd2>
jmp    1176 <do_write+0x36>
jmp    117b <do_write+0x3b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0xffffffffffffffff,-0x30(%rbp)
mov    %al,-0x31(%rbp)
jne    11c1 <do_write+0x81>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    117b <do_write+0x3b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1201 <do_write+0xc1>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
lea    0xe12(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1163 <do_write+0x23>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
test   %rdx,%rdx
je     11bf <do_write+0x7f>
mov    %rdx,%rbp
mov    %edi,%r12d
xor    %r13d,%r13d
mov    0x2e78(%rip),%r15
mov    0x2e61(%rip),%r14
mov    %rdx,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rsp),%rax
lea    (%rax,%r13,1),%rbx
sub    %r13,%rbp
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
jne    11ac <do_write+0x6c>
mov    (%r14),%rcx
cmp    %rcx,(%r15)
je     1190 <do_write+0x50>
cmp    $0xffffffffffffffff,%rax
je     11ce <do_write+0x8e>
add    %rax,%r13
mov    0x8(%rsp),%rbp
cmp    %rbp,%r13
jb     1180 <do_write+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12d,%edi
call   1040 <close@plt>
lea    0xe23(%rip),%rdi
call   1050 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"write() failed\00"
@EXIT_FAILURE = global i32 0
define void @do_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
store i64 -1, i64* %8
br label %10
10: ; preds = %47, %3
%11 = load i64, i64* %7
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %51
14: ; preds = %10
br label %15
15: ; preds = %14
br label %16
16: ; preds = %32, %15
%17 = load i32, i32* %4
%18 = load i8*, i8** %5
%19 = load i64, i64* %7
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i64, i64* %6
%22 = load i64, i64* %7
%23 = sub i64 %21, %22
%24 = call i64 @write(i32 %17, i8* %20, i64 %23)
store i64 %24, i64* %9
br label %25
25: ; preds = %16
%26 = load i64, i64* %9
%27 = icmp eq i64 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br label %32
32: ; preds = %28, %25
%33 = phi i1 [ false, %25 ], [ %31, %28 ]
br i1 %33, label %16, label %34
34: ; preds = %32
%35 = load i64, i64* %9
store i64 %35, i64* %8
br label %36
36: ; preds = %34
%37 = load i64, i64* %8
%38 = icmp eq i64 %37, -1
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; No predecessors!
br label %47
47: ; preds = %46, %36
%48 = load i64, i64* %8
%49 = load i64, i64* %7
%50 = add i64 %49, %48
store i64 %50, i64* %7
br label %10
51: ; preds = %10
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"write() failed\00"
@EXIT_FAILURE = global i32 0
define void @do_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %25, label %7
5: ; preds = %18
%6 = icmp ult i64 %19, %2
br i1 %6, label %7, label %25
7: ; preds = %3, %5
%8 = phi i64 [ %19, %5 ], [ 0, %3 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub i64 %2, %8
br label %11
11: ; preds = %7, %11
%12 = tail call i64 @write(i32 %0, i8* %9, i64 %10)
%13 = icmp eq i64 %12, -1
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %11, label %18
18: ; preds = %11
%19 = add i64 %12, %8
br i1 %13, label %20, label %5
20: ; preds = %18
%21 = tail call i32 @close(i32 %0)
%22 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %5, %3
ret void
}