#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 int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOL_TCP;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ TCP_NODELAY;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int create_and_bind(char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setnonblocking(int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  if (argc < 3) {
    perror("args with addr and port");
    exit(EXIT_FAILURE);
  }
  int listen_fd;
  if (-1 == (listen_fd = create_and_bind(argv[1], argv[2]))) {
    perror("bind error");
    exit(EXIT_FAILURE);
  }
  if (listen(listen_fd, SOMAXCONN) == -1) {
    perror("listen");
    exit(EXIT_FAILURE);
  }
  int serverfd = accept(listen_fd, NULL, NULL);
  if (serverfd == -1) {
    perror("accept!");
    return -1;
  }
  setnonblocking(serverfd);
  int opt = 1;
  setsockopt(serverfd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
  printf("accept success, wait 1 second close\n");
  sleep(1);
  close(listen_fd);
  close(serverfd);
  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   $0x3,-0x8(%rbp)
jge    11da <main+0x3a>
lea    0xe39(%rip),%rdi
call   1090 <perror@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1080 <create_and_bind@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    121b <main+0x7b>
lea    0xe10(%rip),%rdi
call   1090 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    124f <main+0xaf>
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10a0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1283 <main+0xe3>
lea    0xdb8(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <main+0x143>
mov    -0x18(%rbp),%edi
call   1070 <setnonblocking@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0xd75(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jle    126e <main+0xce>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rsi
call   1080 <create_and_bind@plt>
cmp    $0xffffffff,%eax
je     1277 <main+0xd7>
mov    %eax,%ebx
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     1280 <main+0xe0>
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
je     125a <main+0xba>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <setnonblocking@plt>
movl   $0x1,0xc(%rsp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc9(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
jmp    124e <main+0xae>
lea    0xd8b(%rip),%rdi
jmp    1287 <main+0xe7>
lea    0xd9a(%rip),%rdi
jmp    1287 <main+0xe7>
lea    0xd9c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"args with addr and port\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"bind error\00"
@SOMAXCONN = global i32 0
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [8 x i8] c"accept!\00"
@SOL_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.4 = [37 x i8] c"accept success, wait 1 second close\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @create_and_bind(i8* %18, i8* %21)
store i32 %22, i32* %6
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %28
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %15
%29 = load i32, i32* %6
%30 = load i32, i32* @SOMAXCONN
%31 = call i32 @listen(i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %28
%38 = load i32, i32* %6
%39 = call i32 @accept(i32 %38, i32* null, i32* null)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %57
44: ; preds = %37
%45 = load i32, i32* %7
%46 = call i32 @setnonblocking(i32 %45)
store i32 1, i32* %8
%47 = load i32, i32* %7
%48 = load i32, i32* @SOL_TCP
%49 = load i32, i32* @TCP_NODELAY
%50 = call i32 @setsockopt(i32 %47, i32 %48, i32 %49, i32* %8, i32 4)
%51 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%52 = call i32 @sleep(i32 1)
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %7
%56 = call i32 @close(i32 %55)
store i32 0, i32* %3
br label %57
57: ; preds = %44, %42
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [24 x i8] c"args with addr and port\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"bind error\00"
@SOMAXCONN = global i32 0
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [8 x i8] c"accept!\00"
@SOL_TCP = global i32 0
@TCP_NODELAY = global i32 0
@str = [36 x i8] c"accept success, wait 1 second close\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @create_and_bind(i8* %11, i8* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %9
%21 = load i32, i32* @SOMAXCONN
%22 = tail call i32 @listen(i32 %14, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %20
%29 = tail call i32 @accept(i32 %14, i32* null, i32* null)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %43
33: ; preds = %28
%34 = tail call i32 @setnonblocking(i32 %29)
%35 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%36 = load i32, i32* @SOL_TCP
%37 = load i32, i32* @TCP_NODELAY
%38 = call i32 @setsockopt(i32 %29, i32 %36, i32 %37, i32* %3, i32 4)
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%40 = call i32 @sleep(i32 1)
%41 = call i32 @close(i32 %14)
%42 = call i32 @close(i32 %29)
br label %43
43: ; preds = %33, %31
%44 = phi i32 [ -1, %31 ], [ 0, %33 ]
ret i32 %44
}