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

/* Variables and functions */
int /*<<< orphan*/ AF_VSOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ VMADDR_CID_ANY;
int /*<<< orphan*/ WNOHANG;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fork();
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int xfer(int, int);
int run_echosrv(uint32_t port) {
  struct sockaddr_vm vsock_addr = {
      .svm_family = AF_VSOCK, .svm_port = port, .svm_cid = VMADDR_CID_ANY};
  int srv_sock = socket(AF_VSOCK, SOCK_STREAM, 0);
  if (srv_sock < 0) {
    perror("socket()");
    return -1;
  }
  int res = bind(srv_sock, (struct sockaddr *)&vsock_addr,
                 sizeof(struct sockaddr_vm));
  if (res) {
    perror("bind()");
    return -1;
  }
  res = listen(srv_sock, 128);
  if (res) {
    perror("listen()");
    return -1;
  }
  for (;;) {
    struct sockaddr cl_addr;
    socklen_t sockaddr_len = sizeof(cl_addr);
    int cl_sock = accept(srv_sock, &cl_addr, &sockaddr_len);
    if (cl_sock < 0) {
      perror("accept()");
      continue;
    }
    int pid = fork();
    if (pid < 0) {
      perror("fork()");
      close(cl_sock);
      continue;
    }
    if (!pid) {
      int res;
      do {
        res = xfer(cl_sock, cl_sock);
      } while (res > 0);
      return res >= 0 ? 0 : -1;
    }
    close(cl_sock);
    int cstatus;
    waitpid(-1, &cstatus, WNOHANG);
    printf("New client forked...\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_echosrv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1207 <run_echosrv+0x67>
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1345 <run_echosrv+0x1a5>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     123d <run_echosrv+0x9d>
lea    0xddd(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1345 <run_echosrv+0x1a5>
mov    -0x1c(%rbp),%edi
mov    $0x80,%esi
call   1060 <listen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     126f <run_echosrv+0xcf>
lea    0xdb2(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1345 <run_echosrv+0x1a5>
jmp    1274 <run_echosrv+0xd4>
movl   $0x4,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    12a9 <run_echosrv+0x109>
lea    0xd7a(%rip),%rdi
call   1090 <perror@plt>
jmp    1274 <run_echosrv+0xd4>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12d6 <run_echosrv+0x136>
lea    0xd5e(%rip),%rdi
call   1090 <perror@plt>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
jmp    1274 <run_echosrv+0xd4>
cmpl   $0x0,-0x34(%rbp)
jne    1315 <run_echosrv+0x175>
jmp    12e5 <run_echosrv+0x145>
mov    -0x30(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1030 <xfer@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jg     12e5 <run_echosrv+0x145>
mov    -0x38(%rbp),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovge %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1345 <run_echosrv+0x1a5>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x3c(%rbp),%rsi
call   1080 <waitpid@plt>
lea    0xcee(%rip),%rdi
call   1040 <printf@plt>
jmp    1274 <run_echosrv+0xd4>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_echosrv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %edi,0x14(%rsp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x18(%rsp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12d2 <run_echosrv+0x132>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jne    12db <run_echosrv+0x13b>
mov    %ebx,%edi
mov    $0x80,%esi
call   1060 <listen@plt>
test   %eax,%eax
jne    12e4 <run_echosrv+0x144>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x24(%rsp),%rbp
lea    0xe00(%rip),%r13
jmp    1240 <run_echosrv+0xa0>
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
nopw   0x0(%rax,%rax,1)
movl   $0x4,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <accept@plt>
test   %eax,%eax
js     122b <run_echosrv+0x8b>
mov    %eax,%r12d
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1291 <run_echosrv+0xf1>
je     12b0 <run_echosrv+0x110>
mov    %r12d,%edi
call   1050 <close@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
mov    %rbp,%rsi
call   1080 <waitpid@plt>
mov    %r13,%rdi
call   1030 <puts@plt>
jmp    1240 <run_echosrv+0xa0>
lea    0xd8a(%rip),%rdi
call   1090 <perror@plt>
mov    %r12d,%edi
call   1050 <close@plt>
jmp    1240 <run_echosrv+0xa0>
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r12d,%esi
call   1040 <xfer@plt>
test   %eax,%eax
jg     12b0 <run_echosrv+0x110>
neg    %eax
sbb    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd27(%rip),%rdi
jmp    12eb <run_echosrv+0x14b>
lea    0xd27(%rip),%rdi
jmp    12eb <run_echosrv+0x14b>
lea    0xd25(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    12c3 <run_echosrv+0x123>

LLVM IR (-O0):
%struct.sockaddr_vm = type { 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 = [9 x i8] c"accept()\00"
@.str.4 = [7 x i8] c"fork()\00"
@WNOHANG = global i32 0
@.str.5 = [22 x i8] c"New client forked...\0A\00"
define i32 @run_echosrv(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_vm
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
%13 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %4, i32 0, i32 0
%14 = load i32, i32* @VMADDR_CID_ANY
store i32 %14, i32* %13
%15 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %4, i32 0, i32 1
%16 = load i32, i32* %3
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %4, i32 0, i32 2
%18 = load i32, i32* @AF_VSOCK
store i32 %18, i32* %17
%19 = load i32, i32* @AF_VSOCK
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %1
%25 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %79
26: ; preds = %1
%27 = load i32, i32* %5
%28 = bitcast %struct.sockaddr_vm* %4 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %79
34: ; preds = %26
%35 = load i32, i32* %5
%36 = call i32 @listen(i32 %35, i32 128)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %79
41: ; preds = %34
br label %42
42: ; preds = %73, %53, %47, %41
store i32 4, i32* %8
%43 = load i32, i32* %5
%44 = call i32 @accept(i32 %43, %struct.sockaddr* %7, i32* %8)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %42
49: ; preds = %42
%50 = call i32 (...) @fork()
store i32 %50, i32* %10
%51 = load i32, i32* %10
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32, i32* %9
%56 = call i32 @close(i32 %55)
br label %42
57: ; preds = %49
%58 = load i32, i32* %10
%59 = icmp ne i32 %58, 0
br i1 %59, label %73, label %60
60: ; preds = %57
br label %61
61: ; preds = %65, %60
%62 = load i32, i32* %9
%63 = load i32, i32* %9
%64 = call i32 @xfer(i32 %62, i32 %63)
store i32 %64, i32* %11
br label %65
65: ; preds = %61
%66 = load i32, i32* %11
%67 = icmp sgt i32 %66, 0
br i1 %67, label %61, label %68
68: ; preds = %65
%69 = load i32, i32* %11
%70 = icmp sge i32 %69, 0
%71 = zext i1 %70 to i64
%72 = select i1 %70, i32 0, i32 -1
store i32 %72, i32* %2
br label %79
73: ; preds = %57
%74 = load i32, i32* %9
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* @WNOHANG
%77 = call i32 @waitpid(i32 -1, i32* %12, i32 %76)
%78 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %42
79: ; preds = %68, %39, %32, %24
%80 = load i32, i32* %2
ret i32 %80
}

LLVM IR (-O2):
%struct.sockaddr_vm = type { 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 = [9 x i8] c"accept()\00"
@.str.4 = [7 x i8] c"fork()\00"
@WNOHANG = global i32 0
@str = [21 x i8] c"New client forked...\00"
define i32 @run_echosrv(i32 %0) {
%2 = alloca %struct.sockaddr_vm
%3 = alloca %struct.sockaddr
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.sockaddr_vm* %2 to i8*
%7 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %2, i64 0, i32 0
%8 = load i32, i32* @VMADDR_CID_ANY
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %2, i64 0, i32 1
store i32 %0, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_vm, %struct.sockaddr_vm* %2, i64 0, i32 2
%11 = load i32, i32* @AF_VSOCK
store i32 %11, i32* %10
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %57
17: ; preds = %1
%18 = bitcast %struct.sockaddr_vm* %2 to %struct.sockaddr*
%19 = call i32 @bind(i32 %13, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %57
23: ; preds = %17
%24 = call i32 @listen(i32 %13, i32 128)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = bitcast %struct.sockaddr* %3 to i8*
%28 = bitcast i32* %4 to i8*
%29 = bitcast i32* %5 to i8*
br label %32
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %57
32: ; preds = %26, %56
store i32 4, i32* %4
%33 = call i32 @accept(i32 %13, %struct.sockaddr* %3, i32* %4)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %56
37: ; preds = %32
%38 = call i32 (...) @fork()
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @close(i32 %33)
br label %56
43: ; preds = %37
%44 = icmp eq i32 %38, 0
br i1 %44, label %45, label %51
45: ; preds = %43, %45
%46 = call i32 @xfer(i32 %33, i32 %33)
%47 = icmp sgt i32 %46, 0
br i1 %47, label %45, label %48
48: ; preds = %45
%49 = icmp ne i32 %46, 0
%50 = sext i1 %49 to i32
br label %57
51: ; preds = %43
%52 = call i32 @close(i32 %33)
%53 = load i32, i32* @WNOHANG
%54 = call i32 @waitpid(i32 -1, i32* %5, i32 %53)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %56
56: ; preds = %40, %51, %35
br label %32
57: ; preds = %48, %21, %30, %15
%58 = phi i32 [ -1, %15 ], [ -1, %21 ], [ -1, %30 ], [ %50, %48 ]
ret i32 %58
}