#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_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fsin;
typedef int /*<<< orphan*/ __sighandler_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ TCPechod(int);
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errexit(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int passiveTCP(char *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int main(int argc, char *argv[]) {
  char *service = "echo";
  struct sockaddr_in fsin;
  unsigned int alen;
  int msock, ssock;
  switch (argc) {
  case 1:
    break;
  case 2:
    service = argv[1];
    break;
  default:
    errexit("usage: TCPechod [port]\n");
  }
  msock = passiveTCP(service, 32);
  (void)signal(SIGCHLD, (__sighandler_t)32);
  while (1) {
    alen = sizeof(fsin);
    ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
    if (ssock < 0) {
      if (errno == EINTR)
        continue;
      errexit("accept: %s\n", strerror(errno));
    }
    switch (fork()) {
    case 0:
      (void)close(msock);
      exit(TCPechod(ssock));
    default:
      (void)close(ssock);
      break;
    case -1:
      errexit("fork: %s\n", strerror(errno));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x1,%eax
je     11d6 <main+0x46>
jmp    11c5 <main+0x35>
mov    -0x30(%rbp),%eax
sub    $0x2,%eax
je     11db <main+0x4b>
jmp    11ec <main+0x5c>
jmp    11fa <main+0x6a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11fa <main+0x6a>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <errexit@plt>
mov    -0x18(%rbp),%rdi
mov    $0x20,%esi
call   1090 <passiveTCP@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    $0x20,%esi
call   1050 <signal@plt>
movl   $0x4,-0x24(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1280 <main+0xf0>
mov    0x2d8f(%rip),%rax
mov    (%rax),%rax
mov    0x2d7d(%rip),%rcx
cmp    (%rcx),%rax
jne    1261 <main+0xd1>
jmp    121e <main+0x8e>
mov    0x2d70(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1040 <errexit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
sub    $0xffffffff,%eax
je     12ce <main+0x13e>
jmp    129a <main+0x10a>
mov    -0x34(%rbp),%eax
test   %eax,%eax
jne    12c1 <main+0x131>
jmp    12aa <main+0x11a>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x2c(%rbp),%edi
call   1060 <TCPechod@plt>
mov    %eax,%edi
call   1080 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
jmp    12ed <main+0x15d>
mov    0x2d03(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1040 <errexit@plt>
jmp    121e <main+0x8e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe5b(%rip),%rbx
cmp    $0x1,%edi
je     11c3 <main+0x33>
cmp    $0x2,%edi
jne    11b5 <main+0x25>
mov    0x8(%rsi),%rbx
jmp    11c3 <main+0x33>
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1040 <errexit@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1090 <passiveTCP@plt>
mov    %eax,%ebx
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    $0x20,%esi
call   1050 <signal@plt>
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
mov    0x2de2(%rip),%r12
lea    0xe2c(%rip),%r13
jmp    1215 <main+0x85>
nop
mov    (%r12),%rdi
call   10a0 <strerror@plt>
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <errexit@plt>
movl   $0x4,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1255 <main+0xc5>
mov    (%r12),%rdi
mov    0x2d95(%rip),%rax
cmp    (%rax),%rdi
je     1215 <main+0x85>
call   10a0 <strerror@plt>
lea    0xdd1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <errexit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1200 <main+0x70>
test   %eax,%eax
je     126e <main+0xde>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    1215 <main+0x85>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%edi
call   1060 <TCPechod@plt>
mov    %eax,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"echo\00"
@.str.1 = [24 x i8] c"usage: TCPechod [port]\0A\00"
@SIGCHLD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [12 x i8] c"accept: %s\0A\00"
@.str.3 = [10 x i8] c"fork: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %6
%11 = load i32, i32* %4
switch i32 %11, label %17 [
i32 1, label %12
i32 2, label %13
]
12: ; preds = %2
br label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
br label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %13, %12
%20 = load i8*, i8** %6
%21 = call i32 @passiveTCP(i8* %20, i32 32)
store i32 %21, i32* %9
%22 = load i32, i32* @SIGCHLD
%23 = call i32 @signal(i32 %22, i32 32)
br label %24
24: ; preds = %19, %34, %54
store i32 4, i32* %8
%25 = load i32, i32* %9
%26 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%27 = call i32 @accept(i32 %25, %struct.sockaddr* %26, i32* %8)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %24
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
br label %24
35: ; preds = %30
%36 = load i64, i64* @errno
%37 = call i32 @strerror(i64 %36)
%38 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %35, %24
%40 = call i32 (...) @fork()
switch i32 %40, label %47 [
i32 0, label %41
i32 -1, label %50
]
41: ; preds = %39
%42 = load i32, i32* %9
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %10
%45 = call i32 @TCPechod(i32 %44)
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %39
%48 = load i32, i32* %10
%49 = call i32 @close(i32 %48)
br label %54
50: ; preds = %39
%51 = load i64, i64* @errno
%52 = call i32 @strerror(i64 %51)
%53 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %50, %47
br label %24
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"echo\00"
@.str.1 = [24 x i8] c"usage: TCPechod [port]\0A\00"
@SIGCHLD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [12 x i8] c"accept: %s\0A\00"
@.str.3 = [10 x i8] c"fork: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
switch i32 %0, label %10 [
i32 1, label %12
i32 2, label %7
]
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
br label %12
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7, %2
%13 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %10 ], [ %9, %7 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ]
%14 = tail call i32 @passiveTCP(i8* %13, i32 32)
%15 = load i32, i32* @SIGCHLD
%16 = tail call i32 @signal(i32 %15, i32 32)
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %18
18: ; preds = %36, %12
store i32 4, i32* %4
%19 = call i32 @accept(i32 %14, %struct.sockaddr* %17, i32* %4)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %36, label %25
25: ; preds = %21
%26 = call i32 @strerror(i64 %22)
%27 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %18
%29 = call i32 (...) @fork()
switch i32 %29, label %34 [
i32 0, label %30
i32 -1, label %37
]
30: ; preds = %28
%31 = call i32 @close(i32 %14)
%32 = call i32 @TCPechod(i32 %19)
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %28
%35 = call i32 @close(i32 %19)
br label %36
36: ; preds = %34, %37, %21
br label %18
37: ; preds = %28
%38 = load i64, i64* @errno
%39 = call i32 @strerror(i64 %38)
%40 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %36
}