#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_vm {
  int svm_port;
  int /*<<< orphan*/ svm_cid;
  int /*<<< orphan*/ svm_reserved1;
  int /*<<< orphan*/ svm_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_VSOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ VMADDR_CID_HOST;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int i, fd;
  struct sockaddr_vm sa = {
      .svm_family = AF_VSOCK,
      .svm_reserved1 = 0,
      .svm_cid = VMADDR_CID_HOST,
  };
  sa.svm_port = 0x808;
  for (i = 0; i < 100; i++) {
    fd = socket(AF_VSOCK, SOCK_STREAM, 0);
    if (fd < 0) {
      perror("socket");
      return -1;
    }
    if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
      perror("connect");
      return -1;
    }
    printf("Connected to the host.\n");
    close(fd);
    printf("Closed the connection.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x808,-0x20(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x64,-0x8(%rbp)
jge    123c <main+0xec>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11db <main+0x8b>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <main+0xf3>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
je     120e <main+0xbe>
lea    0xe0a(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <main+0xf3>
lea    0xdfa(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
lea    0xdfe(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1193 <main+0x43>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x14(%rsp)
movl   $0x808,0x8(%rsp)
mov    0x2e49(%rip),%r14
mov    (%r14),%esi
xor    %edx,%edx
call   1070 <socket@plt>
lea    0xe60(%rip),%rdi
test   %eax,%eax
js     123b <main+0xeb>
mov    %eax,%ebp
mov    $0x64,%r13d
lea    0x8(%rsp),%r15
lea    0xe53(%rip),%r12
lea    0xe63(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
test   %rax,%rax
jne    1234 <main+0xe4>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
add    $0xffffffff,%r13d
je     1223 <main+0xd3>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11d0 <main+0x80>
lea    0xddf(%rip),%rdi
jmp    123b <main+0xeb>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcc(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    1225 <main+0xd5>

LLVM IR (-O0):
%struct.sockaddr_vm = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@VMADDR_CID_HOST = global i32 0
@AF_VSOCK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [24 x i8] c"Connected to the host.\0A\00"
@.str.3 = [24 x i8] c"Closed the connection.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_vm
store i32 0, i32* %1
%5 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %4, i32 0, i32 0
store i32 0, i32* %5
%6 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %4, i32 0, i32 1
%7 = load i32, i32* @VMADDR_CID_HOST
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %4, i32 0, i32 2
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %4, i32 0, i32 3
%10 = load i32, i32* @AF_VSOCK
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %4, i32 0, i32 0
store i32 2056, i32* %11
store i32 0, i32* %2
br label %12
12: ; preds = %35, %0
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 100
br i1 %14, label %15, label %38
15: ; preds = %12
%16 = load i32, i32* @AF_VSOCK
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %39
23: ; preds = %15
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_vm* %4 to %struct.sockaddr*
%26 = call i64 @connect(i32 %24, %struct.sockaddr* %25, i32 16)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %39
30: ; preds = %23
%31 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %30
%36 = load i32, i32* %2
%37 = add nsw i32 %36, 1
store i32 %37, i32* %2
br label %12
38: ; preds = %12
store i32 0, i32* %1
br label %39
39: ; preds = %38, %28, %21
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_vm = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@VMADDR_CID_HOST = global i32 0
@AF_VSOCK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
@str = [23 x i8] c"Connected to the host.\00"
@str.4 = [23 x i8] c"Closed the connection.\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_vm
%2 = bitcast %struct.sockaddr_vm* %1 to i8*
%3 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %1, i64 0, i32 0
%4 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %1, i64 0, i32 1
%5 = load i32, i32* @VMADDR_CID_HOST
store i32 %5, i32* %4
%6 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %1, i64 0, i32 2
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %1, i64 0, i32 3
%8 = load i32, i32* @AF_VSOCK
store i32 %8, i32* %7
store i32 2056, i32* %3
%9 = bitcast %struct.sockaddr_vm* %1 to %struct.sockaddr*
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %8, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %29, label %13
13: ; preds = %0, %24
%14 = phi i32 [ %27, %24 ], [ %11, %0 ]
%15 = phi i32 [ %22, %24 ], [ 0, %0 ]
%16 = call i64 @connect(i32 %14, %struct.sockaddr* %9, i32 16)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %13
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%20 = call i32 @close(i32 %14)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0))
%22 = add nuw nsw i32 %15, 1
%23 = icmp eq i32 %22, 100
br i1 %23, label %32, label %24
24: ; preds = %18
%25 = load i32, i32* @AF_VSOCK
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %13
29: ; preds = %24, %13, %0
%30 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %24 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %13 ]
%31 = call i32 @perror(i8* %30)
br label %32
32: ; preds = %18, %29
%33 = phi i32 [ -1, %29 ], [ 0, %18 ]
ret i32 %33
}