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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int write(int, char const *, size_t);
ssize_t write_all(int fd, const void *buf, size_t nbyte) {
  size_t bytes_left;
  const char *cbuf = (const char *)buf;
  if (fd < 0 && NULL == cbuf) {
    errno = EINVAL;
    return -1;
  }
  for (bytes_left = nbyte; bytes_left > 0;) {
    ssize_t transferred;
    if ((transferred = write(fd, cbuf, bytes_left)) < 0)
      return -1;
    bytes_left -= transferred;
    cbuf += transferred;
  }
  return nbyte;
}


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)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    115f <write_all+0x4f>
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    115f <write_all+0x4f>
mov    0x2e90(%rip),%rax
mov    (%rax),%ecx
mov    0x2e8f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <write_all+0xb6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11bf <write_all+0xaf>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    119a <write_all+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <write_all+0xb6>
movslq -0x2c(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1167 <write_all+0x57>
mov    -0x18(%rbp),%rax
mov    %eax,-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   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rdx,%rbp
test   %edi,%edi
jns    116a <write_all+0x5a>
test   %rbx,%rbx
jne    116a <write_all+0x5a>
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    0x2ea4(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
jmp    116f <write_all+0x5f>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cltq
sub    %rax,%rbp
add    %rax,%rbx
test   %eax,%eax
js     113e <write_all+0x2e>
test   %rbp,%rbp
jne    1150 <write_all+0x40>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %9
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i8*, i8** %9
%16 = icmp eq i8* null, %15
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
store i32 -1, i32* %4
br label %43
19: ; preds = %14, %3
%20 = load i64, i64* %7
store i64 %20, i64* %8
br label %21
21: ; preds = %31, %19
%22 = load i64, i64* %8
%23 = icmp ugt i64 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %21
%25 = load i32, i32* %5
%26 = load i8*, i8** %9
%27 = load i64, i64* %8
%28 = call i32 @write(i32 %25, i8* %26, i64 %27)
store i32 %28, i32* %10
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %4
br label %43
31: ; preds = %24
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = load i64, i64* %8
%35 = sub i64 %34, %33
store i64 %35, i64* %8
%36 = load i32, i32* %10
%37 = load i8*, i8** %9
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8* %39, i8** %9
br label %21
40: ; preds = %21
%41 = load i64, i64* %7
%42 = trunc i64 %41 to i32
store i32 %42, i32* %4
br label %43
43: ; preds = %40, %30, %17
%44 = load i32, i32* %4
ret i32 %44
}

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