#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 */
typedef int uint32_t;
struct sockaddr_vm {
  int svm_cid;
  int svm_port;
  int /*<<< orphan*/ svm_reserved1;
  int /*<<< orphan*/ svm_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa_listen;
typedef int /*<<< orphan*/ sa_client;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_VSOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ STDOUT_FILENO;
int VMADDR_CID_ANY;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main() {
  uint32_t port = 0x0808;
  int listen_fd;
  int client_fd;
  struct sockaddr_vm sa_listen = {
      .svm_family = AF_VSOCK,
      .svm_reserved1 = 0,
      .svm_cid = VMADDR_CID_ANY,
  };
  struct sockaddr_vm sa_client;
  socklen_t socklen_client;
  char buf[4096];
  int len;
  sa_listen.svm_port = port;
  listen_fd = socket(AF_VSOCK, SOCK_STREAM, 0);
  if (listen_fd < 0) {
    perror("socket()");
    exit(-1);
  }
  if (bind(listen_fd, (struct sockaddr *)&sa_listen, sizeof(sa_listen)) != 0) {
    perror("bind()");
    goto err;
  }
  if (listen(listen_fd, 10) != 0) {
    perror("listen()");
    goto err;
  }
  printf("Listening on port 0x%x...\n", port);
  socklen_client = sizeof(sa_client);
  client_fd = accept(listen_fd, (struct sockaddr *)&sa_client, &socklen_client);
  if (client_fd < 0) {
    perror("accept()");
    goto err;
  }
  printf("Got a connection from the host: cid=0x%x, port=0x%x.\n",
         sa_client.svm_cid, sa_client.svm_port);
  do {
    printf("Reading data from the connection...\n");
    len = read(client_fd, buf, sizeof(buf));
    if (len > 0) {
      printf("Read %d bytes:\n", len);
      fflush(stdout);
      write(STDOUT_FILENO, buf, len);
    }
  } while (len > 0);
  printf("The other end closed the connection.\n");
  close(client_fd);
  close(listen_fd);
  return 0;
err:
  close(listen_fd);
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x808,-0x8(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1231 <main+0x81>
lea    0xdde(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     125d <main+0xad>
lea    0xdb6(%rip),%rdi
call   10a0 <perror@plt>
jmp    138d <main+0x1dd>
mov    -0xc(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     1285 <main+0xd5>
lea    0xd95(%rip),%rdi
call   10a0 <perror@plt>
jmp    138d <main+0x1dd>
mov    -0x8(%rbp),%esi
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x10,-0x34(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12cb <main+0x11b>
lea    0xd73(%rip),%rdi
call   10a0 <perror@plt>
jmp    138d <main+0x1dd>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xd65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x1040(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x1044(%rbp)
cmpl   $0x0,-0x1044(%rbp)
jle    1351 <main+0x1a1>
mov    -0x1044(%rbp),%esi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    -0x1040(%rbp),%rsi
mov    -0x1044(%rbp),%edx
call   1030 <write@plt>
jmp    1356 <main+0x1a6>
cmpl   $0x0,-0x1044(%rbp)
jg     12df <main+0x12f>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <main+0x1ec>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x1c(%rsp)
movq   $0x808,0x14(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     136f <main+0x1af>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    134a <main+0x18a>
mov    %ebx,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    1353 <main+0x193>
lea    0xdd5(%rip),%rdi
mov    $0x808,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x10,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <accept@plt>
test   %eax,%eax
js     135c <main+0x19c>
mov    %eax,%ebp
mov    %ebx,0x8(%rsp)
mov    0x20(%rsp),%esi
mov    0x24(%rsp),%edx
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
lea    0x30(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1316 <main+0x156>
mov    %eax,%r14d
mov    0x2d10(%rip),%r15
mov    0x2d31(%rip),%r12
lea    0x30(%rsp),%rbx
lea    0xdb8(%rip),%r13
nopl   0x0(%rax,%rax,1)
lea    0xd9c(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%edi
call   1080 <fflush@plt>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <write@plt>
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,%r14d
test   %eax,%eax
jg     12d0 <main+0x110>
lea    0xd8a(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x8(%rsp),%ebx
xor    %ebp,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x1038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb8(%rip),%rdi
jmp    1363 <main+0x1a3>
lea    0xcb6(%rip),%rdi
jmp    1363 <main+0x1a3>
lea    0xcd1(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    132f <main+0x16f>
lea    0xc8a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_vm = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@VMADDR_CID_ANY = global i32 0
@AF_VSOCK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [7 x i8] c"bind()\00"
@.str.2 = [9 x i8] c"listen()\00"
@.str.3 = [27 x i8] c"Listening on port 0x%x...\0A\00"
@.str.4 = [9 x i8] c"accept()\00"
@.str.5 = [54 x i8] c"Got a connection from the host: cid=0x%x, port=0x%x.\0A\00"
@.str.6 = [37 x i8] c"Reading data from the connection...\0A\00"
@.str.7 = [16 x i8] c"Read %d bytes:\0A\00"
@stdout = global i32 0
@STDOUT_FILENO = global i32 0
@.str.8 = [38 x i8] c"The other end closed the connection.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_vm
%6 = alloca %struct.sockaddr_vm
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i32
store i32 0, i32* %1
store i32 2056, i32* %2
%10 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %5, i32 0, i32 0
%11 = load i32, i32* @VMADDR_CID_ANY
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %5, i32 0, i32 1
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %5, i32 0, i32 2
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %5, i32 0, i32 3
%15 = load i32, i32* @AF_VSOCK
store i32 %15, i32* %14
%16 = load i32, i32* %2
%17 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %5, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @AF_VSOCK
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %0
%24 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %0
%27 = load i32, i32* %3
%28 = bitcast %struct.sockaddr_vm* %5 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 16)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %81
33: ; preds = %26
%34 = load i32, i32* %3
%35 = call i64 @listen(i32 %34, i32 10)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %81
39: ; preds = %33
%40 = load i32, i32* %2
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %40)
store i32 16, i32* %7
%42 = load i32, i32* %3
%43 = bitcast %struct.sockaddr_vm* %6 to %struct.sockaddr*
%44 = call i32 @accept(i32 %42, %struct.sockaddr* %43, i32* %7)
store i32 %44, i32* %4
%45 = load i32, i32* %4
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %81
49: ; preds = %39
%50 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %6, i32 0, i32 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %6, i32 0, i32 1
%53 = load i32, i32* %52
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i32 %51, i32 %53)
br label %55
55: ; preds = %72, %49
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
%57 = load i32, i32* %4
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 4096)
store i32 %59, i32* %9
%60 = load i32, i32* %9
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %71
62: ; preds = %55
%63 = load i32, i32* %9
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %63)
%65 = load i32, i32* @stdout
%66 = call i32 @fflush(i32 %65)
%67 = load i32, i32* @STDOUT_FILENO
%68 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%69 = load i32, i32* %9
%70 = call i32 @write(i32 %67, i8* %68, i32 %69)
br label %71
71: ; preds = %62, %55
br label %72
72: ; preds = %71
%73 = load i32, i32* %9
%74 = icmp sgt i32 %73, 0
br i1 %74, label %55, label %75
75: ; preds = %72
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0))
%77 = load i32, i32* %4
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %3
%80 = call i32 @close(i32 %79)
store i32 0, i32* %1
br label %84
81: ; preds = %47, %37, %31
%82 = load i32, i32* %3
%83 = call i32 @close(i32 %82)
store i32 -1, i32* %1
br label %84
84: ; preds = %81, %75
%85 = load i32, i32* %1
ret i32 %85
}

LLVM IR (-O2):
%struct.sockaddr_vm = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@VMADDR_CID_ANY = global i32 0
@AF_VSOCK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [7 x i8] c"bind()\00"
@.str.2 = [9 x i8] c"listen()\00"
@.str.3 = [27 x i8] c"Listening on port 0x%x...\0A\00"
@.str.4 = [9 x i8] c"accept()\00"
@.str.5 = [54 x i8] c"Got a connection from the host: cid=0x%x, port=0x%x.\0A\00"
@.str.7 = [16 x i8] c"Read %d bytes:\0A\00"
@stdout = global i32 0
@STDOUT_FILENO = global i32 0
@str = [36 x i8] c"Reading data from the connection...\00"
@str.9 = [37 x i8] c"The other end closed the connection.\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_vm
%2 = alloca %struct.sockaddr_vm
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = bitcast %struct.sockaddr_vm* %1 to i8*
%6 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %1, i64 0, i32 0
%7 = load i32, i32* @VMADDR_CID_ANY
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %1, i64 0, i32 1
%9 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %1, i64 0, i32 2
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %1, i64 0, i32 3
%11 = load i32, i32* @AF_VSOCK
store i32 %11, i32* %10
%12 = bitcast %struct.sockaddr_vm* %2 to i8*
%13 = bitcast i32* %3 to i8*
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
store i32 2056, i32* %8
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %11, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %0
%22 = bitcast %struct.sockaddr_vm* %1 to %struct.sockaddr*
%23 = call i64 @bind(i32 %16, %struct.sockaddr* %22, i32 16)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %55
25: ; preds = %21
%26 = call i64 @listen(i32 %16, i32 10)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %55
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 2056)
store i32 16, i32* %3
%30 = bitcast %struct.sockaddr_vm* %2 to %struct.sockaddr*
%31 = call i32 @accept(i32 %16, %struct.sockaddr* %30, i32* %3)
%32 = icmp slt i32 %31, 0
br i1 %32, label %55, label %33
33: ; preds = %28
%34 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %2, i64 0, i32 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %2, i64 0, i32 1
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i32 %35, i32 %37)
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%40 = call i32 @read(i32 %31, i8* %14, i32 4096)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %52
42: ; preds = %33, %42
%43 = phi i32 [ %50, %42 ], [ %40, %33 ]
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @stdout
%46 = call i32 @fflush(i32 %45)
%47 = load i32, i32* @STDOUT_FILENO
%48 = call i32 @write(i32 %47, i8* %14, i32 %43)
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%50 = call i32 @read(i32 %31, i8* %14, i32 4096)
%51 = icmp sgt i32 %50, 0
br i1 %51, label %42, label %52
52: ; preds = %42, %33
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.9, i64 0, i64 0))
%54 = call i32 @close(i32 %31)
br label %58
55: ; preds = %28, %25, %21
%56 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %21 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %25 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), %28 ]
%57 = call i32 @perror(i8* %56)
br label %58
58: ; preds = %55, %52
%59 = phi i32 [ -1, %55 ], [ 0, %52 ]
%60 = call i32 @close(i32 %16)
ret i32 %59
}