#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);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
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;
  int i;
  struct sockaddr_in address;
  memset(&address, 0, sizeof(address));
  address.sin_family = AF_INET;
  inet_pton(AF_INET, ip, &address.sin_addr);
  address.sin_port = htons(port);
  for (i = 0; i < num; ++i) {
    sleep(1);
    int sockfd = socket(PF_INET, SOCK_STREAM, 0);
    printf("create 1 sock fd=%d\n", sockfd);
    if (sockfd < 0) {
      continue;
    }
    if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == 0) {
      printf("build connection %d\n", i);
      addfd(epoll_fd, sockfd);
    } else {
      printf("build connection %d\n", i);
    }
  }
}


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
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1070 <inet_pton@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1290 <start_conn+0x110>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xde0(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x2c(%rbp)
jge    1234 <start_conn+0xb4>
jmp    1282 <start_conn+0x102>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jne    126e <start_conn+0xee>
mov    -0x1c(%rbp),%esi
lea    0xdbc(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1030 <addfd@plt>
jmp    127d <start_conn+0xfd>
mov    -0x1c(%rbp),%esi
lea    0xd9d(%rip),%rdi
call   1050 <printf@plt>
jmp    1282 <start_conn+0x102>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e4 <start_conn+0x64>
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,%r14d
mov    %rdx,%r15
mov    %esi,%ebx
mov    %edi,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
lea    0xc(%rsp),%rdx
mov    %r15,%rsi
call   1070 <inet_pton@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
test   %ebx,%ebx
jle    125d <start_conn+0xdd>
mov    0x2df6(%rip),%r13
lea    0xe17(%rip),%r15
jmp    11f7 <start_conn+0x77>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    %ebp,%ebx
je     125d <start_conn+0xdd>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%r14d
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
test   %r14d,%r14d
js     11f0 <start_conn+0x70>
mov    %r14d,%edi
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %rax,%r12
lea    0xdd2(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
test   %r12,%r12
jne    11f0 <start_conn+0x70>
mov    0x4(%rsp),%edi
mov    %r14d,%esi
call   1030 <addfd@plt>
jmp    11f0 <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 = [21 x i8] c"create 1 sock fd=%d\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 i32
%11 = alloca %struct.sockaddr_in
%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 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, 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* %11, 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* %11, i32 0, i32 0
store i32 %21, i32* %22
store i32 0, i32* %10
br label %23
23: ; preds = %52, %4
%24 = load i32, i32* %10
%25 = load i32, i32* %6
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %55
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 = load i32, i32* %12
%33 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %12
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %27
br label %52
37: ; preds = %27
%38 = load i32, i32* %12
%39 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%40 = call i64 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i32, i32* %10
%44 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %5
%46 = load i32, i32* %12
%47 = call i32 @addfd(i32 %45, i32 %46)
br label %51
48: ; preds = %37
%49 = load i32, i32* %10
%50 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %48, %42
br label %52
52: ; preds = %51, %36
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %23
55: ; 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 = [21 x i8] c"create 1 sock fd=%d\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 %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, 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 %34
15: ; preds = %4
%16 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %17
17: ; preds = %15, %31
%18 = phi i32 [ 0, %15 ], [ %32, %31 ]
%19 = call i32 @sleep(i32 1)
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = icmp slt i32 %22, 0
br i1 %24, label %31, label %25
25: ; preds = %17
%26 = call i64 @connect(i32 %22, %struct.sockaddr* %16, i32 12)
%27 = icmp eq i64 %26, 0
%28 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br i1 %27, label %29, label %31
29: ; preds = %25
%30 = call i32 @addfd(i32 %0, i32 %22)
br label %31
31: ; preds = %25, %29, %17
%32 = add nuw nsw i32 %18, 1
%33 = icmp eq i32 %32, %1
br i1 %33, label %34, label %17
34: ; preds = %31, %4
ret void
}