#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sin1;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int getpid();
int /*<<< orphan*/ htons(int);
int open(char *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int s, fd_null;
  struct sockaddr_in sin1;
  socklen_t sinlen;
  sin1.sin_family = AF_INET;
  sin1.sin_port = htons((getpid() % 32768) + 11000);
  sin1.sin_addr.s_addr = INADDR_ANY;
  sinlen = sizeof(sin1);
  fd_null = open("/dev/null", O_WRONLY);
  accept(-1, (struct sockaddr *)&sin1, &sinlen);
  accept(fd_null, (struct sockaddr *)&sin1, &sinlen);
  s = socket(PF_INET, SOCK_STREAM, 0);
  bind(s, (struct sockaddr *)&sin1, sizeof(sin1));
  accept(s, (struct sockaddr *)-1, &sinlen);
  accept(s, (struct sockaddr *)&sin1, (socklen_t *)-1);
  close(s);
  close(fd_null);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    $0x8000,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x2af8,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0xc,-0x1c(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe33(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
lea    -0x18(%rbp),%rsi
mov    $0xffffffff,%edi
lea    -0x1c(%rbp),%rdx
call   1080 <accept@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <accept@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    -0x8(%rbp),%edi
mov    $0xffffffffffffffff,%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <accept@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xffffffffffffffff,%rdx
call   1080 <accept@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0x7fff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffff8000,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x2af8,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movl   $0xc,0x4(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe28(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
mov    $0xffffffff,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <accept@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <accept@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %ebp,%edi
mov    $0xffffffffffffffff,%rsi
mov    %r15,%rdx
call   1080 <accept@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xffffffffffffffff,%rdx
call   1080 <accept@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %6, i32* %7
%8 = call i32 (...) @getpid()
%9 = srem i32 %8, 32768
%10 = add nsw i32 %9, 11000
%11 = call i32 @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
store i32 12, i32* %5
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %3
%18 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%19 = call i32 @accept(i32 -1, %struct.sockaddr* %18, i32* %5)
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i32 @accept(i32 %20, %struct.sockaddr* %21, i32* %5)
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = load i32, i32* %2
%30 = call i32 @accept(i32 %29, %struct.sockaddr* inttoptr (i64 -1 to %struct.sockaddr*), i32* %5)
%31 = load i32, i32* %2
%32 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%33 = call i32 @accept(i32 %31, %struct.sockaddr* %32, i32* inttoptr (i64 -1 to i32*))
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 (...) @getpid()
%8 = srem i32 %7, 32768
%9 = add nsw i32 %8, 11000
%10 = tail call i32 @htons(i32 %9)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %12, i32* %13
store i32 12, i32* %2
%14 = load i32, i32* @O_WRONLY
%15 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%17 = call i32 @accept(i32 -1, %struct.sockaddr* %16, i32* %2)
%18 = call i32 @accept(i32 %15, %struct.sockaddr* %16, i32* %2)
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = call i32 @bind(i32 %21, %struct.sockaddr* %16, i32 12)
%23 = call i32 @accept(i32 %21, %struct.sockaddr* inttoptr (i64 -1 to %struct.sockaddr*), i32* %2)
%24 = call i32 @accept(i32 %21, %struct.sockaddr* %16, i32* inttoptr (i64 -1 to i32*))
%25 = call i32 @close(i32 %21)
%26 = call i32 @close(i32 %15)
ret i32 0
}