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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ PF_INET;
int SHUT_RD;
int SHUT_RDWR;
int SHUT_WR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int getpid();
int /*<<< orphan*/ htons(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  char buf[1024];
  int s;
  int fd_null;
  struct sockaddr_in sin1;
  sin1.sin_family = AF_INET;
  sin1.sin_port = htons((getpid() % 32768) + 10000);
  sin1.sin_addr.s_addr = INADDR_ANY;
  fd_null = open("/dev/null", O_WRONLY);
  shutdown(-1, SHUT_RD);
  shutdown(fd_null, SHUT_WR);
  s = socket(PF_INET, SOCK_STREAM, 0);
  shutdown(s, SHUT_RDWR);
  close(s);
  s = socket(PF_INET, SOCK_STREAM, 0);
  shutdown(s, -1);
  close(s);
  close(fd_null);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x420(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    $0x8000,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x2710,%edi
call   1050 <htons@plt>
mov    %eax,-0x424(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x418(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
call   1040 <shutdown@plt>
mov    -0x418(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    -0x414(%rbp),%edi
call   1060 <close@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%edi
mov    $0xffffffff,%esi
call   1040 <shutdown@plt>
mov    -0x414(%rbp),%edi
call   1060 <close@plt>
mov    -0x418(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
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    $0x2710,%edi
call   1050 <htons@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
call   1040 <shutdown@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
mov    0x2df0(%rip),%r14
mov    (%r14),%edi
mov    0x2dee(%rip),%r15
mov    (%r15),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <shutdown@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%r14),%edi
mov    (%r15),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0xffffffff,%esi
call   1040 <shutdown@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%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 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@SHUT_RD = global i32 0
@SHUT_WR = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SHUT_RDWR = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %6, i32* %7
%8 = call i32 (...) @getpid()
%9 = srem i32 %8, 32768
%10 = add nsw i32 %9, 10000
%11 = call i32 @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%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* %4
%18 = load i32, i32* @SHUT_RD
%19 = call i32 @shutdown(i32 -1, i32 %18)
%20 = load i32, i32* %4
%21 = load i32, i32* @SHUT_WR
%22 = call i32 @shutdown(i32 %20, i32 %21)
%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* %3
%26 = load i32, i32* %3
%27 = load i32, i32* @SHUT_RDWR
%28 = call i32 @shutdown(i32 %26, i32 %27)
%29 = load i32, i32* %3
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @PF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = call i32 @shutdown(i32 %34, i32 -1)
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
ret i32 0
}

LLVM IR (-O2):
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@SHUT_RD = global i32 0
@SHUT_WR = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SHUT_RDWR = global i32 0
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = srem i32 %1, 32768
%3 = add nsw i32 %2, 10000
%4 = tail call i32 @htons(i32 %3)
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @SHUT_RD
%8 = tail call i32 @shutdown(i32 -1, i32 %7)
%9 = load i32, i32* @SHUT_WR
%10 = tail call i32 @shutdown(i32 %6, i32 %9)
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = load i32, i32* @SHUT_RDWR
%15 = tail call i32 @shutdown(i32 %13, i32 %14)
%16 = tail call i32 @close(i32 %13)
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = tail call i32 @shutdown(i32 %19, i32 -1)
%21 = tail call i32 @close(i32 %19)
%22 = tail call i32 @close(i32 %6)
ret i32 0
}