#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*/ err(int, char *);
int write(int, void const *, int);
void swrite(int fd, const void *buf, size_t nb) {
  ssize_t nw, off = 0;
  while (nb > 0) {
    nw = write(fd, buf + off, nb - off);
    if (nw < 0)
      err(1, "write failed");
    nb -= nw;
    off += nw;
  }
}


x86 assembly (clang, -O0):
<swrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11a0 <swrite+0x80>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1183 <swrite+0x63>
mov    $0x1,%edi
lea    0xe82(%rip),%rsi
call   1040 <err@plt>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    113a <swrite+0x1a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1183 <swrite+0x63>
mov    %rdx,%r13
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xebe(%rip),%r15
jmp    115a <swrite+0x3a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %ebp,%rax
add    %ebp,%ebx
sub    %rax,%r13
je     1183 <swrite+0x63>
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %r13d,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1150 <swrite+0x30>
mov    $0x1,%edi
mov    %r15,%rsi
call   1040 <err@plt>
jmp    1150 <swrite+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"write failed\00"
define void @swrite(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %8
br label %9
9: ; preds = %28, %3
%10 = load i64, i64* %6
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %36
12: ; preds = %9
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i32, i32* %8
%16 = sext i32 %15 to i64
%17 = getelementptr i8, i8* %14, i64 %16
%18 = load i64, i64* %6
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = sub i64 %18, %20
%22 = trunc i64 %21 to i32
%23 = call i32 @write(i32 %13, i8* %17, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %12
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %12
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = load i64, i64* %6
%32 = sub i64 %31, %30
store i64 %32, i64* %6
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = add nsw i32 %34, %33
store i32 %35, i32* %8
br label %9
36: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"write failed\00"
define void @swrite(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %16
%6 = phi i32 [ %19, %16 ], [ 0, %3 ]
%7 = phi i64 [ %18, %16 ], [ %2, %3 ]
%8 = sext i32 %6 to i64
%9 = getelementptr i8, i8* %1, i64 %8
%10 = trunc i64 %7 to i32
%11 = sub i32 %10, %6
%12 = tail call i32 @write(i32 %0, i8* %9, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %5
%15 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %5
%17 = sext i32 %12 to i64
%18 = sub i64 %7, %17
%19 = add nsw i32 %12, %6
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %5
21: ; preds = %16, %3
ret void
}