#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__ 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 = 0;
        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     11eb <_fwrite+0xcb>
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    11a6 <_fwrite+0x86>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    11a1 <_fwrite+0x81>
mov    0x2e4b(%rip),%rax
movq   $0x0,(%rax)
jmp    1144 <_fwrite+0x24>
jmp    11eb <_fwrite+0xcb>
cmpl   $0x0,-0x24(%rbp)
jne    11b5 <_fwrite+0x95>
jmp    11eb <_fwrite+0xcb>
jmp    11ba <_fwrite+0x9a>
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     1191 <_fwrite+0x71>
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    117e <_fwrite+0x5e>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
jne    1194 <_fwrite+0x74>
movq   $0x0,0x0(%r13)
jmp    1150 <_fwrite+0x30>
test   %eax,%eax
je     1194 <_fwrite+0x74>
cltq
add    %rax,%r12
add    %rax,%r14
sub    %rax,%rbx
jne    1150 <_fwrite+0x30>
jmp    1194 <_fwrite+0x74>
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
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 = %33, %26, %3
%12 = load i64, i64* %5
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %46
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 %28
22: ; preds = %14
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i64 0, i64* @errno
br label %11
27: ; preds = %22
br label %46
28: ; preds = %14
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
br label %46
32: ; preds = %28
br label %33
33: ; preds = %32
%34 = load i32, i32* %8
%35 = load i8*, i8** %9
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %9
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = load i64, i64* %7
%41 = add i64 %40, %39
store i64 %41, i64* %7
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = load i64, i64* %5
%45 = sub i64 %44, %43
store i64 %45, i64* %5
br label %11
46: ; preds = %31, %27, %11
%47 = load i64, i64* %7
ret i64 %47
}

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