#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 int /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
#define EAGAIN 128
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int);
scalar_t__ write(int, int /*<<< orphan*/ *, scalar_t__);
int write_all(int sfd, uint8_t *buf, ssize_t amt) {
  ssize_t w_amt, total;
  total = 0;
  do {
    w_amt = write(sfd, (buf + total), (amt - total));
    if (w_amt == 0) {
      continue;
    } else if (w_amt < 0) {
      switch (errno) {
      case EAGAIN:
        continue;
      default:
        fprintf(stderr, "error while writing to sfd %d: ", errno);
        puts(strerror(errno));
        return -1;
      }
    }
    total += w_amt;
  } while (total < amt);
  return 0;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118e <write_all+0x4e>
jmp    1207 <write_all+0xc7>
cmpq   $0x0,-0x20(%rbp)
jge    11f6 <write_all+0xb6>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    11b7 <write_all+0x77>
jmp    11b2 <write_all+0x72>
jmp    1207 <write_all+0xc7>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%edi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <write_all+0xdc>
jmp    11fb <write_all+0xbb>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jl     115b <write_all+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebp,%ebp
mov    0x2e7e(%rip),%r15
jmp    1168 <write_all+0x28>
nopl   0x0(%rax)
add    %rax,%rbp
cmp    %rbx,%rbp
jge    11b7 <write_all+0x77>
lea    (%r14,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r12d,%edi
call   1040 <write@plt>
test   %rax,%rax
je     1163 <write_all+0x23>
jns    1160 <write_all+0x20>
mov    (%r15),%edx
cmp    $0x80,%edx
je     1163 <write_all+0x23>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1050 <fprintf@plt>
mov    (%r15),%edi
call   1060 <strerror@plt>
mov    %eax,%edi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11b9 <write_all+0x79>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"error while writing to sfd %d: \00"
define i32 @write_all(i32 %0, i32* nocapture %1, i64 %2) {
br label %4
4: ; preds = %23, %3
%5 = phi i64 [ 0, %3 ], [ %24, %23 ]
%6 = getelementptr inbounds i32, i32* %1, i64 %5
%7 = sub nsw i64 %2, %5
%8 = tail call i64 @write(i32 %0, i32* %6, i64 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %4
%11 = icmp slt i64 %8, 0
br i1 %11, label %12, label %21
12: ; preds = %10
%13 = load i32, i32* @errno
%14 = icmp eq i32 %13, 128
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %13)
%18 = load i32, i32* @errno
%19 = tail call i32 @strerror(i32 %18)
%20 = tail call i32 @puts(i32 %19)
br label %26
21: ; preds = %10
%22 = add nsw i64 %8, %5
br label %23
23: ; preds = %12, %4, %21
%24 = phi i64 [ %5, %4 ], [ %5, %12 ], [ %22, %21 ]
%25 = icmp slt i64 %24, %2
br i1 %25, label %4, label %26
26: ; preds = %23, %15
%27 = phi i32 [ -1, %15 ], [ 0, %23 ]
ret i32 %27
}