#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*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ addfd(int, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void start_conn(int epoll_fd, int num, const char *ip, int port) {
  int ret = 0;
  struct sockaddr_in address;
  bzero(&address, sizeof(address));
  address.sin_family = AF_INET;
  inet_pton(AF_INET, ip, &address.sin_addr);
  address.sin_port = htons(port);
  for (int i = 0; i < num; ++i) {
    sleep(1);
    int sockfd = socket(PF_INET, SOCK_STREAM, 0);
    printf("create 1 sock\n");
    if (sockfd < 0) {
      continue;
    }
    if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == 0) {
      printf("build connection %d\n", i);
      addfd(epoll_fd, sockfd);
    }
  }
}


x86 assembly (clang, -O0):
<start_conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1060 <inet_pton@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    127b <start_conn+0xfb>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x30(%rbp)
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x30(%rbp)
jge    1231 <start_conn+0xb1>
jmp    126d <start_conn+0xed>
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jne    1268 <start_conn+0xe8>
mov    -0x2c(%rbp),%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1030 <addfd@plt>
jmp    126d <start_conn+0xed>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e2 <start_conn+0x62>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_conn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %rdx,%rbp
mov    %esi,%ebx
mov    %edi,0x4(%rsp)
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
lea    0xc(%rsp),%rdx
mov    %rbp,%rsi
call   1070 <inet_pton@plt>
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
test   %ebx,%ebx
jle    1263 <start_conn+0xd3>
mov    0x2de2(%rip),%r14
mov    0x2de3(%rip),%r15
lea    0xe19(%rip),%r13
xor    %ebp,%ebp
jmp    1207 <start_conn+0x77>
add    $0x1,%ebp
cmp    %ebp,%ebx
je     1263 <start_conn+0xd3>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    (%r14),%edi
mov    (%r15),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%r12d
mov    %r13,%rdi
call   1040 <puts@plt>
test   %r12d,%r12d
js     1200 <start_conn+0x70>
mov    %r12d,%edi
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
jne    1200 <start_conn+0x70>
lea    0xdb4(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rsp),%edi
mov    %r12d,%esi
call   1030 <addfd@plt>
jmp    1200 <start_conn+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"create 1 sock\0A\00"
@.str.1 = [21 x i8] c"build connection %d\0A\00"
define void @start_conn(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
%13 = call i32 @bzero(%struct.sockaddr_in* %10, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i8*, i8** %7
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%19 = call i32 @inet_pton(i32 %16, i8* %17, i32* %18)
%20 = load i32, i32* %8
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %21, i32* %22
store i32 0, i32* %11
br label %23
23: ; preds = %48, %4
%24 = load i32, i32* %11
%25 = load i32, i32* %6
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %51
27: ; preds = %23
%28 = call i32 @sleep(i32 1)
%29 = load i32, i32* @PF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %12
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %12
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %27
br label %48
36: ; preds = %27
%37 = load i32, i32* %12
%38 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%39 = call i64 @connect(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %36
%42 = load i32, i32* %11
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %5
%45 = load i32, i32* %12
%46 = call i32 @addfd(i32 %44, i32 %45)
br label %47
47: ; preds = %41, %36
br label %48
48: ; preds = %47, %35
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %23
51: ; preds = %23
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"build connection %d\0A\00"
@str = [14 x i8] c"create 1 sock\00"
define void @start_conn(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
%11 = call i32 @inet_pton(i32 %8, i8* %2, i32* %10)
%12 = call i32 @htons(i32 %3)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %12, i32* %13
%14 = icmp sgt i32 %1, 0
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %18
17: ; preds = %32, %4
ret void
18: ; preds = %15, %32
%19 = phi i32 [ 0, %15 ], [ %33, %32 ]
%20 = call i32 @sleep(i32 1)
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%25 = icmp slt i32 %23, 0
br i1 %25, label %32, label %26
26: ; preds = %18
%27 = call i64 @connect(i32 %23, %struct.sockaddr* %16, i32 12)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%31 = call i32 @addfd(i32 %0, i32 %23)
br label %32
32: ; preds = %26, %29, %18
%33 = add nuw nsw i32 %19, 1
%34 = icmp eq i32 %33, %1
br i1 %34, label %17, label %18
}