#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 {
  unsigned int svm_port;
  unsigned int svm_cid;
  int /*<<< orphan*/ svm_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_VSOCK;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int, char *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int openvsock(unsigned int cid, unsigned int port) {
  int s = socket(AF_VSOCK, SOCK_STREAM, 0);
  if (s < 0) {
    perror("socket: AF_VSOCK");
    return -1;
  }
  struct sockaddr_vm addr = {0};
  addr.svm_family = AF_VSOCK;
  addr.svm_port = port;
  addr.svm_cid = cid;
  if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    fprintf(stderr, "connect: port %u: %s", port, strerror(errno));
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<openvsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ac <openvsock+0x4c>
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <openvsock+0xd6>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    1230 <openvsock+0xd0>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xdf2(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <openvsock+0xd6>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openvsock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11ea <openvsock+0x9a>
mov    %eax,%ebp
movl   $0x0,0x14(%rsp)
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,0x8(%rsp)
mov    %ebx,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1050 <connect@plt>
test   %rax,%rax
jns    11dd <openvsock+0x8d>
mov    0x2e2c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe46(%rip),%rsi
mov    %ebx,%edi
mov    %r14d,%edx
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0f(%rip),%rdi
call   1040 <perror@plt>
jmp    11d8 <openvsock+0x88>

LLVM IR (-O0):
%struct.sockaddr_vm = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_VSOCK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"socket: AF_VSOCK\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"connect: port %u: %s\00"
@errno = global i32 0
define i32 @openvsock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_vm
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_VSOCK
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %35
15: ; preds = %2
%16 = bitcast %struct.sockaddr_vm* %7 to i8*
%17 = load i32, i32* @AF_VSOCK
%18 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %7, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* %5
%20 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %7, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %4
%22 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %7, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_vm* %7 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 16)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %15
%28 = load i32, i32* @stderr
%29 = load i32, i32* %5
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %29, i8* %31)
store i32 -1, i32* %3
br label %35
33: ; preds = %15
%34 = load i32, i32* %6
store i32 %34, i32* %3
br label %35
35: ; preds = %33, %27, %13
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_vm = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_VSOCK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"socket: AF_VSOCK\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"connect: port %u: %s\00"
@errno = global i32 0
define i32 @openvsock(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_vm
%4 = load i32, i32* @AF_VSOCK
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %27
10: ; preds = %2
%11 = bitcast %struct.sockaddr_vm* %3 to i8*
%12 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %3, i64 0, i32 3
store i32 0, i32* %12
%13 = load i32, i32* @AF_VSOCK
%14 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %3, i64 0, i32 0
store i32 %1, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %3, i64 0, i32 1
store i32 %0, i32* %16
%17 = bitcast %struct.sockaddr_vm* %3 to %struct.sockaddr*
%18 = call i64 @connect(i32 %6, %struct.sockaddr* %17, i32 16)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %10
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8* %23)
br label %25
25: ; preds = %10, %20
%26 = phi i32 [ -1, %20 ], [ %6, %10 ]
br label %27
27: ; preds = %25, %8
%28 = phi i32 [ -1, %8 ], [ %26, %25 ]
ret i32 %28
}