#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;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int atoi(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int fork();
int print_usage();
int /*<<< orphan*/ printf(char *, int);
int run_echo(int, int);
int run_echosrv(int);
int /*<<< orphan*/ setsid();
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  if (argc < 3) {
    return print_usage();
  }
  if (strcmp(argv[1], "echosrv") == 0) {
    uint32_t port;
    if (strcmp(argv[2], "-d") == 0) {
      if (argc < 4) {
        return print_usage();
      }
      port = atoi(argv[3]);
      if (!port) {
        return print_usage();
      }
      int pid = fork();
      if (pid < 0)
        return -1;
      if (pid) {
        printf("Forked vsock echo daemon listening on port %d\n", port);
        return 0;
      }
      setsid();
      close(STDIN_FILENO);
      close(STDOUT_FILENO);
    } else {
      port = atoi(argv[2]);
      if (!port) {
        return print_usage();
      }
    }
    return run_echosrv(port);
  }
  if (strcmp(argv[1], "echo") == 0) {
    if (argc != 4) {
      return print_usage();
    }
    uint32_t cid = atoi(argv[2]);
    uint32_t port = atoi(argv[3]);
    if (!cid || !port) {
      return print_usage();
    }
    return run_echo(cid, port);
  }
  return print_usage();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11bf <main+0x2f>
