#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 /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ qlen;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_DEFER_ACCEPT;
int /*<<< orphan*/ TCP_FASTOPEN;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
void server(const char *ip, const int port) {
  int portno = port;
  char buffer[2048];
  struct sockaddr_in serv_addr;
  int cfd;
  int sfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(portno);
  inet_pton(AF_INET, ip, &serv_addr.sin_addr);
  int qlen = 5;
  int err = setsockopt(sfd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen));
  if (err != 0) {
    printf(" fastopen setsockopt error: %s , len = %d\n", strerror(errno), err);
  }
  qlen = 1;
  err = setsockopt(sfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &qlen, sizeof(qlen));
  if (err != 0) {
    printf("accept filter error: %s , len = %d\n", strerror(errno), err);
  }
  bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  listen(sfd, 1);
  bzero(buffer, 2048);
  cfd = accept(sfd, NULL, 0);
  printf("accept new client %d\n", cfd);
  while (1) {
    int len = read(cfd, buffer, 2048);
    if (len)
      printf("tcp fast open: %s \n", buffer);
    else
      break;
  }
  sleep(2);
  close(cfd);
  sleep(2);
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x828(%rbp)
lea    -0x820(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x818(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x81c(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x820(%rbp),%rdx
call   1080 <inet_pton@plt>
movl   $0x5,-0x82c(%rbp)
mov    -0x828(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
lea    -0x82c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x830(%rbp)
cmpl   $0x0,-0x830(%rbp)
je     12ba <server+0xea>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    -0x830(%rbp),%edx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x82c(%rbp)
mov    -0x828(%rbp),%edi
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
mov    0x2cf6(%rip),%rax
mov    (%rax),%edx
lea    -0x82c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x830(%rbp)
cmpl   $0x0,-0x830(%rbp)
je     1326 <server+0x156>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    -0x830(%rbp),%edx
lea    0xd0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x828(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    -0x828(%rbp),%edi
mov    $0x1,%esi
call   1090 <listen@plt>
lea    -0x810(%rbp),%rdi
mov    $0x800,%esi
call   10b0 <bzero@plt>
mov    -0x828(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,-0x824(%rbp)
mov    -0x824(%rbp),%esi
lea    0xccd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x824(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1070 <read@plt>
mov    %eax,-0x834(%rbp)
cmpl   $0x0,-0x834(%rbp)
je     13cd <server+0x1fd>
lea    -0x810(%rbp),%rsi
lea    0xca4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13d2 <server+0x202>
jmp    13d7 <server+0x207>
jmp    1389 <server+0x1b9>
mov    $0x2,%edi
call   10e0 <sleep@plt>
mov    -0x824(%rbp),%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x820,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2dfd(%rip),%r12
mov    (%r12),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <inet_pton@plt>
movl   $0x5,0xc(%rsp)
mov    0x2d9c(%rip),%r14
mov    (%r14),%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
je     1289 <server+0xb9>
mov    %eax,%ebp
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd83(%rip),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x1,0xc(%rsp)
mov    (%r14),%esi
mov    0x2d35(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
je     12d6 <server+0x106>
mov    %eax,%ebp
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd61(%rip),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1090 <listen@plt>
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x800,%esi
call   10b0 <bzero@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,%ebp
lea    0xd36(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x800,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1370 <server+0x1a0>
lea    0xd29(%rip),%r14
lea    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    1350 <server+0x180>
mov    $0x2,%edi
call   10e0 <sleep@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
add    $0x820,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_FASTOPEN = global i32 0
@.str = [43 x i8] c" fastopen setsockopt error: %s , len = %d\0A\00"
@errno = global i32 0
@TCP_DEFER_ACCEPT = global i32 0
@.str.1 = [36 x i8] c"accept filter error: %s , len = %d\0A\00"
@.str.2 = [22 x i8] c"accept new client %d\0A\00"
@.str.3 = [20 x i8] c"tcp fast open: %s \0A\00"
define void @server(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2048 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%13 = load i32, i32* %4
store i32 %13, i32* %5
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %9
%17 = bitcast %struct.sockaddr_in* %7 to i8*
%18 = call i32 @bzero(i8* %17, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* %5
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = load i8*, i8** %3
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%27 = call i32 @inet_pton(i32 %24, i8* %25, i32* %26)
store i32 5, i32* %10
%28 = load i32, i32* %9
%29 = load i32, i32* @IPPROTO_TCP
%30 = load i32, i32* @TCP_FASTOPEN
%31 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %10, i32 4)
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %2
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = load i32, i32* %11
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %36, i32 %37)
br label %39
39: ; preds = %34, %2
store i32 1, i32* %10
%40 = load i32, i32* %9
%41 = load i32, i32* @IPPROTO_TCP
%42 = load i32, i32* @TCP_DEFER_ACCEPT
%43 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i32* %10, i32 4)
store i32 %43, i32* %11
%44 = load i32, i32* %11
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %39
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = load i32, i32* %11
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %48, i32 %49)
br label %51
51: ; preds = %46, %39
%52 = load i32, i32* %9
%53 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%54 = call i32 @bind(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = load i32, i32* %9
%56 = call i32 @listen(i32 %55, i32 1)
%57 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%58 = call i32 @bzero(i8* %57, i32 2048)
%59 = load i32, i32* %9
%60 = call i32 @accept(i32 %59, i32* null, i32 0)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %51, %73
%64 = load i32, i32* %8
%65 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%66 = call i32 @read(i32 %64, i8* %65, i32 2048)
store i32 %66, i32* %12
%67 = load i32, i32* %12
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %63
%70 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %70)
br label %73
72: ; preds = %63
br label %74
73: ; preds = %69
br label %63
74: ; preds = %72
%75 = call i32 @sleep(i32 2)
%76 = load i32, i32* %8
%77 = call i32 @close(i32 %76)
%78 = call i32 @sleep(i32 2)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_FASTOPEN = global i32 0
@.str = [43 x i8] c" fastopen setsockopt error: %s , len = %d\0A\00"
@errno = global i32 0
@TCP_DEFER_ACCEPT = global i32 0
@.str.1 = [36 x i8] c"accept filter error: %s , len = %d\0A\00"
@.str.2 = [22 x i8] c"accept new client %d\0A\00"
@.str.3 = [20 x i8] c"tcp fast open: %s \0A\00"
define void @server(i8* %0, i32 %1) {
%3 = alloca [2048 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = call i32 @bzero(i8* %7, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%18 = call i32 @inet_pton(i32 %16, i8* %0, i32* %17)
%19 = bitcast i32* %5 to i8*
store i32 5, i32* %5
%20 = load i32, i32* @IPPROTO_TCP
%21 = load i32, i32* @TCP_FASTOPEN
%22 = call i32 @setsockopt(i32 %10, i32 %20, i32 %21, i32* %5, i32 4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %2
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %26, i32 %22)
br label %28
28: ; preds = %24, %2
store i32 1, i32* %5
%29 = load i32, i32* @IPPROTO_TCP
%30 = load i32, i32* @TCP_DEFER_ACCEPT
%31 = call i32 @setsockopt(i32 %10, i32 %29, i32 %30, i32* %5, i32 4)
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %35, i32 %31)
br label %37
37: ; preds = %33, %28
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @bind(i32 %10, %struct.sockaddr* %38, i32 12)
%40 = call i32 @listen(i32 %10, i32 1)
%41 = call i32 @bzero(i8* %6, i32 2048)
%42 = call i32 @accept(i32 %10, i32* null, i32 0)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = call i32 @read(i32 %42, i8* %6, i32 2048)
%45 = icmp eq i32 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %37, %46
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%48 = call i32 @read(i32 %42, i8* %6, i32 2048)
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %46
50: ; preds = %46, %37
%51 = call i32 @sleep(i32 2)
%52 = call i32 @close(i32 %42)
%53 = call i32 @sleep(i32 2)
ret void
}