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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, unsigned char const *, size_t);
int razor_write(int fd, const void *data, size_t size) {
  size_t rest;
  ssize_t written;
  const unsigned char *p;
  rest = size;
  p = data;
  while (rest > 0) {
    written = write(fd, p, rest);
    if (written < 0) {
      fprintf(stderr, "write error: %m\n");
      return -1;
    }
    rest -= written;
    p += written;
  }
  return 0;
}


x86 assembly (clang, -O0):
<razor_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11ae <razor_write+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    118e <razor_write+0x6e>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <razor_write+0x95>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1143 <razor_write+0x23>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<razor_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     1178 <razor_write+0x58>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     115d <razor_write+0x3d>
add    %rax,%rbp
sub    %rax,%rbx
jne    1140 <razor_write+0x20>
jmp    1178 <razor_write+0x58>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"write error: %m\0A\00"
define i32 @razor_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %13
%6 = phi i8* [ %15, %13 ], [ %1, %3 ]
%7 = phi i64 [ %14, %13 ], [ %2, %3 ]
%8 = tail call i64 @write(i32 %0, i8* %6, i64 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %17
13: ; preds = %5
%14 = sub i64 %7, %8
%15 = getelementptr inbounds i8, i8* %6, i64 %8
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %5
17: ; preds = %13, %3, %10
%18 = phi i32 [ -1, %10 ], [ 0, %3 ], [ 0, %13 ]
ret i32 %18
}