#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;
      }
    }
    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    12cc <rootprocess+0x14c>
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    12cc <rootprocess+0x14c>
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    12cc <rootprocess+0x14c>
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     12b6 <rootprocess+0x136>
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>
jmp    12b6 <rootprocess+0x136>
mov    -0x4(%rbp),%edi
mov    0x2d00(%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    129e <rootprocess+0x11e>
mov    0x2e1c(%rip),%r12
mov    0x2e2d(%rip),%r14
mov    %rsp,%r15
mov    0x2deb(%rip),%r13
jmp    11ea <rootprocess+0x6a>
nop
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <send_err@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x4,%eax
jne    129e <rootprocess+0x11e>
cmpl   $0x80,(%rsp)
jne    1280 <rootprocess+0x100>
mov    $0x80,%edi
call   1050 <is_allowed_port@plt>
test   %eax,%eax
je     1286 <rootprocess+0x106>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%esi
mov    (%r14),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     11c0 <rootprocess+0x40>
mov    %eax,%ebp
movl   $0x1,0x4(%rsp)
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
mov    0x2d73(%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
jne    11c0 <rootprocess+0x40>
mov    %ebx,%edi
mov    %ebp,%esi
call   1060 <send_fd@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    11d0 <rootprocess+0x50>
xchg   %ax,%ax
mov    0x0(%r13),%esi
jmp    128f <rootprocess+0x10f>
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <send_err@plt>
test   %eax,%eax
je     11d0 <rootprocess+0x50>
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 = %64, %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 %68
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 %68
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 %68
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 %64
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
br label %64
64: ; preds = %63, %40
%65 = load i32, i32* %2
%66 = load i32, i32* @errno
%67 = call i32 @send_err(i32 %65, i32 %66)
br label %8
68: ; 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 %48
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, %45
%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 %45, label %48
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 %42, 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 %45, label %48
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 %45
41: ; preds = %32
br label %42
42: ; preds = %41, %22
%43 = load i32, i32* @errno
%44 = call i32 @send_err(i32 %0, i32 %43)
br label %45
45: ; preds = %28, %18, %38, %42
%46 = call i32 @recv(i32 %0, %struct.sockaddr_storage* %5, i32 4, i32 0)
%47 = icmp eq i32 %46, 4
br i1 %47, label %12, label %48
48: ; preds = %45, %18, %28, %1
ret void
}