#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 atoi(char *);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int x_accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ x_close(int);
int x_create_tcp_server(char *, int);
int main(int argc, char *argv[]) {
  int max_conns = 100000;
  int i = 0;
  int port = -1;
  int *conns = NULL;
  int fd = -1;
  int j = 0;
  if (argc < 2) {
    printf("usage: %s <port> [max_connect]\n", argv[0]);
    return -1;
  }
  port = atoi(argv[1]);
  if (argc == 3) {
    max_conns = atoi(argv[2]);
  }
  conns = (int *)malloc(sizeof(int) * max_conns);
  if (conns == NULL) {
    printf("malloc failed");
    return -1;
  }
  fd = x_create_tcp_server("0.0.0.0", port);
  if (fd == -1) {
    perror("create tcp server error");
    return -1;
  }
  i = 0;
  while (1) {
    conns[i] = x_accept(fd, NULL, NULL);
    if (conns[i] == -1) {
      perror("accept error");
      break;
    }
    i++;
    if (i >= max_conns) {
      break;
    }
  }
  for (j = 0; j < i; j++) {
    x_close(conns[j]);
  }
  printf("accept %d connect\n", i + 1);
  free(conns);
  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)
movl   $0x186a0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ec <main+0x6c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133c <main+0x1bc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    1216 <main+0x96>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    124e <main+0xce>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133c <main+0x1bc>
mov    -0x1c(%rbp),%esi
lea    0xdd6(%rip),%rdi
call   1070 <x_create_tcp_server@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1282 <main+0x102>
lea    0xdc5(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133c <main+0x1bc>
movl   $0x0,-0x18(%rbp)
mov    -0x2c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <x_accept@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
jne    12c8 <main+0x148>
lea    0xd90(%rip),%rdi
call   1090 <perror@plt>
jmp    12e7 <main+0x167>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     12e2 <main+0x162>
jmp    12e7 <main+0x167>
jmp    1289 <main+0x109>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1318 <main+0x198>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1050 <x_close@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12ee <main+0x16e>
mov    -0x18(%rbp),%esi
add    $0x1,%esi
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11a6 <main+0x26>
mov    (%rbx),%rsi
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1281 <main+0x101>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    $0x186a0,%r15d
cmp    $0x3,%ebp
jne    11cb <main+0x4b>
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r15d
lea    0x0(,%r15,4),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1273 <main+0xf3>
mov    %rax,%r12
lea    0xe43(%rip),%rdi
mov    %r14d,%esi
call   1070 <x_create_tcp_server@plt>
cmp    $0xffffffff,%eax
je     1291 <main+0x111>
mov    %eax,%ebp
cmp    $0x2,%r15d
mov    $0x1,%r14d
cmovge %r15d,%r14d
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <x_accept@plt>
mov    %eax,(%r12,%rbx,4)
cmp    $0xffffffff,%eax
je     129f <main+0x11f>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1210 <main+0x90>
mov    %r14d,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12,%rbp,4),%edi
call   1050 <x_close@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1240 <main+0xc0>
add    $0x1,%r14d
lea    0xdfe(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1286 <main+0x106>
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   1090 <perror@plt>
jmp    1281 <main+0x101>
lea    0xda8(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%r14d
test   %ebx,%ebx
jg     122d <main+0xad>
jmp    1252 <main+0xd2>

LLVM IR (-O0):
@.str = [32 x i8] c"usage: %s <port> [max_connect]\0A\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@.str.2 = [8 x i8] c"0.0.0.0\00"
@.str.3 = [24 x i8] c"create tcp server error\00"
@.str.4 = [13 x i8] c"accept error\00"
@.str.5 = [19 x i8] c"accept %d connect\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
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 100000, i32* %6
store i32 0, i32* %7
store i32 -1, i32* %8
store i32* null, i32** %9
store i32 -1, i32* %10
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %94
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %8
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 3
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %6
br label %31
31: ; preds = %26, %19
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = mul i64 4, %33
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i32*
store i32* %37, i32** %9
%38 = load i32*, i32** %9
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %94
42: ; preds = %31
%43 = load i32, i32* %8
%44 = call i32 @x_create_tcp_server(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %43)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %94
49: ; preds = %42
store i32 0, i32* %7
br label %50
50: ; preds = %49, %72
%51 = load i32, i32* %10
%52 = call i32 @x_accept(i32 %51, i32* null, i32* null)
%53 = load i32*, i32** %9
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %53, i64 %55
store i32 %52, i32* %56
%57 = load i32*, i32** %9
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
%61 = load i32, i32* %60
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %50
%64 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %73
65: ; preds = %50
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 1
store i32 %67, i32* %7
%68 = load i32, i32* %7
%69 = load i32, i32* %6
%70 = icmp sge i32 %68, %69
br i1 %70, label %71, label %72
71: ; preds = %65
br label %73
72: ; preds = %65
br label %50
73: ; preds = %71, %63
store i32 0, i32* %11
br label %74
74: ; preds = %85, %73
%75 = load i32, i32* %11
%76 = load i32, i32* %7
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %88
78: ; preds = %74
%79 = load i32*, i32** %9
%80 = load i32, i32* %11
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i32, i32* %79, i64 %81
%83 = load i32, i32* %82
%84 = call i32 @x_close(i32 %83)
br label %85
85: ; preds = %78
%86 = load i32, i32* %11
%87 = add nsw i32 %86, 1
store i32 %87, i32* %11
br label %74
88: ; preds = %74
%89 = load i32, i32* %7
%90 = add nsw i32 %89, 1
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %90)
%92 = load i32*, i32** %9
%93 = call i32 @free(i32* %92)
store i32 0, i32* %3
br label %94
94: ; preds = %88, %47, %40, %14
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str = [32 x i8] c"usage: %s <port> [max_connect]\0A\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@.str.2 = [8 x i8] c"0.0.0.0\00"
@.str.3 = [24 x i8] c"create tcp server error\00"
@.str.4 = [13 x i8] c"accept error\00"
@.str.5 = [19 x i8] c"accept %d connect\0A\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 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %59
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = icmp eq i32 %0, 3
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
br label %16
16: ; preds = %12, %7
%17 = phi i32 [ %15, %12 ], [ 100000, %7 ]
%18 = shl i32 %17, 2
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %59
24: ; preds = %16
%25 = tail call i32 @x_create_tcp_server(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%26 = icmp eq i32 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = call i32 @llvm.smax.i32(i32 %17, i32 1)
%29 = zext i32 %28 to i64
br label %32
30: ; preds = %24
%31 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %59
32: ; preds = %27, %37
%33 = phi i64 [ 0, %27 ], [ %38, %37 ]
%34 = tail call i32 @x_accept(i32 %25, i32* null, i32* null)
%35 = getelementptr inbounds i32, i32* %20, i64 %33
store i32 %34, i32* %35
%36 = icmp eq i32 %34, -1
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = add nuw nsw i64 %33, 1
%39 = icmp eq i64 %38, %29
br i1 %39, label %44, label %32
40: ; preds = %32
%41 = trunc i64 %33 to i32
%42 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%43 = icmp sgt i32 %41, 0
br i1 %43, label %44, label %54
44: ; preds = %37, %40
%45 = phi i32 [ %41, %40 ], [ %28, %37 ]
%46 = zext i32 %45 to i64
br label %47
47: ; preds = %44, %47
%48 = phi i64 [ 0, %44 ], [ %52, %47 ]
%49 = getelementptr inbounds i32, i32* %20, i64 %48
%50 = load i32, i32* %49
%51 = tail call i32 @x_close(i32 %50)
%52 = add nuw nsw i64 %48, 1
%53 = icmp eq i64 %52, %46
br i1 %53, label %54, label %47
54: ; preds = %47, %40
%55 = phi i32 [ %41, %40 ], [ %45, %47 ]
%56 = add nsw i32 %55, 1
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %56)
%58 = tail call i32 @free(i32* %20)
br label %59
59: ; preds = %54, %30, %22, %4
%60 = phi i32 [ -1, %4 ], [ -1, %22 ], [ -1, %30 ], [ 0, %54 ]
ret i32 %60
}