#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 atoi(char *);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ x_close(int);
int x_create_tcp_client(char *, int);
int main(int argc, char *argv[]) {
  int connections = 1;
  char *ip = NULL;
  int port = -1;
  int *conns = NULL;
  int i = 0;
  int j = 0;
  if (argc < 3) {
    printf("usage: %s <ip> <port> [connections]\n", argv[0]);
    return -1;
  }
  ip = argv[1];
  port = atoi(argv[2]);
  if (argc == 4) {
    connections = atoi(argv[3]);
  }
  conns = (int *)malloc(sizeof(int) * connections);
  if (conns == NULL) {
    printf("malloc failed");
    return -1;
  }
  for (i = 0; i < connections; i++) {
    conns[i] = x_create_tcp_client(ip, port);
    if (conns[i] == -1) {
      perror("create tcp client error");
      break;
    }
    printf("create client fd: %d\n", conns[i]);
  }
  printf("create %d connect\n", i + 1);
  pause();
  for (j = 0; j < i; j++) {
    x_close(conns[j]);
  }
  free(conns);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ed <main+0x6d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132d <main+0x1ad>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    1223 <main+0xa3>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    125b <main+0xdb>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132d <main+0x1ad>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12d1 <main+0x151>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <x_create_tcp_client@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
jne    12aa <main+0x12a>
lea    0xd93(%rip),%rdi
call   1090 <perror@plt>
jmp    12d1 <main+0x151>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1262 <main+0xe2>
mov    -0x34(%rbp),%esi
add    $0x1,%esi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <pause@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    131d <main+0x19d>
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1060 <x_close@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12f3 <main+0x173>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
cmp    $0x2,%edi
jg     11b0 <main+0x30>
mov    (%r12),%rsi
lea    0xe62(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%r14d
jmp    128a <main+0x10a>
mov    %edi,%ebx
mov    0x8(%r12),%r14
mov    0x10(%r12),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r13d
mov    $0x1,%ebp
cmp    $0x4,%ebx
jne    11da <main+0x5a>
mov    0x18(%r12),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
lea    0x0(,%rbp,4),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1232 <main+0xb2>
mov    %rax,%r15
mov    %ebp,%r12d
test   %r12d,%r12d
jle    1248 <main+0xc8>
lea    0xe4e(%rip),%rbx
xor    %ebp,%ebp
nop
mov    %r14,%rdi
mov    %r13d,%esi
call   1050 <x_create_tcp_client@plt>
mov    %eax,(%r15,%rbp,4)
cmp    $0xffffffff,%eax
je     129c <main+0x11c>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1200 <main+0x80>
mov    %r12d,%ebp
jmp    124a <main+0xca>
lea    0xdec(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%r14d
jmp    128a <main+0x10a>
xor    %ebp,%ebp
lea    0x1(%rbp),%esi
lea    0xe0d(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <pause@plt>
test   %ebp,%ebp
je     1282 <main+0x102>
mov    %ebp,%ebp
xor    %ebx,%ebx
nopl   (%rax)
mov    (%r15,%rbx,4),%edi
call   1060 <x_close@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1270 <main+0xf0>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd90(%rip),%rdi
call   1090 <perror@plt>
jmp    124a <main+0xca>

LLVM IR (-O0):
@.str = [37 x i8] c"usage: %s <ip> <port> [connections]\0A\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@.str.2 = [24 x i8] c"create tcp client error\00"
@.str.3 = [22 x i8] c"create client fd: %d\0A\00"
@.str.4 = [19 x i8] c"create %d connect\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
store i8* null, i8** %7
store i32 -1, i32* %8
store i32* null, i32** %9
store i32 0, i32* %10
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %98
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %7
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %8
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 4
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %6
br label %34
34: ; preds = %29, %19
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = mul i64 4, %36
%38 = trunc i64 %37 to i32
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i32*
store i32* %40, i32** %9
%41 = load i32*, i32** %9
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %98
45: ; preds = %34
store i32 0, i32* %10
br label %46
46: ; preds = %73, %45
%47 = load i32, i32* %10
%48 = load i32, i32* %6
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %76
50: ; preds = %46
%51 = load i8*, i8** %7
%52 = load i32, i32* %8
%53 = call i32 @x_create_tcp_client(i8* %51, i32 %52)
%54 = load i32*, i32** %9
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
store i32 %53, i32* %57
%58 = load i32*, i32** %9
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %58, i64 %60
%62 = load i32, i32* %61
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %50
%65 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %76
66: ; preds = %50
%67 = load i32*, i32** %9
%68 = load i32, i32* %10
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i32, i32* %67, i64 %69
%71 = load i32, i32* %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %66
%74 = load i32, i32* %10
%75 = add nsw i32 %74, 1
store i32 %75, i32* %10
br label %46
76: ; preds = %64, %46
%77 = load i32, i32* %10
%78 = add nsw i32 %77, 1
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %78)
%80 = call i32 (...) @pause()
store i32 0, i32* %11
br label %81
81: ; preds = %92, %76
%82 = load i32, i32* %11
%83 = load i32, i32* %10
%84 = icmp slt i32 %82, %83
br i1 %84, label %85, label %95
85: ; preds = %81
%86 = load i32*, i32** %9
%87 = load i32, i32* %11
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i32, i32* %86, i64 %88
%90 = load i32, i32* %89
%91 = call i32 @x_close(i32 %90)
br label %92
92: ; preds = %85
%93 = load i32, i32* %11
%94 = add nsw i32 %93, 1
store i32 %94, i32* %11
br label %81
95: ; preds = %81
%96 = load i32*, i32** %9
%97 = call i32 @free(i32* %96)
store i32 0, i32* %3
br label %98
98: ; preds = %95, %43, %14
%99 = load i32, i32* %3
ret i32 %99
}

LLVM IR (-O2):
@.str = [37 x i8] c"usage: %s <ip> <port> [connections]\0A\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@.str.2 = [24 x i8] c"create tcp client error\00"
@.str.3 = [22 x i8] c"create client fd: %d\0A\00"
@.str.4 = [19 x i8] c"create %d connect\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %59
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp eq i32 %0, 4
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
br label %18
18: ; preds = %14, %7
%19 = phi i32 [ %17, %14 ], [ 1, %7 ]
%20 = shl i32 %19, 2
%21 = tail call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i32*
%23 = icmp eq i64 %21, 0
br i1 %23, label %28, label %24
24: ; preds = %18
%25 = icmp sgt i32 %19, 0
br i1 %25, label %26, label %42
26: ; preds = %24
%27 = zext i32 %19 to i64
br label %30
28: ; preds = %18
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %59
30: ; preds = %26, %38
%31 = phi i64 [ 0, %26 ], [ %40, %38 ]
%32 = tail call i32 @x_create_tcp_client(i8* %9, i32 %12)
%33 = getelementptr inbounds i32, i32* %22, i64 %31
store i32 %32, i32* %33
%34 = icmp eq i32 %32, -1
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = trunc i64 %31 to i32
%37 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %42
38: ; preds = %30
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%40 = add nuw nsw i64 %31, 1
%41 = icmp eq i64 %40, %27
br i1 %41, label %42, label %30
42: ; preds = %38, %24, %35
%43 = phi i32 [ %36, %35 ], [ 0, %24 ], [ %19, %38 ]
%44 = add nuw nsw i32 %43, 1
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = tail call i32 (...) @pause()
%47 = icmp eq i32 %43, 0
br i1 %47, label %57, label %48
48: ; preds = %42
%49 = zext i32 %43 to i64
br label %50
50: ; preds = %48, %50
%51 = phi i64 [ 0, %48 ], [ %55, %50 ]
%52 = getelementptr inbounds i32, i32* %22, i64 %51
%53 = load i32, i32* %52
%54 = tail call i32 @x_close(i32 %53)
%55 = add nuw nsw i64 %51, 1
%56 = icmp eq i64 %55, %49
br i1 %56, label %57, label %50
57: ; preds = %50, %42
%58 = tail call i32 @free(i32* %22)
br label %59
59: ; preds = %57, %28, %4
%60 = phi i32 [ -1, %4 ], [ -1, %28 ], [ 0, %57 ]
ret i32 %60
}