#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
long atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
long splice(int /*<<< orphan*/, int /*<<< orphan*/ *, int, int /*<<< orphan*/ *,
            long, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd;
  long int slen = 0;
  long int to_write = 1000000;
  if (argc < 2) {
    printf("Usage: ls | ./splice_write out\n");
    exit(-1);
  }
  fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd == -1) {
    printf("open file failed.\n");
    exit(-1);
  }
  if (argc == 3)
    to_write = atol(argv[2]);
  while (to_write > 0) {
    slen = splice(STDIN_FILENO, NULL, fd, NULL, to_write, 0);
    if (slen < 0)
      fprintf(stderr, "splice failed.\n");
    else if (slen == 0)
      break;
    else {
      to_write -= slen;
      fprintf(stderr, "spliced length = %ld\n", slen);
    }
  }
  close(fd);
  if (slen < 0)
    exit(-1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0xf4240,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b6 <main+0x46>
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1206 <main+0x96>
lea    0xe29(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jne    1221 <main+0xb1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atol@plt>
mov    %rax,-0x28(%rbp)
jmp    1226 <main+0xb6>
cmpq   $0x0,-0x28(%rbp)
jle    12bf <main+0x14f>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
xor    %r9d,%r9d
mov    %rcx,%rsi
call   1030 <splice@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    127b <main+0x10b>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ba <main+0x14a>
cmpq   $0x0,-0x20(%rbp)
jne    128b <main+0x11b>
jmp    12bf <main+0x14f>
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ba <main+0x14a>
jmp    1226 <main+0xb6>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x20(%rbp)
jge    12dc <main+0x16c>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    127e <main+0x10e>
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e28(%rip),%rax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r14),%rdi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1287 <main+0x117>
mov    %eax,%r12d
mov    $0xf4240,%ebx
cmp    $0x3,%ebp
jne    11dd <main+0x6d>
mov    0x10(%r14),%rdi
call   1070 <atol@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    1265 <main+0xf5>
mov    0x2de4(%rip),%r13
mov    0x2de5(%rip),%r14
lea    0xe1e(%rip),%r15
jmp    1216 <main+0xa6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14),%edi
lea    0xdf6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %rbx,%rbx
jle    124e <main+0xde>
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
xor    %r9d,%r9d
call   1030 <splice@plt>
mov    %rax,%rbp
test   %rax,%rax
js     1200 <main+0x90>
je     1265 <main+0xf5>
sub    %rbp,%rbx
mov    (%r14),%edi
mov    %r15,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %rbx,%rbx
jg     1216 <main+0xa6>
mov    %r12d,%edi
call   1050 <close@plt>
test   %rbp,%rbp
jns    126d <main+0xfd>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb3(%rip),%rdi
jmp    128e <main+0x11e>
lea    0xd98(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: ls | ./splice_write out\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [19 x i8] c"open file failed.\0A\00"
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str.2 = [16 x i8] c"splice failed.\0A\00"
@.str.3 = [22 x i8] c"spliced length = %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %7
store i64 1000000, i64* %8
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %17, i32 %22, i32 420)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %14
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 3
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i64 @atol(i8* %35)
store i64 %36, i64* %8
br label %37
37: ; preds = %32, %29
br label %38
38: ; preds = %63, %37
%39 = load i64, i64* %8
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %64
41: ; preds = %38
%42 = load i32, i32* @STDIN_FILENO
%43 = load i32, i32* %6
%44 = load i64, i64* %8
%45 = call i64 @splice(i32 %42, i32* null, i32 %43, i32* null, i64 %44, i32 0)
store i64 %45, i64* %7
%46 = load i64, i64* %7
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %41
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %63
51: ; preds = %41
%52 = load i64, i64* %7
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
br label %64
55: ; preds = %51
%56 = load i64, i64* %7
%57 = load i64, i64* %8
%58 = sub nsw i64 %57, %56
store i64 %58, i64* %8
%59 = load i32, i32* @stderr
%60 = load i64, i64* %7
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %60)
br label %62
62: ; preds = %55
br label %63
63: ; preds = %62, %48
br label %38
64: ; preds = %54, %38
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
%67 = load i64, i64* %7
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %64
%70 = call i32 @exit(i32 -1)
unreachable
71: ; preds = %64
ret i32 0
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str.2 = [16 x i8] c"splice failed.\0A\00"
@.str.3 = [22 x i8] c"spliced length = %ld\0A\00"
@str = [18 x i8] c"open file failed.\00"
@str.4 = [31 x i8] c"Usage: ls | ./splice_write out\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_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %9, i32 %14, i32 420)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %7
%21 = icmp eq i32 %0, 3
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = tail call i64 @atol(i8* %24)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %51
27: ; preds = %20, %22
%28 = phi i64 [ 1000000, %20 ], [ %25, %22 ]
br label %29
29: ; preds = %27, %43
%30 = phi i64 [ %44, %43 ], [ %28, %27 ]
%31 = load i32, i32* @STDIN_FILENO
%32 = tail call i64 @splice(i32 %31, i32* null, i32 %15, i32* null, i64 %30, i32 0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %43
37: ; preds = %29
%38 = icmp eq i64 %32, 0
br i1 %38, label %51, label %39
39: ; preds = %37
%40 = sub nsw i64 %30, %32
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %32)
br label %43
43: ; preds = %39, %34
%44 = phi i64 [ %30, %34 ], [ %40, %39 ]
%45 = icmp sgt i64 %44, 0
br i1 %45, label %29, label %46
46: ; preds = %43
%47 = icmp slt i64 %32, 0
%48 = tail call i32 @close(i32 %15)
br i1 %47, label %49, label %53
49: ; preds = %46
%50 = tail call i32 @exit(i32 -1)
unreachable
51: ; preds = %37, %22
%52 = tail call i32 @close(i32 %15)
br label %53
53: ; preds = %51, %46
ret i32 0
}