#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 */
int BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
void stdin_to_stream(char *buf, ssize_t r, FILE *outf) {
  while (r > 0) {
    if (fwrite(buf, r * sizeof(char), 1, outf) < 1) {
      fprintf(stderr, "Write error\n");
      exit(EXIT_FAILURE);
    }
    r = read(0, buf, BUFSIZ * sizeof(char));
  }
  if (r == -1) {
    perror("read");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<stdin_to_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11d8 <stdin_to_stream+0x88>
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jge    11b5 <stdin_to_stream+0x65>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rsi
mov    0x2e18(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edx
xor    %edi,%edi
call   1030 <read@plt>
mov    %eax,-0xc(%rbp)
jmp    1163 <stdin_to_stream+0x13>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fc <stdin_to_stream+0xac>
lea    0xe24(%rip),%rdi
call   1050 <perror@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdin_to_stream>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%eax
test   %esi,%esi
jle    1197 <stdin_to_stream+0x47>
mov    %rdx,%r14
mov    %rdi,%rbx
mov    0x2e70(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
test   %eax,%eax
jle    11a2 <stdin_to_stream+0x52>
mov    (%r15),%edx
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <read@plt>
test   %eax,%eax
jg     1170 <stdin_to_stream+0x20>
cmp    $0xffffffff,%eax
je     11c5 <stdin_to_stream+0x75>
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
jmp    11b7 <stdin_to_stream+0x67>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"Write error\0A\00"
@EXIT_FAILURE = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [5 x i8] c"read\00"
define void @stdin_to_stream(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
br label %7
7: ; preds = %24, %3
%8 = load i32, i32* %5
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %7
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 1
%15 = trunc i64 %14 to i32
%16 = load i32*, i32** %6
%17 = call i32 @fwrite(i8* %11, i32 %15, i32 1, i32* %16)
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %10
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %10
%25 = load i8*, i8** %4
%26 = load i32, i32* @BUFSIZ
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 1
%29 = trunc i64 %28 to i32
%30 = call i32 @read(i32 0, i8* %25, i32 %29)
store i32 %30, i32* %5
br label %7
31: ; preds = %7
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %31
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"Write error\0A\00"
@EXIT_FAILURE = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [5 x i8] c"read\00"
define void @stdin_to_stream(i8* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %18
5: ; preds = %3, %14
%6 = phi i32 [ %16, %14 ], [ %1, %3 ]
%7 = tail call i32 @fwrite(i8* %0, i32 %6, i32 1, i32* %2)
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %5
%15 = load i32, i32* @BUFSIZ
%16 = tail call i32 @read(i32 0, i8* %0, i32 %15)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %5, label %18
18: ; preds = %14, %3
%19 = phi i32 [ %1, %3 ], [ %16, %14 ]
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %18
ret void
}