#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 */

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
int bot_fileutils_write_fully(int fd, const void *b, int len) {
  int cnt = 0;
  int thiscnt;
  unsigned char *bb = (unsigned char *)b;
  while (cnt < len) {
    thiscnt = write(fd, &bb[cnt], len - cnt);
    if (thiscnt < 0) {
      perror("write");
      return -1;
    }
    cnt += thiscnt;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<bot_fileutils_write_fully>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1199 <bot_fileutils_write_fully+0x79>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118b <bot_fileutils_write_fully+0x6b>
lea    0xe86(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <bot_fileutils_write_fully+0x7f>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1141 <bot_fileutils_write_fully+0x21>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bot_fileutils_write_fully>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    115f <bot_fileutils_write_fully+0x3f>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
js     116e <bot_fileutils_write_fully+0x4e>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1140 <bot_fileutils_write_fully+0x20>
jmp    1161 <bot_fileutils_write_fully+0x41>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8b(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1161 <bot_fileutils_write_fully+0x41>

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

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