mov    $0x0,%al
call   10a0 <print_usage@plt>
mov    %eax,-0x4(%rbp)
jmp    137f <main+0x1ef>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe32(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12e8 <main+0x158>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe1c(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12aa <main+0x11a>
cmpl   $0x4,-0x8(%rbp)
jge    1214 <main+0x84>
mov    $0x0,%al
call   10a0 <print_usage@plt>
mov    %eax,-0x4(%rbp)
jmp    137f <main+0x1ef>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    123d <main+0xad>
mov    $0x0,%al
call   10a0 <print_usage@plt>
mov    %eax,-0x4(%rbp)
jmp    137f <main+0x1ef>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    125d <main+0xcd>
movl   $0xffffffff,-0x4(%rbp)
jmp    137f <main+0x1ef>
cmpl   $0x0,-0x18(%rbp)
je     1282 <main+0xf2>
mov    -0x14(%rbp),%esi
lea    0xd9a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    137f <main+0x1ef>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
jmp    12d8 <main+0x148>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    12d3 <main+0x143>
mov    $0x0,%al
call   10a0 <print_usage@plt>
mov    %eax,-0x4(%rbp)
jmp    137f <main+0x1ef>
jmp    12d8 <main+0x148>
mov    -0x14(%rbp),%edi
call   1030 <run_echosrv@plt>
mov    %eax,-0x4(%rbp)
jmp    137f <main+0x1ef>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd43(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1375 <main+0x1e5>
cmpl   $0x4,-0x8(%rbp)
je     131f <main+0x18f>
mov    $0x0,%al
call   10a0 <print_usage@plt>
mov    %eax,-0x4(%rbp)
jmp    137f <main+0x1ef>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1353 <main+0x1c3>
cmpl   $0x0,-0x20(%rbp)
jne    1362 <main+0x1d2>
mov    $0x0,%al
call   10a0 <print_usage@plt>
mov    %eax,-0x4(%rbp)
jmp    137f <main+0x1ef>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1080 <run_echo@plt>
mov    %eax,-0x4(%rbp)
jmp    137f <main+0x1ef>
mov    $0x0,%al
call   10a0 <print_usage@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jle    1267 <main+0xd7>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
lea    0xe54(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1203 <main+0x73>
mov    0x8(%rbx),%rdi
lea    0xe79(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1267 <main+0xd7>
cmp    $0x4,%ebp
jne    1267 <main+0xd7>
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    0x18(%rbx),%rdi
call   1090 <atoi@plt>
test   %ebp,%ebp
je     1267 <main+0xd7>
test   %eax,%eax
je     1267 <main+0xd7>
mov    %ebp,%edi
mov    %eax,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <run_echo@plt>
mov    0x10(%rbx),%rdi
lea    0xdfa(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1234 <main+0xa4>
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1267 <main+0xd7>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <run_echosrv@plt>
cmp    $0x3,%ebp
jbe    1267 <main+0xd7>
mov    0x18(%rbx),%rdi
call   1090 <atoi@plt>
test   %eax,%eax
je     1267 <main+0xd7>
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1274 <main+0xe4>
je     1280 <main+0xf0>
lea    0xdaf(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %eax,%eax
jmp    1279 <main+0xe9>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    10a0 <print_usage@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
jmp    1227 <main+0x97>

LLVM IR (-O0):
@.str = [8 x i8] c"echosrv\00"
@.str.1 = [3 x i8] c"-d\00"
@.str.2 = [47 x i8] c"Forked vsock echo daemon listening on port %d\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.3 = [5 x i8] c"echo\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (...) @print_usage()
store i32 %13, i32* %3
br label %103
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %70
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %57
26: ; preds = %20
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 4
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 (...) @print_usage()
store i32 %30, i32* %3
br label %103
31: ; preds = %26
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %31
%39 = call i32 (...) @print_usage()
store i32 %39, i32* %3
br label %103
40: ; preds = %31
%41 = call i32 (...) @fork()
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -1, i32* %3
br label %103
45: ; preds = %40
%46 = load i32, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %6
%50 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %49)
store i32 0, i32* %3
br label %103
51: ; preds = %45
%52 = call i32 (...) @setsid()
%53 = load i32, i32* @STDIN_FILENO
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* @STDOUT_FILENO
%56 = call i32 @close(i32 %55)
br label %67
57: ; preds = %20
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 2
%60 = load i8*, i8** %59
%61 = call i32 @atoi(i8* %60)
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = icmp ne i32 %62, 0
br i1 %63, label %66, label %64
64: ; preds = %57
%65 = call i32 (...) @print_usage()
store i32 %65, i32* %3
br label %103
66: ; preds = %57
br label %67
67: ; preds = %66, %51
%68 = load i32, i32* %6
%69 = call i32 @run_echosrv(i32 %68)
store i32 %69, i32* %3
br label %103
70: ; preds = %14
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 1
%73 = load i8*, i8** %72
%74 = call i64 @strcmp(i8* %73, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %101
76: ; preds = %70
%77 = load i32, i32* %4
%78 = icmp ne i32 %77, 4
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 (...) @print_usage()
store i32 %80, i32* %3
br label %103
81: ; preds = %76
%82 = load i8**, i8*** %5
%83 = getelementptr inbounds i8*, i8** %82, i64 2
%84 = load i8*, i8** %83
%85 = call i32 @atoi(i8* %84)
store i32 %85, i32* %8
%86 = load i8**, i8*** %5
%87 = getelementptr inbounds i8*, i8** %86, i64 3
%88 = load i8*, i8** %87
%89 = call i32 @atoi(i8* %88)
store i32 %89, i32* %9
%90 = load i32, i32* %8
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %81
%93 = load i32, i32* %9
%94 = icmp ne i32 %93, 0
br i1 %94, label %97, label %95
95: ; preds = %92, %81
%96 = call i32 (...) @print_usage()
store i32 %96, i32* %3
br label %103
97: ; preds = %92
%98 = load i32, i32* %8
%99 = load i32, i32* %9
%100 = call i32 @run_echo(i32 %98, i32 %99)
store i32 %100, i32* %3
br label %103
101: ; preds = %70
%102 = call i32 (...) @print_usage()
store i32 %102, i32* %3
br label %103
103: ; preds = %101, %97, %95, %79, %67, %64, %48, %44, %38, %29, %12
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
@.str = [8 x i8] c"echosrv\00"
@.str.1 = [3 x i8] c"-d\00"
@.str.2 = [47 x i8] c"Forked vsock echo daemon listening on port %d\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.3 = [5 x i8] c"echo\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (...) @print_usage()
br label %73
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %49
11: ; preds = %6
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %40
16: ; preds = %11
%17 = icmp ult i32 %0, 4
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 (...) @print_usage()
br label %73
20: ; preds = %16
%21 = getelementptr inbounds i8*, i8** %1, i64 3
%22 = load i8*, i8** %21
%23 = tail call i32 @atoi(i8* %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 (...) @print_usage()
br label %73
27: ; preds = %20
%28 = tail call i32 (...) @fork()
%29 = icmp slt i32 %28, 0
br i1 %29, label %73, label %30
30: ; preds = %27
%31 = icmp eq i32 %28, 0
br i1 %31, label %34, label %32
32: ; preds = %30
%33 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %73
34: ; preds = %30
%35 = tail call i32 (...) @setsid()
%36 = load i32, i32* @STDIN_FILENO
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @STDOUT_FILENO
%39 = tail call i32 @close(i32 %38)
br label %46
40: ; preds = %11
%41 = load i8*, i8** %12
%42 = tail call i32 @atoi(i8* %41)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = tail call i32 (...) @print_usage()
br label %73
46: ; preds = %34, %40
%47 = phi i32 [ %23, %34 ], [ %42, %40 ]
%48 = tail call i32 @run_echosrv(i32 %47)
br label %73
49: ; preds = %6
%50 = load i8*, i8** %7
%51 = tail call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %71
53: ; preds = %49
%54 = icmp eq i32 %0, 4
br i1 %54, label %57, label %55
55: ; preds = %53
%56 = tail call i32 (...) @print_usage()
br label %73
57: ; preds = %53
%58 = getelementptr inbounds i8*, i8** %1, i64 2
%59 = load i8*, i8** %58
%60 = tail call i32 @atoi(i8* %59)
%61 = getelementptr inbounds i8*, i8** %1, i64 3
%62 = load i8*, i8** %61
%63 = tail call i32 @atoi(i8* %62)
%64 = icmp ne i32 %60, 0
%65 = icmp ne i32 %63, 0
%66 = select i1 %64, i1 %65, i1 false
br i1 %66, label %69, label %67
67: ; preds = %57
%68 = tail call i32 (...) @print_usage()
br label %73
69: ; preds = %57
%70 = tail call i32 @run_echo(i32 %60, i32 %63)
br label %73
71: ; preds = %49
%72 = tail call i32 (...) @print_usage()
br label %73
73: ; preds = %27, %32, %67, %69, %18, %25, %44, %46, %71, %55, %4
%74 = phi i32 [ %5, %4 ], [ %56, %55 ], [ %72, %71 ], [ %19, %18 ], [ %48, %46 ], [ %26, %25 ], [ %45, %44 ], [ %70, %69 ], [ %68, %67 ], [ -1, %27 ], [ 0, %32 ]
ret i32 %74
}