#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, void const *, size_t);
ssize_t do_write(int fd, const void *buf, size_t count) {
  size_t offset = 0;
  ssize_t l;
  while (offset < count) {
    l = write(fd, buf + offset, count - offset);
    if (l < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK)
        continue;
      perror("write");
      return -1;
    }
    offset += l;
  }
  return offset;
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11d3 <do_write+0xb3>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11c2 <do_write+0xa2>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <do_write+0x84>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11a9 <do_write+0x89>
jmp    113b <do_write+0x1b>
lea    0xe50(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11db <do_write+0xbb>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    113b <do_write+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%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
test   %rdx,%rdx
je     11d6 <do_write+0xb6>
mov    %edi,%r13d
xor    %eax,%eax
mov    0x2e9d(%rip),%r15
mov    0x2e7e(%rip),%r14
mov    0x2e87(%rip),%r12
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdx,0x8(%rsp)
mov    %rsi,(%rsp)
jmp    118d <do_write+0x6d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x10(%rsp),%rcx
add    %rax,%rcx
mov    %rcx,%rax
mov    (%rsp),%rsi
lea    (%rsi,%rcx,1),%rbp
mov    0x8(%rsp),%rbx
sub    %rcx,%rbx
jbe    11d8 <do_write+0xb8>
mov    %rax,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    1170 <do_write+0x50>
mov    (%r15),%rax
cmp    (%r14),%rax
je     11a0 <do_write+0x80>
cmp    (%r12),%rax
je     11a0 <do_write+0x80>
lea    0xe38(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffffffffffff,%rax
jmp    11d8 <do_write+0xb8>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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