#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int splice(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/ *,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd;
  int slen;
  if (argc < 2) {
    printf("Usage: ./splice_read out | cat\n");
    exit(-1);
  }
  fd = open(argv[1], O_RDONLY, 0644);
  if (fd == -1) {
    printf("open file failed.\n");
    exit(-1);
  }
  slen = splice(fd, NULL, STDOUT_FILENO, NULL, 10000000, 0);
  if (slen < 0)
    fprintf(stderr, "splice failed.\n");
  else
    fprintf(stderr, "spliced length = %d\n", slen);
  close(fd);
  if (slen < 0)
    exit(-1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1196 <main+0x36>
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d4 <main+0x74>
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x989680,%r8d
xor    %r9d,%r9d
mov    %rcx,%rsi
call   1030 <splice@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    121e <main+0xbe>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1238 <main+0xd8>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x18(%rbp)
jge    1254 <main+0xf4>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jle    11c9 <main+0x69>
mov    0x8(%rsi),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11d2 <main+0x72>
mov    %eax,%ebx
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
mov    $0x989680,%r8d
xor    %r9d,%r9d
call   1030 <splice@plt>
mov    0x2e28(%rip),%rcx
mov    (%rcx),%edi
test   %eax,%eax
js     11e8 <main+0x88>
lea    0xe5b(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe67(%rip),%rdi
jmp    11d9 <main+0x79>
lea    0xe4c(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xe11(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: ./splice_read out | cat\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [19 x i8] c"open file failed.\0A\00"
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str.2 = [16 x i8] c"splice failed.\0A\00"
@.str.3 = [21 x i8] c"spliced length = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17, i32 420)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %13
%25 = load i32, i32* %6
%26 = load i32, i32* @STDOUT_FILENO
%27 = call i32 @splice(i32 %25, i32* null, i32 %26, i32* null, i32 10000000, i32 0)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %37
33: ; preds = %24
%34 = load i32, i32* @stderr
%35 = load i32, i32* %7
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %30
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %37
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str.2 = [16 x i8] c"splice failed.\0A\00"
@.str.3 = [21 x i8] c"spliced length = %d\0A\00"
@str = [18 x i8] c"open file failed.\00"
@str.4 = [31 x i8] c"Usage: ./splice_read out | cat\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10, i32 420)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %7
%17 = load i32, i32* @STDOUT_FILENO
%18 = tail call i32 @splice(i32 %11, i32* null, i32 %17, i32* null, i32 10000000, i32 0)
%19 = icmp slt i32 %18, 0
%20 = load i32, i32* @stderr
br i1 %19, label %21, label %25
21: ; preds = %16
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @close(i32 %11)
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %16
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%27 = tail call i32 @close(i32 %11)
ret i32 0
}