#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*/ val;
struct sockaddr_storage {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int sa_family;
};

/* Variables and functions */
#define AF_INET 128
int /*<<< orphan*/ EACCES;
int /*<<< orphan*/ EAFNOSUPPORT;
int /*<<< orphan*/ IPPROTO_TCP;
int PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, unsigned int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ is_allowed_port(int /*<<< orphan*/);
int recv(int, struct sockaddr_storage *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send_err(int, int /*<<< orphan*/);
int /*<<< orphan*/ send_fd(int, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int const *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
void rootprocess(int fd) {
  union {
    struct sockaddr sa;
    struct sockaddr_storage ss;
    struct sockaddr_in sin;
  } addr;
  while (recv(fd, &addr.ss, sizeof(addr.ss), 0) == sizeof(addr.ss)) {
    unsigned len;
    int sock;
    int family;
    switch (addr.sa.sa_family) {
    case AF_INET:
      if (!is_allowed_port(addr.sin.sin_port)) {
        if (send_err(fd, EACCES))
          return;
        continue;
      }
      len = sizeof(struct sockaddr_in);
      family = PF_INET;
      break;
    default:
      if (send_err(fd, EAFNOSUPPORT))
        return;
      continue;
    }
    sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
    if (sock != -1) {
      const int val = 1;
      setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
      if (bind(sock, &addr.sa, len) == 0) {
        send_fd(fd, sock);
        close(sock);
        continue;
      }
      close(sock);
    }
    send_err(fd, errno);
  }
}


x86 assembly (clang, -O0):
<rootprocess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
cmp    $0x4,%rax
jne    12cf <rootprocess+0x14f>
mov    -0x8(%rbp),%eax
sub    $0x80,%eax
jne    120a <rootprocess+0x8a>
jmp    11bd <rootprocess+0x3d>
mov    -0x8(%rbp),%edi
call   1050 <is_allowed_port@plt>
cmp    $0x0,%eax
jne    11f2 <rootprocess+0x72>
mov    -0x4(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1080 <send_err@plt>
cmp    $0x0,%eax
je     11ed <rootprocess+0x6d>
jmp    12cf <rootprocess+0x14f>
jmp    118b <rootprocess+0xb>
movl   $0x4,-0xc(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    122e <rootprocess+0xae>
mov    -0x4(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
call   1080 <send_err@plt>
cmp    $0x0,%eax
je     1229 <rootprocess+0xa9>
jmp    12cf <rootprocess+0x14f>
jmp    118b <rootprocess+0xb>
mov    -0x14(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     12b9 <rootprocess+0x139>
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jne    12b1 <rootprocess+0x131>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1060 <send_fd@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
jmp    118b <rootprocess+0xb>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%edi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
call   1080 <send_err@plt>
jmp    118b <rootprocess+0xb>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rootprocess>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x4,%eax
jne    129f <rootprocess+0x11f>
mov    0x2e1c(%rip),%r12
mov    0x2e2d(%rip),%r14
mov    %rsp,%r15
mov    0x2deb(%rip),%r13
jmp    11ed <rootprocess+0x6d>
nop
mov    0x0(%r13),%esi
mov    %ebx,%edi
call   1080 <send_err@plt>
test   %eax,%eax
jne    129f <rootprocess+0x11f>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x4,%eax
jne    129f <rootprocess+0x11f>
cmpl   $0x80,(%rsp)
jne    11c0 <rootprocess+0x40>
mov    $0x80,%edi
call   1050 <is_allowed_port@plt>
test   %eax,%eax
je     127c <rootprocess+0xfc>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%esi
mov    (%r14),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1267 <rootprocess+0xe7>
mov    %eax,%ebp
movl   $0x1,0x4(%rsp)
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
test   %rax,%rax
je     128a <rootprocess+0x10a>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <send_err@plt>
jmp    11d3 <rootprocess+0x53>
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
jmp    11c4 <rootprocess+0x44>
mov    %ebx,%edi
mov    %ebp,%esi
call   1060 <send_fd@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    11d3 <rootprocess+0x53>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.anon = type { %struct.sockaddr }
%struct.sockaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { i32 }
@EACCES = global i32 0
@PF_INET = global i32 0
@EAFNOSUPPORT = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define void @rootprocess(i32 %0) {
%2 = alloca i32
%3 = alloca %union.anon
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
br label %8
8: ; preds = %66, %57, %39, %30, %1
%9 = load i32, i32* %2
%10 = bitcast %union.anon* %3 to %struct.sockaddr_storage*
%11 = call i32 @recv(i32 %9, %struct.sockaddr_storage* %10, i32 4, i32 0)
%12 = sext i32 %11 to i64
%13 = icmp eq i64 %12, 4
br i1 %13, label %14, label %70
14: ; preds = %8
%15 = bitcast %union.anon* %3 to %struct.sockaddr*
%16 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %15, i32 0, i32 0
%17 = load i32, i32* %16
switch i32 %17, label %33 [
i32 128, label %18
]
18: ; preds = %14
%19 = bitcast %union.anon* %3 to %struct.sockaddr_in*
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @is_allowed_port(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %18
%25 = load i32, i32* %2
%26 = load i32, i32* @EACCES
%27 = call i32 @send_err(i32 %25, i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %70
30: ; preds = %24
br label %8
31: ; preds = %18
store i32 4, i32* %4
%32 = load i32, i32* @PF_INET
store i32 %32, i32* %6
br label %40
33: ; preds = %14
%34 = load i32, i32* %2
%35 = load i32, i32* @EAFNOSUPPORT
%36 = call i32 @send_err(i32 %34, i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %70
39: ; preds = %33
br label %8
40: ; preds = %31
%41 = load i32, i32* %6
%42 = load i32, i32* @SOCK_STREAM
%43 = load i32, i32* @IPPROTO_TCP
%44 = call i32 @socket(i32 %41, i32 %42, i32 %43)
store i32 %44, i32* %5
%45 = load i32, i32* %5
%46 = icmp ne i32 %45, -1
br i1 %46, label %47, label %66
47: ; preds = %40
store i32 1, i32* %7
%48 = load i32, i32* %5
%49 = load i32, i32* @SOL_SOCKET
%50 = load i32, i32* @SO_REUSEADDR
%51 = call i32 @setsockopt(i32 %48, i32 %49, i32 %50, i32* %7, i32 4)
%52 = load i32, i32* %5
%53 = bitcast %union.anon* %3 to %struct.sockaddr*
%54 = load i32, i32* %4
%55 = call i64 @bind(i32 %52, %struct.sockaddr* %53, i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %47
%58 = load i32, i32* %2
%59 = load i32, i32* %5
%60 = call i32 @send_fd(i32 %58, i32 %59)
%61 = load i32, i32* %5
%62 = call i32 @close(i32 %61)
br label %8
63: ; preds = %47
%64 = load i32, i32* %5
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %63, %40
%67 = load i32, i32* %2
%68 = load i32, i32* @errno
%69 = call i32 @send_err(i32 %67, i32 %68)
br label %8
70: ; preds = %29, %38, %8
ret void
}

LLVM IR (-O2):
%union.anon = type { %struct.sockaddr }
%struct.sockaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
@EACCES = global i32 0
@PF_INET = global i32 0
@EAFNOSUPPORT = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define void @rootprocess(i32 %0) {
%2 = alloca %union.anon
%3 = alloca i32
%4 = bitcast %union.anon* %2 to i8*
%5 = bitcast %union.anon* %2 to %struct.sockaddr_storage*
%6 = call i32 @recv(i32 %0, %struct.sockaddr_storage* %5, i32 4, i32 0)
%7 = icmp eq i32 %6, 4
br i1 %7, label %8, label %49
8: ; preds = %1
%9 = getelementptr inbounds %union.anon, %union.anon* %2, i64 0, i32 0
%10 = getelementptr inbounds %union.anon, %union.anon* %2, i64 0, i32 0, i32 0
%11 = bitcast i32* %3 to i8*
br label %12
12: ; preds = %8, %46
%13 = load i32, i32* %10
%14 = icmp eq i32 %13, 128
br i1 %14, label %15, label %28
15: ; preds = %12
%16 = call i32 @is_allowed_port(i32 128)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @EACCES
%20 = call i32 @send_err(i32 %0, i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %46, label %49
22: ; preds = %15
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = load i32, i32* @IPPROTO_TCP
%26 = call i32 @socket(i32 %23, i32 %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %43, label %32
28: ; preds = %12
%29 = load i32, i32* @EAFNOSUPPORT
%30 = call i32 @send_err(i32 %0, i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %46, label %49
32: ; preds = %22
store i32 1, i32* %3
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i32 @setsockopt(i32 %26, i32 %33, i32 %34, i32* %3, i32 4)
%36 = call i64 @bind(i32 %26, %struct.sockaddr* %9, i32 4)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = call i32 @send_fd(i32 %0, i32 %26)
%40 = call i32 @close(i32 %26)
br label %46
41: ; preds = %32
%42 = call i32 @close(i32 %26)
br label %43
43: ; preds = %41, %22
%44 = load i32, i32* @errno
%45 = call i32 @send_err(i32 %0, i32 %44)
br label %46
46: ; preds = %28, %18, %38, %43
%47 = call i32 @recv(i32 %0, %struct.sockaddr_storage* %5, i32 4, i32 0)
%48 = icmp eq i32 %47, 4
br i1 %48, label %12, label %49
49: ; preds = %46, %18, %28, %1
ret void
}