#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int write(int /*<<< orphan*/, char const *, size_t);
size_t _fwrite(const void *buf, size_t count, FILE *f) {
  size_t bytes = 0;
  ssize_t rv;
  const char *p = buf;
  while (count) {
    rv = write(fileno(f), p, count);
    if (rv == -1) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      else
        break;
    } else if (rv == 0) {
      break;
    }
    p += rv;
    bytes += rv;
    count -= rv;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<_fwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f7 <_fwrite+0xd7>
mov    -0x18(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b2 <_fwrite+0x92>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <_fwrite+0x88>
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <_fwrite+0x8d>
jmp    1144 <_fwrite+0x24>
jmp    11f7 <_fwrite+0xd7>
cmpl   $0x0,-0x24(%rbp)
jne    11c1 <_fwrite+0xa1>
jmp    11f7 <_fwrite+0xd7>
jmp    11c6 <_fwrite+0xa6>
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
movslq -0x24(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1144 <_fwrite+0x24>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     1195 <_fwrite+0x75>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %r14d,%r14d
mov    0x2e9d(%rip),%r13
mov    0x2e8e(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1182 <_fwrite+0x62>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     1150 <_fwrite+0x30>
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
je     1150 <_fwrite+0x30>
jmp    1198 <_fwrite+0x78>
test   %eax,%eax
je     1198 <_fwrite+0x78>
cltq
add    %rax,%r12
add    %rax,%r14
sub    %rax,%rbx
jne    1150 <_fwrite+0x30>
jmp    1198 <_fwrite+0x78>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @_fwrite(i8* nocapture %0, i64 %1, i32* nocapture %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %25, label %5
5: ; preds = %3, %19
%6 = phi i8* [ %21, %19 ], [ %0, %3 ]
%7 = phi i64 [ %23, %19 ], [ %1, %3 ]
%8 = phi i64 [ %22, %19 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %12
%10 = tail call i32 @fileno(i32* %2)
%11 = tail call i32 @write(i32 %10, i8* %6, i64 %7)
switch i32 %11, label %19 [
i32 -1, label %12
i32 0, label %25
]
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %9, label %25
19: ; preds = %9
%20 = sext i32 %11 to i64
%21 = getelementptr inbounds i8, i8* %6, i64 %20
%22 = add i64 %8, %20
%23 = sub i64 %7, %20
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %5
25: ; preds = %19, %12, %9, %3
%26 = phi i64 [ 0, %3 ], [ %8, %9 ], [ %8, %12 ], [ %22, %19 ]
ret i64 %26
}