#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ reuse;
typedef int /*<<< orphan*/ client;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ assert(int);
int atoi(char *);
char *basename(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int listen(int, int);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int splice(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/ *, int,
           int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc <= 2) {
    printf("usage: %s ip port\n", basename(argv[0]));
    return 1;
  }
  const char *ip = argv[1];
  int port = atoi(argv[2]);
  struct sockaddr_in address;
  bzero(&address, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  inet_pton(AF_INET, ip, &address.sin_addr);
  int sock = socket(PF_INET, SOCK_STREAM, 0);
  assert(sock >= 0);
  int reuse = 1;
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  int ret = bind(sock, (struct sockaddr *)&address, sizeof(address));
  assert(ret != -1);
  ret = listen(sock, 5);
  assert(ret != -1);
  int i = 0;
  while (1) {
    int connfd;
    struct sockaddr_in client;
    socklen_t client_addrlength = sizeof(client);
    connfd = accept(sock, (struct sockaddr *)&client, &client_addrlength);
    if (connfd < 0) {
      printf("errno is: %s,%d\n", strerror(errno), i++);
    } else {
      int pipefd[2];
      ret = pipe(pipefd);
      assert(ret != -1);
      ret = splice(connfd, NULL, pipefd[1], NULL, 32768, 0);
      assert(ret != -1);
      ret = splice(pipefd[0], NULL, connfd, NULL, 32768, 0);
      assert(ret != -1);
      close(connfd);
    }
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jg     1248 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <basename@plt>
mov    %rax,%rsi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x60,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10b0 <inet_pton@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
mov    0x2cd2(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x2c(%rbp),%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
movl   $0x0,-0x38(%rbp)
movl   $0xc,-0x4c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x4c(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1397 <main+0x197>
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %rax,%rsi
mov    -0x38(%rbp),%edx
mov    %edx,%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
lea    0xc88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    141a <main+0x21a>
lea    -0x54(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x3c(%rbp),%edi
mov    -0x50(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x8000,%r8d
xor    %r9d,%r9d
mov    %rcx,%rsi
call   1030 <splice@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x54(%rbp),%edi
mov    -0x3c(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x8000,%r8d
xor    %r9d,%r9d
mov    %rcx,%rsi
call   1030 <splice@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
jmp    1344 <main+0x144>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0x2,%edi
jg     1240 <main+0x40>
mov    (%rsi),%rdi
call   10a0 <basename@plt>
lea    0xdde(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsi),%r15
mov    0x10(%rsi),%rdi
call   1100 <atoi@plt>
mov    %eax,%ebp
lea    0x18(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d78(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x20(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x1c(%rsp)
mov    (%rbx),%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   10b0 <inet_pton@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%r13d
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1090 <assert@plt>
movl   $0x1,0xc(%rsp)
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
mov    0x2cf2(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1090 <assert@plt>
mov    %r13d,%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1090 <assert@plt>
lea    0x28(%rsp),%r14
lea    0x8(%rsp),%r15
xor    %ebp,%ebp
jmp    1356 <main+0x156>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0x1(%rbp),%ebx
lea    0xccb(%rip),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%ebp
movl   $0xc,0x8(%rsp)
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10f0 <accept@plt>
test   %eax,%eax
js     1330 <main+0x130>
mov    %eax,%r12d
lea    0x10(%rsp),%rdi
call   1080 <pipe@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1090 <assert@plt>
mov    0x14(%rsp),%edx
mov    %r12d,%edi
xor    %esi,%esi
xor    %ecx,%ecx
mov    $0x8000,%r8d
xor    %r9d,%r9d
call   1030 <splice@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1090 <assert@plt>
mov    0x10(%rsp),%edi
xor    %esi,%esi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    $0x8000,%r8d
xor    %r9d,%r9d
call   1030 <splice@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1090 <assert@plt>
mov    %r12d,%edi
call   1070 <close@plt>
jmp    1356 <main+0x156>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [19 x i8] c"usage: %s ip port\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [17 x i8] c"errno is: %s,%d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.sockaddr_in
%15 = alloca i32
%16 = alloca [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp sle i32 %17, 2
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i8* @basename(i8* %22)
%24 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %23)
ret i32 1
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %7
%33 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* %7
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* @AF_INET
%40 = load i8*, i8** %6
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%42 = call i32 @inet_pton(i32 %39, i8* %40, i32* %41)
%43 = load i32, i32* @PF_INET
%44 = load i32, i32* @SOCK_STREAM
%45 = call i32 @socket(i32 %43, i32 %44, i32 0)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp sge i32 %46, 0
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
store i32 1, i32* %10
%50 = load i32, i32* %9
%51 = load i32, i32* @SOL_SOCKET
%52 = load i32, i32* @SO_REUSEADDR
%53 = call i32 @setsockopt(i32 %50, i32 %51, i32 %52, i32* %10, i32 4)
%54 = load i32, i32* %9
%55 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%56 = call i32 @bind(i32 %54, %struct.sockaddr* %55, i32 12)
store i32 %56, i32* %11
%57 = load i32, i32* %11
%58 = icmp ne i32 %57, -1
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = load i32, i32* %9
%62 = call i32 @listen(i32 %61, i32 5)
store i32 %62, i32* %11
%63 = load i32, i32* %11
%64 = icmp ne i32 %63, -1
%65 = zext i1 %64 to i32
%66 = call i32 @assert(i32 %65)
store i32 0, i32* %12
br label %67
67: ; preds = %25, %104
store i32 12, i32* %15
%68 = load i32, i32* %9
%69 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%70 = call i32 @accept(i32 %68, %struct.sockaddr* %69, i32* %15)
store i32 %70, i32* %13
%71 = load i32, i32* %13
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %79
73: ; preds = %67
%74 = load i32, i32* @errno
%75 = call i8* @strerror(i32 %74)
%76 = load i32, i32* %12
%77 = add nsw i32 %76, 1
store i32 %77, i32* %12
%78 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %75, i32 %76)
br label %104
79: ; preds = %67
%80 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%81 = call i32 @pipe(i32* %80)
store i32 %81, i32* %11
%82 = load i32, i32* %11
%83 = icmp ne i32 %82, -1
%84 = zext i1 %83 to i32
%85 = call i32 @assert(i32 %84)
%86 = load i32, i32* %13
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 1
%88 = load i32, i32* %87
%89 = call i32 @splice(i32 %86, i32* null, i32 %88, i32* null, i32 32768, i32 0)
store i32 %89, i32* %11
%90 = load i32, i32* %11
%91 = icmp ne i32 %90, -1
%92 = zext i1 %91 to i32
%93 = call i32 @assert(i32 %92)
%94 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%95 = load i32, i32* %94
%96 = load i32, i32* %13
%97 = call i32 @splice(i32 %95, i32* null, i32 %96, i32* null, i32 32768, i32 0)
store i32 %97, i32* %11
%98 = load i32, i32* %11
%99 = icmp ne i32 %98, -1
%100 = zext i1 %99 to i32
%101 = call i32 @assert(i32 %100)
%102 = load i32, i32* %13
%103 = call i32 @close(i32 %102)
br label %104
104: ; preds = %79, %73
br label %67
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [19 x i8] c"usage: %s ip port\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [17 x i8] c"errno is: %s,%d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = icmp slt i32 %0, 3
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i8* @basename(i8* %10)
%12 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %11)
ret i32 1
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = bitcast %struct.sockaddr_in* %3 to i8*
%20 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 %18)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%27 = call i32 @inet_pton(i32 %25, i8* %15, i32* %26)
%28 = load i32, i32* @PF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
%31 = xor i32 %30, -1
%32 = lshr i32 %31, 31
%33 = call i32 @assert(i32 %32)
%34 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_REUSEADDR
%37 = call i32 @setsockopt(i32 %30, i32 %35, i32 %36, i32* %4, i32 4)
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i32 @bind(i32 %30, %struct.sockaddr* %38, i32 12)
%40 = icmp ne i32 %39, -1
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = call i32 @listen(i32 %30, i32 5)
%44 = icmp ne i32 %43, -1
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = bitcast %struct.sockaddr_in* %5 to i8*
%48 = bitcast i32* %6 to i8*
%49 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%50 = bitcast [2 x i32]* %7 to i8*
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
br label %53
53: ; preds = %78, %13
%54 = phi i32 [ 0, %13 ], [ %79, %78 ]
store i32 12, i32* %6
%55 = call i32 @accept(i32 %30, %struct.sockaddr* %49, i32* %6)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %53
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = add nsw i32 %54, 1
%61 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %59, i32 %54)
br label %78
62: ; preds = %53
%63 = call i32 @pipe(i32* %51)
%64 = icmp ne i32 %63, -1
%65 = zext i1 %64 to i32
%66 = call i32 @assert(i32 %65)
%67 = load i32, i32* %52
%68 = call i32 @splice(i32 %55, i32* null, i32 %67, i32* null, i32 32768, i32 0)
%69 = icmp ne i32 %68, -1
%70 = zext i1 %69 to i32
%71 = call i32 @assert(i32 %70)
%72 = load i32, i32* %51
%73 = call i32 @splice(i32 %72, i32* null, i32 %55, i32* null, i32 32768, i32 0)
%74 = icmp ne i32 %73, -1
%75 = zext i1 %74 to i32
%76 = call i32 @assert(i32 %75)
%77 = call i32 @close(i32 %55)
br label %78
78: ; preds = %62, %57
%79 = phi i32 [ %60, %57 ], [ %54, %62 ]
br label %53
}