#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 /*<<< orphan*/ getchar();
int /*<<< orphan*/ ksock_close(int);
scalar_t__ ksock_connect(int, char *, int);
int /*<<< orphan*/ ksock_init();
int ksock_init_fd();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ usleep(int);
int main() {
  int i = 0;
  int c = 0;
  char ip[16] = "";
  int port = 0;
  int fds[8000] = {0};
  scanf("%s", ip);
  scanf("%d", &port);
  getchar();
  ksock_init();
  for (i = 0; i < 8000; ++i) {
    int fd = ksock_init_fd();
    if (0 == ksock_connect(fd, ip, port)) {
      printf("%d\n", i);
    } else {
      usleep(500 * 1000);
    }
    fds[i] = fd;
    usleep(10 * 1000);
  }
  getchar();
  for (i = 0; i < 8000; ++i) {
    ksock_close(fds[i]);
    usleep(10 * 1000);
  }
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7d40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x0,-0x24(%rbp)
lea    -0x7d30(%rbp),%rdi
xor    %esi,%esi
mov    $0x7d00,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rsi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   10a0 <scanf@plt>
lea    0xe10(%rip),%rdi
lea    -0x24(%rbp),%rsi
mov    $0x0,%al
call   10a0 <scanf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    $0x0,%al
call   1080 <ksock_init@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x1f40,-0x8(%rbp)
jge    1294 <main+0x104>
mov    $0x0,%al
call   1030 <ksock_init_fd@plt>
mov    %eax,-0x7d34(%rbp)
mov    -0x7d34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1090 <ksock_connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1261 <main+0xd1>
mov    -0x8(%rbp),%esi
lea    0xdaf(%rip),%rdi
call   1040 <printf@plt>
jmp    126b <main+0xdb>
mov    $0x7a120,%edi
call   10b0 <usleep@plt>
mov    -0x7d34(%rbp),%ecx
movslq -0x8(%rbp),%rax
mov    %ecx,-0x7d30(%rbp,%rax,4)
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1213 <main+0x83>
mov    $0x0,%al
call   1060 <getchar@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x1f40,-0x8(%rbp)
jge    12d7 <main+0x147>
movslq -0x8(%rbp),%rax
mov    -0x7d30(%rbp,%rax,4),%edi
call   1070 <ksock_close@plt>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12a2 <main+0x112>
mov    $0x0,%al
call   1060 <getchar@plt>
xor    %eax,%eax
add    $0x7d40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x7d28,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x7d00,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe3b(%rip),%rdi
lea    0x10(%rsp),%r15
mov    %r15,%rsi
xor    %eax,%eax
call   10a0 <scanf@plt>
lea    0xe28(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   10a0 <scanf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
xor    %eax,%eax
call   1080 <ksock_init@plt>
lea    0xe0a(%rip),%r14
xor    %ebx,%ebx
jmp    1225 <main+0x95>
mov    $0x7a120,%edi
call   10b0 <usleep@plt>
mov    %ebp,0x20(%rsp,%rbx,4)
mov    $0x2710,%edi
call   10b0 <usleep@plt>
add    $0x1,%rbx
cmp    $0x1f40,%rbx
je     124d <main+0xbd>
xor    %eax,%eax
call   1030 <ksock_init_fd@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edx
mov    %eax,%edi
mov    %r15,%rsi
call   1090 <ksock_connect@plt>
test   %rax,%rax
jne    1200 <main+0x70>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
jmp    120a <main+0x7a>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getchar@plt>
cs nopw 0x0(%rax,%rax,1)
mov    0x20(%rsp,%rbx,4),%edi
call   1070 <ksock_close@plt>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
add    $0x1,%rbx
cmp    $0x1f40,%rbx
jne    1260 <main+0xd0>
xor    %eax,%eax
call   1060 <getchar@plt>
xor    %eax,%eax
add    $0x7d28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca i32
%6 = alloca [8000 x i32]
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
%8 = bitcast [16 x i8]* %4 to i8*
store i32 0, i32* %5
%9 = bitcast [8000 x i32]* %6 to i8*
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%11 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%13 = call i32 (...) @getchar()
%14 = call i32 (...) @ksock_init()
store i32 0, i32* %2
br label %15
15: ; preds = %36, %0
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 8000
br i1 %17, label %18, label %39
18: ; preds = %15
%19 = call i32 (...) @ksock_init_fd()
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* %5
%23 = call i64 @ksock_connect(i32 %20, i8* %21, i32 %22)
%24 = icmp eq i64 0, %23
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* %2
%27 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %30
28: ; preds = %18
%29 = call i32 @usleep(i32 500000)
br label %30
30: ; preds = %28, %25
%31 = load i32, i32* %7
%32 = load i32, i32* %2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [8000 x i32], [8000 x i32]* %6, i64 0, i64 %33
store i32 %31, i32* %34
%35 = call i32 @usleep(i32 10000)
br label %36
36: ; preds = %30
%37 = load i32, i32* %2
%38 = add nsw i32 %37, 1
store i32 %38, i32* %2
br label %15
39: ; preds = %15
%40 = call i32 (...) @getchar()
store i32 0, i32* %2
br label %41
41: ; preds = %51, %39
%42 = load i32, i32* %2
%43 = icmp slt i32 %42, 8000
br i1 %43, label %44, label %54
44: ; preds = %41
%45 = load i32, i32* %2
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [8000 x i32], [8000 x i32]* %6, i64 0, i64 %46
%48 = load i32, i32* %47
%49 = call i32 @ksock_close(i32 %48)
%50 = call i32 @usleep(i32 10000)
br label %51
51: ; preds = %44
%52 = load i32, i32* %2
%53 = add nsw i32 %52, 1
store i32 %53, i32* %2
br label %41
54: ; preds = %41
%55 = call i32 (...) @getchar()
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [16 x i8]
%2 = alloca i32
%3 = alloca [8000 x i32]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%6 = bitcast [8000 x i32]* %3 to i8*
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = call i32 (...) @getchar()
%10 = call i32 (...) @ksock_init()
br label %11
11: ; preds = %0, %22
%12 = phi i64 [ 0, %0 ], [ %25, %22 ]
%13 = call i32 (...) @ksock_init_fd()
%14 = load i32, i32* %2
%15 = call i64 @ksock_connect(i32 %13, i8* %4, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = trunc i64 %12 to i32
%19 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %22
20: ; preds = %11
%21 = call i32 @usleep(i32 500000)
br label %22
22: ; preds = %20, %17
%23 = getelementptr inbounds [8000 x i32], [8000 x i32]* %3, i64 0, i64 %12
store i32 %13, i32* %23
%24 = call i32 @usleep(i32 10000)
%25 = add nuw nsw i64 %12, 1
%26 = icmp eq i64 %25, 8000
br i1 %26, label %27, label %11
27: ; preds = %22
%28 = call i32 (...) @getchar()
br label %29
29: ; preds = %27, %29
%30 = phi i64 [ 0, %27 ], [ %35, %29 ]
%31 = getelementptr inbounds [8000 x i32], [8000 x i32]* %3, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = call i32 @ksock_close(i32 %32)
%34 = call i32 @usleep(i32 10000)
%35 = add nuw nsw i64 %30, 1
%36 = icmp eq i64 %35, 8000
br i1 %36, label %37, label %29
37: ; preds = %29
%38 = call i32 (...) @getchar()
ret i32 0
}