#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*/ ERANGE;
size_t SSIZE_MAX;
int /*<<< orphan*/ errno;
int write(int, char const *, size_t);
ssize_t nwrite(int fd, const void *b, size_t s) {
  size_t i;
  ssize_t t;
  if (b == NULL) {
    errno = EINVAL;
    return (-1);
  }
  if (s > SSIZE_MAX) {
    errno = ERANGE;
    return (-1);
  }
  for (i = 0; i < s; i += t)
    if ((t = write(fd, (const char *)b + i, s - i)) < 0)
      return (t);
    else if (t == 0)
      return (i);
  return (s);
}


x86 assembly (clang, -O0):
<nwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114c <nwrite+0x3c>
mov    0x2e93(%rip),%rax
mov    (%rax),%ecx
mov    0x2e92(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <nwrite+0xeb>
mov    -0x18(%rbp),%rax
mov    0x2e91(%rip),%rcx
cmp    (%rcx),%rax
jbe    117e <nwrite+0x6e>
mov    0x2e79(%rip),%rax
mov    (%rax),%ecx
mov    0x2e60(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <nwrite+0xeb>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11f4 <nwrite+0xe4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11c3 <nwrite+0xb3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <nwrite+0xeb>
cmpl   $0x0,-0x24(%rbp)
jne    11d9 <nwrite+0xc9>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11fb <nwrite+0xeb>
jmp    11de <nwrite+0xce>
jmp    11e3 <nwrite+0xd3>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1186 <nwrite+0x76>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1134 <nwrite+0x24>
mov    %rdx,%rbp
mov    0x2ec2(%rip),%rax
cmp    %rdx,(%rax)
jae    1158 <nwrite+0x48>
mov    0x2eae(%rip),%rax
jmp    113b <nwrite+0x2b>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    0x2e8c(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %rbp,%rbp
je     114b <nwrite+0x3b>
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r14,%rbx,1),%rsi
mov    %rbp,%rdx
sub    %rbx,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
js     1194 <nwrite+0x84>
je     1198 <nwrite+0x88>
mov    %eax,%eax
add    %rax,%rbx
cmp    %rbp,%rbx
jb     1170 <nwrite+0x60>
jmp    114b <nwrite+0x3b>
mov    %eax,%ebp
jmp    114b <nwrite+0x3b>
mov    %ebx,%ebp
jmp    114b <nwrite+0x3b>

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

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