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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ ferror(int /*<<< orphan*/ *);
int fwrite(char const *, int, int, int /*<<< orphan*/ *);
int(_io_write_stdio)(void *data, const char *s, int n) {
  int ndone;
  int nnew;
  FILE *fp = (FILE *)data;
  for (ndone = 0; ndone < n; ndone += nnew) {
    int nmore = n - ndone;
    nnew = fwrite(s, sizeof(char), nmore, fp);
    if (nnew < nmore) {
      if (errno == EINTR)
        clearerr(fp);
      else
        return ferror(fp) ? -1 : ndone + nnew;
    };
  };
  return ndone;
}


x86 assembly (clang, -O0):
<_io_write_stdio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11ff <_io_write_stdio+0xcf>
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    11ec <_io_write_stdio+0xbc>
mov    0x2e4e(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    11b3 <_io_write_stdio+0x83>
mov    -0x30(%rbp),%rdi
call   1040 <clearerr@plt>
jmp    11e7 <_io_write_stdio+0xb7>
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11d3 <_io_write_stdio+0xa3>
mov    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
jmp    11dc <_io_write_stdio+0xac>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1205 <_io_write_stdio+0xd5>
jmp    11ec <_io_write_stdio+0xbc>
jmp    11f1 <_io_write_stdio+0xc1>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1152 <_io_write_stdio+0x22>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_io_write_stdio>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1196 <_io_write_stdio+0x66>
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebx,%ebx
jmp    1158 <_io_write_stdio+0x28>
nopl   0x0(%rax)
add    %r13d,%ebx
cmp    %r12d,%ebx
jge    11ad <_io_write_stdio+0x7d>
mov    %r12d,%ebp
sub    %ebx,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1050 <fwrite@plt>
mov    %eax,%r13d
cmp    %ebp,%eax
jge    1150 <_io_write_stdio+0x20>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <_io_write_stdio+0x6a>
mov    %r14,%rdi
call   1040 <clearerr@plt>
jmp    1150 <_io_write_stdio+0x20>
xor    %ebx,%ebx
jmp    11ad <_io_write_stdio+0x7d>
mov    %r14,%rdi
call   1030 <ferror@plt>
add    %ebx,%r13d
neg    %rax
sbb    %ebx,%ebx
or     %r13d,%ebx
mov    %ebx,%eax
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 i32 @_io_write_stdio(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %10
store i32 0, i32* %8
br label %14
14: ; preds = %49, %3
%15 = load i32, i32* %8
%16 = load i32, i32* %7
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %53
18: ; preds = %14
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = sub nsw i32 %19, %20
store i32 %21, i32* %11
%22 = load i8*, i8** %6
%23 = load i32, i32* %11
%24 = load i32*, i32** %10
%25 = call i32 @fwrite(i8* %22, i32 1, i32 %23, i32* %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = load i32, i32* %11
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %48
29: ; preds = %18
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32*, i32** %10
%35 = call i32 @clearerr(i32* %34)
br label %47
36: ; preds = %29
%37 = load i32*, i32** %10
%38 = call i64 @ferror(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
br label %45
41: ; preds = %36
%42 = load i32, i32* %8
%43 = load i32, i32* %9
%44 = add nsw i32 %42, %43
br label %45
45: ; preds = %41, %40
%46 = phi i32 [ -1, %40 ], [ %44, %41 ]
store i32 %46, i32* %4
br label %55
47: ; preds = %33
br label %48
48: ; preds = %47, %18
br label %49
49: ; preds = %48
%50 = load i32, i32* %9
%51 = load i32, i32* %8
%52 = add nsw i32 %51, %50
store i32 %52, i32* %8
br label %14
53: ; preds = %14
%54 = load i32, i32* %8
store i32 %54, i32* %4
br label %55
55: ; preds = %53, %45
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @_io_write_stdio(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = bitcast i8* %0 to i32*
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %25
6: ; preds = %3, %22
%7 = phi i32 [ %23, %22 ], [ 0, %3 ]
%8 = sub nsw i32 %2, %7
%9 = tail call i32 @fwrite(i8* %1, i32 1, i32 %8, i32* %4)
%10 = icmp slt i32 %9, %8
br i1 %10, label %11, label %22
11: ; preds = %6
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @clearerr(i32* %4)
br label %22
17: ; preds = %11
%18 = tail call i64 @ferror(i32* %4)
%19 = icmp eq i64 %18, 0
%20 = add nsw i32 %9, %7
%21 = select i1 %19, i32 %20, i32 -1
br label %25
22: ; preds = %15, %6
%23 = add nsw i32 %9, %7
%24 = icmp slt i32 %23, %2
br i1 %24, label %6, label %25
25: ; preds = %22, %3, %17
%26 = phi i32 [ %21, %17 ], [ 0, %3 ], [ %23, %22 ]
ret i32 %26
}