#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*/ bufsize;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_SNDBUF;
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*/ htons(short);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, 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;
  while (1) {
    if (++index >= 100) {
      index = 0;
    }
    int port = base_port + index;
    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++;
    if (connections % 1000 == 999) {
      printf("connections: %d, fd: %d\n", connections, sock);
    }
    usleep(1 * 1000);
    bufsize = 5000;
    setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize));
    setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));
  }
  return 0;
sock_err:
  printf("connections: %d\n", connections);
  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     11ec <main+0x3c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x1,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2da9(%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    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
cmp    $0x64,%eax
jl     126a <main+0xba>
movl   $0x0,-0x4c(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
movswl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmp    $0xffffffff,%eax
jne    12ab <main+0xfb>
jmp    135c <main+0x1ac>
mov    -0x54(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ca <main+0x11a>
jmp    135c <main+0x1ac>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cmp    $0x3e7,%edx
jne    12fe <main+0x14e>
mov    -0x3c(%rbp),%esi
mov    -0x54(%rbp),%edx
lea    0xd1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
movl   $0x1388,-0x34(%rbp)
mov    -0x54(%rbp),%edi
mov    0x2ccf(%rip),%rax
mov    (%rax),%esi
mov    0x2cb6(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x54(%rbp),%edi
mov    0x2cab(%rip),%rax
mov    (%rax),%esi
mov    0x2c82(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
jmp    1251 <main+0xa1>
mov    -0x3c(%rbp),%esi
lea    0xcc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c44(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xcb8(%rip),%rdi
mov    $0x0,%al
call   1050 <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    1389 <main+0x1d9>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    %rsp,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2df6(%rip),%r14
mov    (%r14),%edi
mov    %edi,0x8(%rsp)
lea    0x4(%rsp),%rdx
mov    %rbx,%rsi
call   1060 <inet_pton@plt>
mov    %rbp,0x10(%rsp)
lea    0x1(%rbp),%eax
movswl %ax,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    (%r14),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
xor    %r13d,%r13d
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1348 <main+0x198>
mov    %eax,%ebx
mov    $0x1,%r15d
mov    $0x1,%r12d
xor    %r14d,%r14d
jmp    12f3 <main+0x143>
cs nopw 0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
movl   $0x1388,0xc(%rsp)
mov    0x2d7f(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
lea    0xc(%rsp),%r14
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x1(%r12),%ebp
cmp    $0x63,%r12d
mov    $0x0,%eax
cmovge %eax,%ebp
mov    0x10(%rsp),%rax
add    %ebp,%eax
movswl %ax,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
mov    0x2ce7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
add    $0x1,%r15d
mov    %ebp,%r12d
mov    %r13d,%r14d
cmp    $0xffffffff,%eax
je     1348 <main+0x198>
mov    %ebx,%edi
mov    %rsp,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     1345 <main+0x195>
mov    %r15d,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%eax
add    $0x3e6,%eax
lea    0x1(%r14),%r13d
cmp    %r14d,%eax
jne    1250 <main+0xa0>
lea    0xcdf(%rip),%rdi
mov    %r13d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1250 <main+0xa0>
mov    %r14d,%r13d
lea    0xcdd(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2c58(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xccf(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <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    0xc6d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <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
@SOCK_STREAM = global i32 0
@.str.1 = [25 x i8] c"connections: %d, fd: %d\0A\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
@.str.2 = [17 x i8] c"connections: %d\0A\00"
@.str.3 = [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
br label %40
40: ; preds = %25, %74
%41 = load i32, i32* %14
%42 = add nsw i32 %41, 1
store i32 %42, i32* %14
%43 = icmp sge i32 %42, 100
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 0, i32* %14
br label %45
45: ; preds = %44, %40
%46 = load i32, i32* %8
%47 = load i32, i32* %14
%48 = add nsw i32 %46, %47
store i32 %48, i32* %15
%49 = load i32, i32* %15
%50 = trunc i32 %49 to i16
%51 = call i32 @htons(i16 signext %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %51, i32* %52
%53 = load i32, i32* @AF_INET
%54 = load i32, i32* @SOCK_STREAM
%55 = call i32 @socket(i32 %53, i32 %54, i32 0)
store i32 %55, i32* %16
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %45
br label %84
58: ; preds = %45
%59 = load i32, i32* %16
%60 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%61 = call i32 @connect(i32 %59, %struct.sockaddr* %60, i32 12)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %64
63: ; preds = %58
br label %84
64: ; preds = %58
%65 = load i32, i32* %12
%66 = add nsw i32 %65, 1
store i32 %66, i32* %12
%67 = load i32, i32* %12
%68 = srem i32 %67, 1000
%69 = icmp eq i32 %68, 999
br i1 %69, label %70, label %74
70: ; preds = %64
%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.1, i64 0, i64 0), i32 %71, i32 %72)
br label %74
74: ; preds = %70, %64
%75 = call i32 @usleep(i32 1000)
store i32 5000, i32* %10
%76 = load i32, i32* %16
%77 = load i32, i32* @SOL_SOCKET
%78 = load i32, i32* @SO_SNDBUF
%79 = call i32 @setsockopt(i32 %76, i32 %77, i32 %78, i32* %10, i32 4)
%80 = load i32, i32* %16
%81 = load i32, i32* @SOL_SOCKET
%82 = load i32, i32* @SO_RCVBUF
%83 = call i32 @setsockopt(i32 %80, i32 %81, i32 %82, i32* %10, i32 4)
br label %40
84: ; preds = %63, %57
%85 = load i32, i32* %12
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %85)
%87 = load i32, i32* @errno
%88 = call i8* @strerror(i32 %87)
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %88)
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
@SOCK_STREAM = global i32 0
@.str.1 = [25 x i8] c"connections: %d, fd: %d\0A\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
@.str.2 = [17 x i8] c"connections: %d\0A\00"
@.str.3 = [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 = alloca i32
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = bitcast i32* %4 to i8*
%18 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%22 = call i32 @inet_pton(i32 %19, i8* %13, i32* %21)
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = trunc i32 %16 to i16
%25 = add i16 %24, 1
%26 = call i32 @htons(i16 signext %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %62, label %32
32: ; preds = %10, %44
%33 = phi i32 [ %60, %44 ], [ %30, %10 ]
%34 = phi i32 [ %54, %44 ], [ 1, %10 ]
%35 = phi i32 [ %39, %44 ], [ 0, %10 ]
%36 = call i32 @connect(i32 %33, %struct.sockaddr* %23, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %62, label %38
38: ; preds = %32
%39 = add nuw nsw i32 %35, 1
%40 = urem i32 %39, 1000
%41 = icmp eq i32 %40, 999
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %39, i32 %33)
br label %44
44: ; preds = %38, %42
%45 = call i32 @usleep(i32 1000)
store i32 5000, i32* %4
%46 = load i32, i32* @SOL_SOCKET
%47 = load i32, i32* @SO_SNDBUF
%48 = call i32 @setsockopt(i32 %33, i32 %46, i32 %47, i32* %4, i32 4)
%49 = load i32, i32* @SOL_SOCKET
%50 = load i32, i32* @SO_RCVBUF
%51 = call i32 @setsockopt(i32 %33, i32 %49, i32 %50, i32* %4, i32 4)
%52 = add nsw i32 %34, 1
%53 = icmp sgt i32 %34, 98
%54 = select i1 %53, i32 0, i32 %52
%55 = add nsw i32 %54, %16
%56 = trunc i32 %55 to i16
%57 = call i32 @htons(i16 signext %56)
store i32 %57, i32* %27
%58 = load i32, i32* @AF_INET
%59 = load i32, i32* @SOCK_STREAM
%60 = call i32 @socket(i32 %58, i32 %59, i32 0)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %32
62: ; preds = %32, %44, %10
%63 = phi i32 [ 0, %10 ], [ %39, %44 ], [ %35, %32 ]
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %63)
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %66)
ret i32 0
}