#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 *);
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ getchar();
int *malloc(int);
int openConnection(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int port, amount, i;
  char *hostname;
  int *socketArray;
  if (argc != 4) {
    fprintf(stderr, "%s: invalid parameters\n", argv[0]);
    return 1;
  }
  amount = atoi(argv[1]);
  port = atoi(argv[3]);
  socketArray = malloc(amount * sizeof(int));
  for (i = 0; i < amount; i++) {
    printf("\rOpening connection %d", i);
    socketArray[i] = openConnection(argv[2], port);
    if (socketArray[i] < 0)
      break;
  }
  printf("\rAll %d connections are open", amount);
  getchar();
  for (; i;)
    if (close(socketArray[--i]) < 0)
      perror("close");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11c8 <main+0x48>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12be <main+0x13e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1264 <main+0xe4>
mov    -0x1c(%rbp),%esi
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x14(%rbp),%esi
call   1030 <openConnection@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jge    1251 <main+0xd1>
jmp    1264 <main+0xe4>
jmp    1256 <main+0xd6>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1202 <main+0x82>
mov    -0x18(%rbp),%esi
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12b7 <main+0x137>
mov    -0x30(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    12b2 <main+0x132>
lea    0xd9f(%rip),%rdi
call   1090 <perror@plt>
jmp    127a <main+0xfa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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,%rbp
cmp    $0x4,%edi
jne    1270 <main+0xf0>
mov    0x8(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    %eax,%r12d
mov    0x18(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r13d
lea    0x0(,%r14,4),%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    $0x0,%ebx
test   %r14d,%r14d
jle    120a <main+0x8a>
xor    %ebx,%ebx
lea    0xe43(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    0x10(%rbp),%rdi
mov    %r13d,%esi
call   1030 <openConnection@plt>
mov    %eax,(%r15,%rbx,4)
test   %eax,%eax
js     120a <main+0x8a>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11e0 <main+0x60>
mov    %r12d,%ebx
lea    0xe1e(%rip),%rdi
mov    %r12d,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
test   %ebx,%ebx
mov    $0x0,%eax
je     1292 <main+0x112>
movslq %ebx,%rax
neg    %ebx
lea    (%r15,%rax,4),%r15
add    $0xfffffffffffffffc,%r15
xor    %ebp,%ebp
lea    0xe0d(%rip),%r14
jmp    1258 <main+0xd8>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbp
cmp    %ebp,%ebx
je     1290 <main+0x110>
mov    (%r15,%rbp,4),%edi
call   1050 <close@plt>
test   %rax,%rax
jns    1250 <main+0xd0>
mov    %r14,%rdi
call   1090 <perror@plt>
jmp    1250 <main+0xd0>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xd7c(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
jmp    1292 <main+0x112>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"%s: invalid parameters\0A\00"
@.str.1 = [23 x i8] c"\0DOpening connection %d\00"
@.str.2 = [29 x i8] c"\0DAll %d connections are open\00"
@.str.3 = [6 x i8] c"close\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 4
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %80
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %7
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 3
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %6
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 4
%31 = trunc i64 %30 to i32
%32 = call i32* @malloc(i32 %31)
store i32* %32, i32** %10
store i32 0, i32* %8
br label %33
33: ; preds = %57, %19
%34 = load i32, i32* %8
%35 = load i32, i32* %7
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %60
37: ; preds = %33
%38 = load i32, i32* %8
%39 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = load i32, i32* %6
%44 = call i32 @openConnection(i8* %42, i32 %43)
%45 = load i32*, i32** %10
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 %44, i32* %48
%49 = load i32*, i32** %10
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %37
br label %60
56: ; preds = %37
br label %57
57: ; preds = %56
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %33
60: ; preds = %55, %33
%61 = load i32, i32* %7
%62 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %61)
%63 = call i32 (...) @getchar()
br label %64
64: ; preds = %78, %60
%65 = load i32, i32* %8
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %79
67: ; preds = %64
%68 = load i32*, i32** %10
%69 = load i32, i32* %8
%70 = add nsw i32 %69, -1
store i32 %70, i32* %8
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i32, i32* %68, i64 %71
%73 = load i32, i32* %72
%74 = call i64 @close(i32 %73)
%75 = icmp slt i64 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %67
%77 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %78
78: ; preds = %76, %67
br label %64
79: ; preds = %64
store i32 0, i32* %3
br label %80
80: ; preds = %79, %13
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"%s: invalid parameters\0A\00"
@.str.1 = [23 x i8] c"\0DOpening connection %d\00"
@.str.2 = [29 x i8] c"\0DAll %d connections are open\00"
@.str.3 = [6 x i8] c"close\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %53
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 3
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = shl i32 %11, 2
%16 = tail call i32* @malloc(i32 %15)
%17 = icmp sgt i32 %11, 0
br i1 %17, label %18, label %34
18: ; preds = %8
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = zext i32 %11 to i64
br label %21
21: ; preds = %18, %29
%22 = phi i64 [ 0, %18 ], [ %30, %29 ]
%23 = trunc i64 %22 to i32
%24 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i8*, i8** %19
%26 = tail call i32 @openConnection(i8* %25, i32 %14)
%27 = getelementptr inbounds i32, i32* %16, i64 %22
store i32 %26, i32* %27
%28 = icmp slt i32 %26, 0
br i1 %28, label %32, label %29
29: ; preds = %21
%30 = add nuw nsw i64 %22, 1
%31 = icmp eq i64 %30, %20
br i1 %31, label %34, label %21
32: ; preds = %21
%33 = trunc i64 %22 to i32
br label %34
34: ; preds = %29, %32, %8
%35 = phi i32 [ 0, %8 ], [ %33, %32 ], [ %11, %29 ]
%36 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%37 = tail call i32 (...) @getchar()
%38 = icmp eq i32 %35, 0
br i1 %38, label %53, label %39
39: ; preds = %34
%40 = sext i32 %35 to i64
br label %41
41: ; preds = %39, %50
%42 = phi i64 [ %40, %39 ], [ %43, %50 ]
%43 = add nsw i64 %42, -1
%44 = getelementptr inbounds i32, i32* %16, i64 %43
%45 = load i32, i32* %44
%46 = tail call i64 @close(i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %41
%51 = trunc i64 %43 to i32
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %41
53: ; preds = %50, %34, %4
%54 = phi i32 [ 1, %4 ], [ 0, %34 ], [ 0, %50 ]
ret i32 %54
}