#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setlimit(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  if (argc <= 2) {
    printf("Usage: %s ip port\n", argv[0]);
    exit(0);
  }
  struct sockaddr_in addr;
  const char *ip = argv[1];
  int base_port = atoi(argv[2]);
  int opt = 1;
  int bufsize;
  socklen_t optlen;
  int connections = 0;
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  inet_pton(AF_INET, ip, &addr.sin_addr);
  char tmp_data[10];
  int index = 0;
  setlimit(100000);
  while (1) {
    if (++index >= 10) {
      index = 0;
    }
    int port = base_port + index;
    printf("connect to %s:%d\n", ip, port);
    addr.sin_port = htons((short)port);
    int sock;
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      goto sock_err;
    }
    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
      goto sock_err;
    }
    connections++;
    printf("connections: %d, fd: %d\n", connections, sock);
    if (connections % 10000 == 9999) {
      printf("press Enter to continue: ");
      getchar();
    }
    usleep(1 * 1000);
  }
  return 0;
sock_err:
  printf("error: %s\n", strerror(errno));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jg     11fc <main+0x3c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x1,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rdx
add    $0x4,%rdx
call   1060 <inet_pton@plt>
movl   $0x0,-0x4c(%rbp)
mov    $0x186a0,%edi
call   1070 <setlimit@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
cmp    $0xa,%eax
jl     1284 <main+0xc4>
movl   $0x0,-0x4c(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x50(%rbp),%edx
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%eax
movswl %ax,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmp    $0xffffffff,%eax
jne    12da <main+0x11a>
jmp    1351 <main+0x191>
mov    -0x54(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12f9 <main+0x139>
jmp    1351 <main+0x191>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
mov    -0x54(%rbp),%edx
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3c(%rbp),%eax
mov    $0x2710,%ecx
cltd
idiv   %ecx
cmp    $0x270f,%edx
jne    1342 <main+0x182>
lea    0xd0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
jmp    126b <main+0xab>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xcef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jle    132e <main+0x16e>
mov    0x8(%rsi),%r13
mov    0x10(%rsi),%rdi
call   1090 <atoi@plt>
mov    %rax,0x10(%rsp)
mov    %rsp,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
lea    0x4(%rsp),%rdx
mov    %r13,%rsi
call   1060 <inet_pton@plt>
mov    $0x186a0,%edi
call   1070 <setlimit@plt>
mov    $0x270e,%r12d
mov    $0x1,%ebp
xor    %r14d,%r14d
jmp    1241 <main+0x81>
nopw   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
add    $0xffffffff,%r12d
add    $0x1,%ebp
cmp    $0x9,%r14d
lea    0x1(%r14),%eax
mov    %eax,%r14d
mov    $0x0,%eax
cmovge %eax,%r14d
mov    0x10(%rsp),%rax
lea    (%r14,%rax,1),%ebx
lea    0xdae(%rip),%rdi
mov    %r13,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movswl %bx,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12fe <main+0x13e>
mov    %eax,%r15d
mov    %eax,%edi
mov    %rsp,%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     12fe <main+0x13e>
mov    %ebp,%eax
mov    $0xd1b71759,%ecx
imul   %rcx,%rax
shr    $0x2d,%rax
imul   $0x2710,%eax,%ebx
add    %r12d,%ebx
lea    0xd55(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jne    1230 <main+0x70>
lea    0xd53(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
jmp    1230 <main+0x70>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd45(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsi),%rsi
lea    0xcc8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [19 x i8] c"Usage: %s ip port\0A\00"
@AF_INET = global i32 0
@.str.1 = [18 x i8] c"connect to %s:%d\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [25 x i8] c"connections: %d, fd: %d\0A\00"
@.str.3 = [26 x i8] c"press Enter to continue: \00"
@.str.4 = [11 x i8] c"error: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [10 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp sle i32 %17, 2
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %7
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %8
store i32 1, i32* %9
store i32 0, i32* %12
%33 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* @AF_INET
%37 = load i8*, i8** %7
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%39 = call i32 @inet_pton(i32 %36, i8* %37, i32* %38)
store i32 0, i32* %14
%40 = call i32 @setlimit(i32 100000)
br label %41
41: ; preds = %25, %80
%42 = load i32, i32* %14
%43 = add nsw i32 %42, 1
store i32 %43, i32* %14
%44 = icmp sge i32 %43, 10
br i1 %44, label %45, label %46
45: ; preds = %41
store i32 0, i32* %14
br label %46
46: ; preds = %45, %41
%47 = load i32, i32* %8
%48 = load i32, i32* %14
%49 = add nsw i32 %47, %48
store i32 %49, i32* %15
%50 = load i8*, i8** %7
%51 = load i32, i32* %15
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %50, i32 %51)
%53 = load i32, i32* %15
%54 = trunc i32 %53 to i16
%55 = call i32 @htons(i16 signext %54)
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %55, i32* %56
%57 = load i32, i32* @AF_INET
%58 = load i32, i32* @SOCK_STREAM
%59 = call i32 @socket(i32 %57, i32 %58, i32 0)
store i32 %59, i32* %16
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %46
br label %82
62: ; preds = %46
%63 = load i32, i32* %16
%64 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%65 = call i32 @connect(i32 %63, %struct.sockaddr* %64, i32 12)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %68
67: ; preds = %62
br label %82
68: ; preds = %62
%69 = load i32, i32* %12
%70 = add nsw i32 %69, 1
store i32 %70, i32* %12
%71 = load i32, i32* %12
%72 = load i32, i32* %16
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %71, i32 %72)
%74 = load i32, i32* %12
%75 = srem i32 %74, 10000
%76 = icmp eq i32 %75, 9999
br i1 %76, label %77, label %80
77: ; preds = %68
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%79 = call i32 (...) @getchar()
br label %80
80: ; preds = %77, %68
%81 = call i32 @usleep(i32 1000)
br label %41
82: ; preds = %67, %61
%83 = load i32, i32* @errno
%84 = call i8* @strerror(i32 %83)
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %84)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [19 x i8] c"Usage: %s ip port\0A\00"
@AF_INET = global i32 0
@.str.1 = [18 x i8] c"connect to %s:%d\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [25 x i8] c"connections: %d, fd: %d\0A\00"
@.str.3 = [26 x i8] c"press Enter to continue: \00"
@.str.4 = [11 x i8] c"error: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = bitcast %struct.sockaddr_in* %3 to i8*
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%20 = call i32 @inet_pton(i32 %17, i8* %12, i32* %19)
%21 = call i32 @setlimit(i32 100000)
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %24
24: ; preds = %49, %9
%25 = phi i32 [ 0, %9 ], [ %29, %49 ]
%26 = phi i32 [ 0, %9 ], [ %42, %49 ]
%27 = add nsw i32 %25, 1
%28 = icmp sgt i32 %25, 8
%29 = select i1 %28, i32 0, i32 %27
%30 = add nsw i32 %29, %15
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 %30)
%32 = trunc i32 %30 to i16
%33 = call i32 @htons(i16 signext %32)
store i32 %33, i32* %23
%34 = load i32, i32* @AF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %51, label %38
38: ; preds = %24
%39 = call i32 @connect(i32 %36, %struct.sockaddr* %22, i32 12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %51, label %41
41: ; preds = %38
%42 = add nuw nsw i32 %26, 1
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %42, i32 %36)
%44 = urem i32 %42, 10000
%45 = icmp eq i32 %44, 9999
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 (...) @getchar()
br label %49
49: ; preds = %41, %46
%50 = call i32 @usleep(i32 1000)
br label %24
51: ; preds = %24, %38
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %53)
ret i32 0
}