#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 dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
char *gai_strerror(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int createSocketInBatch(int socketArray[], int socketArraySize,
                        struct sockaddr_in *serverAddr) {
  int retValue = 0;
  if (socketArray != NULL && socketArraySize > 0) {
    for (int i = 0; i < socketArraySize; i++) {
      socketArray[i] = -1;
    }
    for (int i = 0; i < socketArraySize; i++) {
      int sock = socket(AF_INET, SOCK_STREAM, 0);
      if (sock >= 0) {
        int error =
            connect(sock, (struct sockaddr *)serverAddr, sizeof(*serverAddr));
        if (error >= 0) {
          socketArray[i] = sock;
        } else {
          fprintf(stderr, "connect: %d : %s\n", error, gai_strerror(error));
          close(sock);
        }
      }
    }
  }
  return retValue;
}


x86 assembly (clang, -O0):
<createSocketInBatch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     126d <createSocketInBatch+0x11d>
cmpl   $0x0,-0xc(%rbp)
jle    126d <createSocketInBatch+0x11d>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11af <createSocketInBatch+0x5f>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1186 <createSocketInBatch+0x36>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1268 <createSocketInBatch+0x118>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     1255 <createSocketInBatch+0x105>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1219 <createSocketInBatch+0xc9>
mov    -0x28(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1250 <createSocketInBatch+0x100>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%edx
mov    %rax,%rcx
lea    0xdbd(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
jmp    1255 <createSocketInBatch+0x105>
jmp    125a <createSocketInBatch+0x10a>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b6 <createSocketInBatch+0x66>
jmp    126d <createSocketInBatch+0x11d>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createSocketInBatch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1220 <createSocketInBatch+0xc0>
test   %esi,%esi
jle    1220 <createSocketInBatch+0xc0>
mov    %rdx,%r12
mov    %rdi,%r13
mov    %esi,%r15d
lea    0x0(,%r15,4),%rdx
mov    $0xff,%esi
call   1040 <memset@plt>
xor    %r14d,%r14d
mov    %r15,(%rsp)
jmp    11e7 <createSocketInBatch+0x87>
mov    %eax,%ebx
mov    %r12,%r15
mov    %r13,%r12
mov    0x2e29(%rip),%rax
mov    (%rax),%r13d
mov    %ebx,%edi
call   1030 <gai_strerror@plt>
mov    %r13d,%edi
mov    %r12,%r13
mov    %r15,%r12
mov    (%rsp),%r15
lea    0xe33(%rip),%rsi
mov    %ebx,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%r14
cmp    %r14,%r15
je     1220 <createSocketInBatch+0xc0>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11de <createSocketInBatch+0x7e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %eax,%eax
js     11a0 <createSocketInBatch+0x40>
mov    %ebp,0x0(%r13,%r14,4)
jmp    11de <createSocketInBatch+0x7e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"connect: %d : %s\0A\00"
define i32 @createSocketInBatch(i32* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %6
store i32 0, i32* %7
%12 = load i32*, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %68
14: ; preds = %3
%15 = load i32, i32* %5
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %68
17: ; preds = %14
store i32 0, i32* %8
br label %18
18: ; preds = %27, %17
%19 = load i32, i32* %8
%20 = load i32, i32* %5
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = load i32*, i32** %4
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
store i32 -1, i32* %26
br label %27
27: ; preds = %22
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %18
30: ; preds = %18
store i32 0, i32* %9
br label %31
31: ; preds = %64, %30
%32 = load i32, i32* %9
%33 = load i32, i32* %5
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %67
35: ; preds = %31
%36 = load i32, i32* @AF_INET
%37 = load i32, i32* @SOCK_STREAM
%38 = call i32 @socket(i32 %36, i32 %37, i32 0)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp sge i32 %39, 0
br i1 %40, label %41, label %63
41: ; preds = %35
%42 = load i32, i32* %10
%43 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%44 = bitcast %struct.sockaddr_in* %43 to %struct.sockaddr*
%45 = call i32 @connect(i32 %42, %struct.sockaddr* %44, i32 4)
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %41
%49 = load i32, i32* %10
%50 = load i32*, i32** %4
%51 = load i32, i32* %9
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
store i32 %49, i32* %53
br label %62
54: ; preds = %41
%55 = load i32, i32* @stderr
%56 = load i32, i32* %11
%57 = load i32, i32* %11
%58 = call i8* @gai_strerror(i32 %57)
%59 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %56, i8* %58)
%60 = load i32, i32* %10
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %54, %48
br label %63
63: ; preds = %62, %35
br label %64
64: ; preds = %63
%65 = load i32, i32* %9
%66 = add nsw i32 %65, 1
store i32 %66, i32* %9
br label %31
67: ; preds = %31
br label %68
68: ; preds = %67, %14, %3
%69 = load i32, i32* %7
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"connect: %d : %s\0A\00"
define i32 @createSocketInBatch(i32* writeonly %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = icmp ne i32* %0, null
%5 = icmp sgt i32 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %31
7: ; preds = %3
%8 = bitcast i32* %0 to i8*
%9 = zext i32 %1 to i64
%10 = shl nuw nsw i64 %9, 2
%11 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %12
12: ; preds = %7, %28
%13 = phi i64 [ 0, %7 ], [ %29, %28 ]
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %28
18: ; preds = %12
%19 = tail call i32 @connect(i32 %16, %struct.sockaddr* %11, i32 4)
%20 = icmp sgt i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = getelementptr inbounds i32, i32* %0, i64 %13
store i32 %16, i32* %22
br label %28
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i8* @gai_strerror(i32 %19)
%26 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %19, i8* %25)
%27 = tail call i32 @close(i32 %16)
br label %28
28: ; preds = %21, %23, %12
%29 = add nuw nsw i64 %13, 1
%30 = icmp eq i64 %29, %9
br i1 %30, label %31, label %12
31: ; preds = %28, %3
ret i32 0
}