#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ sleep(int);
int write(int, char const *, size_t);
size_t httpd_write_fully(int fd, const char *buf, size_t sz) {
  size_t written = 0;
  while (written < sz) {
    int r = 0;
    r = write(fd, buf + written, sz - written);
    if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
      sleep(1);
      continue;
    }
    if (r < 0) {
      return r;
    }
    if (r == 0) {
      break;
    }
    written += r;
  }
  return written;
}


x86 assembly (clang, -O0):
<httpd_write_fully>:
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    11ef <httpd_write_fully+0xcf>
movl   $0x0,-0x2c(%rbp)
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    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11b8 <httpd_write_fully+0x98>
mov    0x2e64(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     11a9 <httpd_write_fully+0x89>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
jne    11b8 <httpd_write_fully+0x98>
mov    $0x1,%edi
call   1040 <sleep@plt>
jmp    113b <httpd_write_fully+0x1b>
cmpl   $0x0,-0x2c(%rbp)
jge    11cf <httpd_write_fully+0xaf>
movslq -0x2c(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f7 <httpd_write_fully+0xd7>
cmpl   $0x0,-0x2c(%rbp)
jne    11de <httpd_write_fully+0xbe>
jmp    11ef <httpd_write_fully+0xcf>
movslq -0x2c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    113b <httpd_write_fully+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<httpd_write_fully>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11ab <httpd_write_fully+0x8b>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2e9f(%rip),%r12
mov    0x2e90(%rip),%r13
jmp    115f <httpd_write_fully+0x3f>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1040 <sleep@plt>
cmp    %r14,%rbx
jae    11ad <httpd_write_fully+0x8d>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
test   %eax,%eax
js     1190 <httpd_write_fully+0x70>
je     11ad <httpd_write_fully+0x8d>
mov    %eax,%eax
add    %rax,%rbx
cmp    %r14,%rbx
jb     115f <httpd_write_fully+0x3f>
jmp    11ad <httpd_write_fully+0x8d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1150 <httpd_write_fully+0x30>
mov    0x2e27(%rip),%rdx
cmp    (%rdx),%rcx
je     1150 <httpd_write_fully+0x30>
movslq %eax,%rbx
jmp    11ad <httpd_write_fully+0x8d>
xor    %ebx,%ebx
mov    %rbx,%rax
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
@EAGAIN = global i64 0
define i64 @httpd_write_fully(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
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 = %45, %33, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %50
14: ; preds = %10
store i32 0, i32* %9
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %8
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = call i32 @write(i32 %15, i8* %18, i64 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %14
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%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* @EAGAIN
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29, %25
%34 = call i32 @sleep(i32 1)
br label %10
35: ; preds = %29, %14
%36 = load i32, i32* %9
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
store i64 %40, i64* %4
br label %52
41: ; preds = %35
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
br label %50
45: ; preds = %41
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = load i64, i64* %8
%49 = add i64 %48, %47
store i64 %49, i64* %8
br label %10
50: ; preds = %44, %10
%51 = load i64, i64* %8
store i64 %51, i64* %4
br label %52
52: ; preds = %50, %38
%53 = load i64, i64* %4
ret i64 %53
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @httpd_write_fully(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %30, label %5
5: ; preds = %3, %27
%6 = phi i64 [ %28, %27 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i32 @write(i32 %0, i8* %7, i64 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @sleep(i32 1)
br label %27
20: ; preds = %11
%21 = sext i32 %9 to i64
br label %30
22: ; preds = %5
%23 = icmp eq i32 %9, 0
br i1 %23, label %30, label %24
24: ; preds = %22
%25 = zext i32 %9 to i64
%26 = add i64 %6, %25
br label %27
27: ; preds = %24, %18
%28 = phi i64 [ %6, %18 ], [ %26, %24 ]
%29 = icmp ult i64 %28, %2
br i1 %29, label %5, label %30
30: ; preds = %27, %22, %3, %20
%31 = phi i64 [ %21, %20 ], [ 0, %3 ], [ %28, %27 ], [ %6, %22 ]
ret i64 %31
}