#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 */
scalar_t__ EAGAIN;
scalar_t__ bind_socket(int, int, int);
int create_socket();
int /*<<< orphan*/ destroy_socket(int);
int /*<<< orphan*/ disconnect_socket(int);
int /*<<< orphan*/ do_processing(int);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int wait_connection(int);
int main(int argc, char *argv[]) {
  int sockfd = create_socket();
  if (sockfd != -1) {
    if (bind_socket(sockfd, 9090, 10)) {
      int sockets[10] = {-1};
      int opened_sockets = 0;
      while (1) {
        int idx, max;
        if (opened_sockets < 10) {
          int new_sockfd = wait_connection(sockfd);
          if (new_sockfd != -1) {
            sockets[opened_sockets++] = new_sockfd;
          } else if (errno != EAGAIN) {
            perror("ERROR waiting");
          }
        }
        for (idx = 0, max = opened_sockets; idx < max; idx++) {
          if (sockets[idx] != -1) {
            if (!do_processing(sockets[idx])) {
              if (errno != EAGAIN) {
                printf("Disconnecting...\n");
                disconnect_socket(sockets[idx]);
                sockets[idx] = sockets[opened_sockets - 1];
                sockets[opened_sockets - 1] = -1;
                opened_sockets--;
              }
            }
          }
        }
      }
    } else {
      perror("ERROR binding");
    }
    disconnect_socket(sockfd);
  } else {
    perror("ERROR opening socket");
  }
  destroy_socket(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <create_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1335 <main+0x1a5>
mov    -0x14(%rbp),%edi
mov    $0x2382,%esi
mov    $0xa,%edx
call   1030 <bind_socket@plt>
cmp    $0x0,%rax
je     131c <main+0x18c>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
movl   $0xffffffff,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0xa,-0x44(%rbp)
jge    125c <main+0xcc>
mov    -0x14(%rbp),%edi
call   1080 <wait_connection@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
je     122c <main+0x9c>
mov    -0x50(%rbp),%ecx
mov    -0x44(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x44(%rbp)
cltq
mov    %ecx,-0x40(%rbp,%rax,4)
jmp    1257 <main+0xc7>
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    0x2d93(%rip),%rcx
cmp    (%rcx),%rax
je     1252 <main+0xc2>
lea    0xdb3(%rip),%rdi
call   10b0 <perror@plt>
jmp    1257 <main+0xc7>
jmp    125c <main+0xcc>
movl   $0x0,-0x48(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    1317 <main+0x187>
movslq -0x48(%rbp),%rax
cmpl   $0xffffffff,-0x40(%rbp,%rax,4)
je     1304 <main+0x174>
movslq -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
call   1090 <do_processing@plt>
cmp    $0x0,%eax
jne    12ff <main+0x16f>
mov    0x2d3f(%rip),%rax
mov    (%rax),%rax
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%rax
je     12fa <main+0x16a>
lea    0xd53(%rip),%rdi
call   1040 <printf@plt>
movslq -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
call   1050 <disconnect_socket@plt>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x40(%rbp,%rax,4),%ecx
movslq -0x48(%rbp),%rax
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
movl   $0xffffffff,-0x40(%rbp,%rax,4)
ff
mov    -0x44(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
jmp    12ff <main+0x16f>
jmp    1304 <main+0x174>
jmp    1309 <main+0x179>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1269 <main+0xd9>
jmp    11f4 <main+0x64>
lea    0xcfd(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <disconnect_socket@plt>
jmp    1341 <main+0x1b1>
lea    0xcf2(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <destroy_socket@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
xor    %eax,%eax
call   1090 <create_socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1298 <main+0x118>
mov    %ebx,%edi
mov    $0x2382,%esi
mov    $0xa,%edx
call   1030 <bind_socket@plt>
test   %rax,%rax
je     12a6 <main+0x126>
mov    %ebx,0xc(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
movq   $0x0,0x30(%rsp)
movl   $0xffffffff,0x10(%rsp)
ff
xor    %ebx,%ebx
mov    0x2dfb(%rip),%r13
mov    0x2de4(%rip),%r14
nopl   0x0(%rax)
cmp    $0x9,%ebx
jg     1220 <main+0xa0>
mov    0xc(%rsp),%edi
call   1070 <wait_connection@plt>
cmp    $0xffffffff,%eax
je     1213 <main+0x93>
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %eax,0x10(%rsp,%rcx,4)
test   %ebx,%ebx
jg     1220 <main+0xa0>
jmp    11f0 <main+0x70>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
jne    1283 <main+0x103>
test   %ebx,%ebx
jle    11f0 <main+0x70>
mov    %ebx,%r15d
xor    %r12d,%r12d
jmp    1239 <main+0xb9>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12
cmp    %r12,%r15
je     11f0 <main+0x70>
mov    0x10(%rsp,%r12,4),%ebp
cmp    $0xffffffff,%ebp
je     1230 <main+0xb0>
mov    %ebp,%edi
call   1080 <do_processing@plt>
test   %eax,%eax
jne    1230 <main+0xb0>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1230 <main+0xb0>
lea    0xdd3(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1050 <disconnect_socket@plt>
movslq %ebx,%rax
add    $0xffffffff,%ebx
mov    0xc(%rsp,%rax,4),%ecx
mov    %ecx,0x10(%rsp,%r12,4)
movl   $0xffffffff,0xc(%rsp,%rax,4)
ff
jmp    1230 <main+0xb0>
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
test   %ebx,%ebx
jg     1220 <main+0xa0>
jmp    11f0 <main+0x70>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
jmp    12b9 <main+0x139>
lea    0xd61(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1050 <disconnect_socket@plt>
mov    %ebx,%edi
call   1060 <destroy_socket@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [14 x i8] c"ERROR waiting\00"
@.str.1 = [18 x i8] c"Disconnecting...\0A\00"
@.str.2 = [14 x i8] c"ERROR binding\00"
@.str.3 = [21 x i8] c"ERROR opening socket\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [10 x 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
%12 = call i32 (...) @create_socket()
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %101
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i64 @bind_socket(i32 %16, i32 9090, i32 10)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %96
19: ; preds = %15
%20 = bitcast [10 x i32]* %7 to i8*
%21 = bitcast i8* %20 to <{ i32, [9 x i32] }>*
%22 = getelementptr inbounds <{ i32, [9 x i32] }>, <{ i32, [9 x i32] }>* %21, i32 0, i32 0
store i32 -1, i32* %22
store i32 0, i32* %8
br label %23
23: ; preds = %19, %95
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 10
br i1 %25, label %26, label %45
26: ; preds = %23
%27 = load i32, i32* %6
%28 = call i32 @wait_connection(i32 %27)
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = icmp ne i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i32, i32* %11
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
store i32 %34, i32* %8
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %35
store i32 %32, i32* %36
br label %44
37: ; preds = %26
%38 = load i64, i64* @errno
%39 = load i64, i64* @EAGAIN
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
br label %44
44: ; preds = %43, %31
br label %45
45: ; preds = %44, %23
store i32 0, i32* %9
%46 = load i32, i32* %8
store i32 %46, i32* %10
br label %47
47: ; preds = %92, %45
%48 = load i32, i32* %9
%49 = load i32, i32* %10
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %95
51: ; preds = %47
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %53
%55 = load i32, i32* %54
%56 = icmp ne i32 %55, -1
br i1 %56, label %57, label %91
57: ; preds = %51
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %59
%61 = load i32, i32* %60
%62 = call i32 @do_processing(i32 %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %90, label %64
64: ; preds = %57
%65 = load i64, i64* @errno
%66 = load i64, i64* @EAGAIN
%67 = icmp ne i64 %65, %66
br i1 %67, label %68, label %89
68: ; preds = %64
%69 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%70 = load i32, i32* %9
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %71
%73 = load i32, i32* %72
%74 = call i32 @disconnect_socket(i32 %73)
%75 = load i32, i32* %8
%76 = sub nsw i32 %75, 1
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %77
%79 = load i32, i32* %78
%80 = load i32, i32* %9
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %81
store i32 %79, i32* %82
%83 = load i32, i32* %8
%84 = sub nsw i32 %83, 1
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %85
store i32 -1, i32* %86
%87 = load i32, i32* %8
%88 = add nsw i32 %87, -1
store i32 %88, i32* %8
br label %89
89: ; preds = %68, %64
br label %90
90: ; preds = %89, %57
br label %91
91: ; preds = %90, %51
br label %92
92: ; preds = %91
%93 = load i32, i32* %9
%94 = add nsw i32 %93, 1
store i32 %94, i32* %9
br label %47
95: ; preds = %47
br label %23
96: ; preds = %15
%97 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %98
98: ; preds = %96
%99 = load i32, i32* %6
%100 = call i32 @disconnect_socket(i32 %99)
br label %103
101: ; preds = %2
%102 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %103
103: ; preds = %101, %98
%104 = load i32, i32* %6
%105 = call i32 @destroy_socket(i32 %104)
ret i32 0
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [14 x i8] c"ERROR waiting\00"
@.str.2 = [14 x i8] c"ERROR binding\00"
@.str.3 = [21 x i8] c"ERROR opening socket\00"
@str = [17 x i8] c"Disconnecting...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i32]
%4 = tail call i32 (...) @create_socket()
%5 = icmp eq i32 %4, -1
br i1 %5, label %63, label %6
6: ; preds = %2
%7 = tail call i64 @bind_socket(i32 %4, i32 9090, i32 10)
%8 = icmp eq i64 %7, 0
br i1 %8, label %60, label %9
9: ; preds = %6
%10 = bitcast [10 x i32]* %3 to i8*
%11 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 0
store i32 -1, i32* %11
br label %12
12: ; preds = %31, %9
%13 = phi i32 [ 0, %9 ], [ %32, %31 ]
%14 = icmp slt i32 %13, 10
br i1 %14, label %15, label %33
15: ; preds = %12
%16 = tail call i32 @wait_connection(i32 %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = add nsw i32 %13, 1
%20 = sext i32 %13 to i64
%21 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 %20
store i32 %16, i32* %21
br label %28
22: ; preds = %15
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %18, %26, %22
%29 = phi i32 [ %19, %18 ], [ %13, %26 ], [ %13, %22 ]
%30 = icmp sgt i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %56, %28
%32 = phi i32 [ %29, %28 ], [ %57, %56 ]
br label %12
33: ; preds = %12, %28
%34 = phi i32 [ %29, %28 ], [ %13, %12 ]
%35 = zext i32 %34 to i64
br label %36
36: ; preds = %33, %56
%37 = phi i64 [ 0, %33 ], [ %58, %56 ]
%38 = phi i32 [ %34, %33 ], [ %57, %56 ]
%39 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 %37
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, -1
br i1 %41, label %56, label %42
42: ; preds = %36
%43 = tail call i32 @do_processing(i32 %40)
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %42
%46 = load i64, i64* @errno
%47 = load i64, i64* @EAGAIN
%48 = icmp eq i64 %46, %47
br i1 %48, label %56, label %49
49: ; preds = %45
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%51 = tail call i32 @disconnect_socket(i32 %40)
%52 = add nsw i32 %38, -1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 %53
%55 = load i32, i32* %54
store i32 %55, i32* %39
store i32 -1, i32* %54
br label %56
56: ; preds = %36, %45, %49, %42
%57 = phi i32 [ %38, %42 ], [ %52, %49 ], [ %38, %45 ], [ %38, %36 ]
%58 = add nuw nsw i64 %37, 1
%59 = icmp eq i64 %58, %35
br i1 %59, label %31, label %36
60: ; preds = %6
%61 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%62 = tail call i32 @disconnect_socket(i32 %4)
br label %65
63: ; preds = %2
%64 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
%66 = tail call i32 @destroy_socket(i32 %4)
ret i32 0
}