#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 */
scalar_t__ writeFromb(int, char *, size_t);
int writexactly(int fd, char *buf, size_t count) {
  ssize_t i;
  int j = 0, n = 0;
  for (;;) {
    if ((i = writeFromb(fd, buf + j, count - j)) < 0)
      return -1;
    j += i;
    if (j == count)
      break;
    if (i == 0)
      n++;
    else
      n = 0;
    if (n > 2)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writexactly>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x24(%rbp),%rax
sub    %rax,%rdx
call   1030 <writeFromb@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1169 <writexactly+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <writexactly+0xbc>
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    118a <writexactly+0x7a>
jmp    11c5 <writexactly+0xb5>
cmpq   $0x0,-0x20(%rbp)
jne    11a3 <writexactly+0x93>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11aa <writexactly+0x9a>
movl   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x28(%rbp)
jle    11c0 <writexactly+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <writexactly+0xbc>
jmp    1131 <writexactly+0x21>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writexactly>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r14
mov    %edi,%r12d
xor    %r15d,%r15d
xor    %ebp,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rbp
lea    (%r14,%rbp,1),%rsi
mov    %r13,%rdx
sub    %rbp,%rdx
mov    %r12d,%edi
call   1030 <writeFromb@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
js     1170 <writexactly+0x60>
add    %rcx,%rbp
movslq %ebp,%rdx
cmp    %r13,%rdx
je     116e <writexactly+0x5e>
add    $0x1,%ebx
test   %rcx,%rcx
cmovne %r15d,%ebx
cmp    $0x3,%ebx
jl     1130 <writexactly+0x20>
jmp    1170 <writexactly+0x60>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @writexactly(i32 %0, i8* %1, i64 %2) {
br label %4
4: ; preds = %18, %3
%5 = phi i64 [ 0, %3 ], [ %14, %18 ]
%6 = phi i32 [ 0, %3 ], [ %21, %18 ]
%7 = shl i64 %5, 32
%8 = ashr exact i64 %7, 32
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub i64 %2, %8
%11 = tail call i64 @writeFromb(i32 %0, i8* %9, i64 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %23, label %13
13: ; preds = %4
%14 = add nsw i64 %8, %11
%15 = shl i64 %14, 32
%16 = ashr exact i64 %15, 32
%17 = icmp eq i64 %16, %2
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = icmp eq i64 %11, 0
%20 = add nsw i32 %6, 1
%21 = select i1 %19, i32 %20, i32 0
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %4
23: ; preds = %13, %18, %4
%24 = phi i32 [ -1, %4 ], [ -1, %18 ], [ 0, %13 ]
ret i32 %24
}