#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 */
int /*<<< orphan*/ perror(char *);
int splice(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/ *, scalar_t__,
           int);
ssize_t splice_all(int fd_in, int pipe[2], int fd_out, ssize_t nr) {
  ssize_t r, re = 0, we = 0;
  do {
    int r = splice(fd_in, NULL, pipe[1], NULL, nr - re, 1);
    if (r < 0) {
      perror("splice");
      return -1;
    }
    re += r;
    r = splice(pipe[0], NULL, fd_out, NULL, nr - we, 1);
    if (r < 0) {
      perror("splice");
      return -1;
    }
    we += r;
  } while (we < nr);
  return nr;
}


x86 assembly (clang, -O0):
<splice_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x28(%rbp),%r8
sub    -0x38(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x1,%r9d
mov    %rcx,%rsi
call   1030 <splice@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1190 <splice_all+0x70>
lea    0xe82(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1207 <splice_all+0xe7>
movslq -0x44(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%r8
sub    -0x40(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x1,%r9d
mov    %rcx,%rsi
call   1030 <splice@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11e5 <splice_all+0xc5>
lea    0xe2d(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1207 <splice_all+0xe7>
movslq -0x44(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jl     1146 <splice_all+0x26>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<splice_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %edx,0x4(%rsp)
mov    %rsi,%r12
mov    %edi,%r15d
xor    %ebx,%ebx
xor    %r14d,%r14d
nopl   (%rax)
mov    0x4(%r12),%edx
mov    %r13,%r8
sub    %rbx,%r8
mov    %r15d,%edi
xor    %esi,%esi
xor    %ecx,%ecx
mov    $0x1,%r9d
call   1030 <splice@plt>
test   %eax,%eax
js     11a5 <splice_all+0x85>
mov    %eax,%ebp
mov    (%r12),%edi
mov    %r13,%r8
sub    %r14,%r8
xor    %esi,%esi
mov    0x4(%rsp),%edx
xor    %ecx,%ecx
mov    $0x1,%r9d
call   1030 <splice@plt>
test   %eax,%eax
js     11a5 <splice_all+0x85>
mov    %ebp,%ecx
add    %rcx,%rbx
mov    %eax,%eax
add    %rax,%r14
cmp    %r13,%r14
jl     1140 <splice_all+0x20>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe54(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffffffffffff,%r13
jmp    1193 <splice_all+0x73>

LLVM IR (-O0):
@.str = [7 x i8] c"splice\00"
define i64 @splice_all(i32 %0, i32* %1, i32 %2, i64 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i64 %3, i64* %9
store i64 0, i64* %11
store i64 0, i64* %12
br label %14
14: ; preds = %49, %4
%15 = load i32, i32* %6
%16 = load i32*, i32** %7
%17 = getelementptr inbounds i32, i32* %16, i64 1
%18 = load i32, i32* %17
%19 = load i64, i64* %9
%20 = load i64, i64* %11
%21 = sub nsw i64 %19, %20
%22 = call i32 @splice(i32 %15, i32* null, i32 %18, i32* null, i64 %21, i32 1)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %5
br label %55
27: ; preds = %14
%28 = load i32, i32* %13
%29 = sext i32 %28 to i64
%30 = load i64, i64* %11
%31 = add nsw i64 %30, %29
store i64 %31, i64* %11
%32 = load i32*, i32** %7
%33 = getelementptr inbounds i32, i32* %32, i64 0
%34 = load i32, i32* %33
%35 = load i32, i32* %8
%36 = load i64, i64* %9
%37 = load i64, i64* %12
%38 = sub nsw i64 %36, %37
%39 = call i32 @splice(i32 %34, i32* null, i32 %35, i32* null, i64 %38, i32 1)
store i32 %39, i32* %13
%40 = load i32, i32* %13
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %27
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %5
br label %55
44: ; preds = %27
%45 = load i32, i32* %13
%46 = sext i32 %45 to i64
%47 = load i64, i64* %12
%48 = add nsw i64 %47, %46
store i64 %48, i64* %12
br label %49
49: ; preds = %44
%50 = load i64, i64* %12
%51 = load i64, i64* %9
%52 = icmp slt i64 %50, %51
br i1 %52, label %14, label %53
53: ; preds = %49
%54 = load i64, i64* %9
store i64 %54, i64* %5
br label %55
55: ; preds = %53, %42, %25
%56 = load i64, i64* %5
ret i64 %56
}

LLVM IR (-O2):
@.str = [7 x i8] c"splice\00"
define i64 @splice_all(i32 %0, i32* nocapture %1, i32 %2, i64 %3) {
%5 = getelementptr inbounds i32, i32* %1, i64 1
br label %6
6: ; preds = %18, %4
%7 = phi i64 [ 0, %4 ], [ %20, %18 ]
%8 = phi i64 [ 0, %4 ], [ %22, %18 ]
%9 = load i32, i32* %5
%10 = sub nsw i64 %3, %7
%11 = tail call i32 @splice(i32 %0, i32* null, i32 %9, i32* null, i64 %10, i32 1)
%12 = icmp slt i32 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %6
%14 = load i32, i32* %1
%15 = sub nsw i64 %3, %8
%16 = tail call i32 @splice(i32 %14, i32* null, i32 %2, i32* null, i64 %15, i32 1)
%17 = icmp slt i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %13
%19 = zext i32 %11 to i64
%20 = add nuw nsw i64 %7, %19
%21 = zext i32 %16 to i64
%22 = add nuw nsw i64 %8, %21
%23 = icmp slt i64 %22, %3
br i1 %23, label %6, label %26
24: ; preds = %13, %6
%25 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %18, %24
%27 = phi i64 [ -1, %24 ], [ %3, %18 ]
ret i64 %27
}