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

/* Variables and functions */
int /*<<< orphan*/ AF_VSOCK;
int /*<<< orphan*/ SOCK_STREAM;
int STDIN_FILENO;
int STDOUT_FILENO;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int xfer(int, int);
int run_echo(uint32_t cid, uint32_t port) {
  int sock = socket(AF_VSOCK, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket()");
    return -1;
  }
  struct sockaddr_vm vsock_addr = {
      .svm_family = AF_VSOCK, .svm_port = port, .svm_cid = cid};
  if (connect(sock, (struct sockaddr *)&vsock_addr, sizeof(vsock_addr)) < 0) {
    perror("connect()");
    return -1;
  }
  for (;;) {
    int ping_cnt = xfer(STDIN_FILENO, sock);
    if (!ping_cnt)
      break;
    if (ping_cnt < 0)
      return -1;
    int pong_cnt = 0;
    while (pong_cnt < ping_cnt) {
      int res = xfer(sock, STDOUT_FILENO);
      if (res <= 0)
        return -1;
      pong_cnt += res;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    118c <run_echo+0x4c>
lea    0xe85(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <run_echo+0x12c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jge    11d7 <run_echo+0x97>
lea    0xe43(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <run_echo+0x12c>
jmp    11dc <run_echo+0x9c>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%esi
call   1030 <xfer@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11ff <run_echo+0xbf>
jmp    1265 <run_echo+0x125>
cmpl   $0x0,-0x24(%rbp)
jge    1215 <run_echo+0xd5>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <run_echo+0x12c>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1260 <run_echo+0x120>
mov    -0x10(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   1030 <xfer@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     1252 <run_echo+0x112>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <run_echo+0x12c>
mov    -0x2c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    121c <run_echo+0xdc>
jmp    11dc <run_echo+0x9c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_echo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e76(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     1206 <run_echo+0xc6>
mov    %eax,%r13d
mov    %ebp,0x8(%rsp)
mov    %r14d,0xc(%rsp)
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   1050 <connect@plt>
test   %rax,%rax
js     120f <run_echo+0xcf>
mov    0x2e24(%rip),%r15
mov    (%r15),%edi
mov    %r13d,%esi
call   1030 <xfer@plt>
test   %eax,%eax
je     1201 <run_echo+0xc1>
mov    %eax,%ebp
mov    0x2e34(%rip),%r12
mov    $0xffffffff,%r14d
test   %ebp,%ebp
js     1221 <run_echo+0xe1>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
cmp    %ebp,%ebx
jge    11f0 <run_echo+0xb0>
mov    (%r12),%esi
mov    %r13d,%edi
call   1030 <xfer@plt>
add    %eax,%ebx
test   %eax,%eax
jg     11d0 <run_echo+0x90>
jmp    1221 <run_echo+0xe1>
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %r13d,%esi
call   1030 <xfer@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11c2 <run_echo+0x82>
xor    %r14d,%r14d
jmp    1221 <run_echo+0xe1>
lea    0xdf3(%rip),%rdi
jmp    1216 <run_echo+0xd6>
lea    0xdf3(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_vm = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_VSOCK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [10 x i8] c"connect()\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @run_echo(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 ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %42
10: ; preds = %2
%11 = bitcast %struct.sockaddr_vm* %3 to i8*
%12 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %3, i64 0, i32 0
store i32 %0, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %3, i64 0, i32 1
store i32 %1, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %3, i64 0, i32 2
%15 = load i32, i32* @AF_VSOCK
store i32 %15, i32* %14
%16 = bitcast %struct.sockaddr_vm* %3 to %struct.sockaddr*
%17 = call i64 @connect(i32 %6, %struct.sockaddr* %16, i32 12)
%18 = icmp slt i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %10
%20 = load i32, i32* @STDIN_FILENO
%21 = call i32 @xfer(i32 %20, i32 %6)
%22 = icmp eq i32 %21, 0
br i1 %22, label %40, label %29
23: ; preds = %10
%24 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %40
25: ; preds = %32
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @xfer(i32 %26, i32 %6)
%28 = icmp eq i32 %27, 0
br i1 %28, label %40, label %29
29: ; preds = %19, %25
%30 = phi i32 [ %27, %25 ], [ %21, %19 ]
%31 = icmp slt i32 %30, 0
br i1 %31, label %40, label %32
32: ; preds = %29, %35
%33 = phi i32 [ %39, %35 ], [ 0, %29 ]
%34 = icmp slt i32 %33, %30
br i1 %34, label %35, label %25
35: ; preds = %32
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @xfer(i32 %6, i32 %36)
%38 = icmp slt i32 %37, 1
%39 = add nsw i32 %37, %33
br i1 %38, label %40, label %32
40: ; preds = %25, %29, %35, %19, %23
%41 = phi i32 [ -1, %23 ], [ 0, %19 ], [ -1, %35 ], [ 0, %25 ], [ -1, %29 ]
br label %42
42: ; preds = %40, %8
%43 = phi i32 [ -1, %8 ], [ %41, %40 ]
ret i32 %43
}