#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open_port(int, int);
int /*<<< orphan*/ stderr;
int main(void) {
  int listenfd;
  int fd1, fd2;
  fprintf(stderr, "Opening 127.0.0.1:%d\n", 9999);
  listenfd = open_port(0, 0);
  if (listenfd < 0)
    error(1, errno, "Couldn't open listen socket");
  fprintf(stderr, "Opening INADDR_ANY:%d\n", 9999);
  fd1 = open_port(0, 1);
  if (fd1 >= 0)
    error(1, 0,
          "Was allowed to create an ipv4 reuseport on a already bound "
          "non-reuseport socket");
  fprintf(stderr, "Opening in6addr_any:%d\n", 9999);
  fd1 = open_port(1, 1);
  if (fd1 < 0)
    error(1, errno, "Couldn't open ipv6 reuseport");
  fprintf(stderr, "Opening INADDR_ANY:%d\n", 9999);
  fd2 = open_port(0, 1);
  if (fd2 >= 0)
    error(1, 0,
          "Was allowed to create an ipv4 reuseport on a already bound "
          "non-reuseport socket");
  close(fd1);
  fprintf(stderr, "Opening INADDR_ANY:%d after closing ipv6 socket\n", 9999);
  fd1 = open_port(0, 1);
  if (fd1 >= 0)
    error(1, 0,
          "Was allowed to create an ipv4 reuseport on an already bound "
          "non-reuseport socket with no ipv6");
  fprintf(stderr, "Success");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
lea    0xea1(%rip),%rsi
mov    $0x270f,%edx
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1060 <open_port@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119b <main+0x5b>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe80(%rip),%rdx
call   1050 <error@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
mov    $0x270f,%edx
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <open_port@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11e3 <main+0xa3>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe6b(%rip),%rdx
call   1050 <error@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xea6(%rip),%rsi
mov    $0x270f,%edx
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%esi
mov    %esi,%edi
call   1060 <open_port@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1232 <main+0xf2>
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe84(%rip),%rdx
call   1050 <error@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    $0x270f,%edx
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <open_port@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     127a <main+0x13a>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xdd4(%rip),%rdx
call   1050 <error@plt>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
mov    $0x270f,%edx
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <open_port@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     12ca <main+0x18a>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe3a(%rip),%rdx
call   1050 <error@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e95(%rip),%r14
mov    (%r14),%edi
lea    0xeab(%rip),%rsi
mov    $0x270f,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <open_port@plt>
test   %eax,%eax
jns    1188 <main+0x48>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdx
mov    $0x1,%edi
call   1050 <error@plt>
mov    (%r14),%edi
lea    0xea0(%rip),%rsi
mov    $0x270f,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <open_port@plt>
test   %eax,%eax
js     11c1 <main+0x81>
lea    0xe94(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1050 <error@plt>
mov    (%r14),%edi
lea    0xece(%rip),%rsi
mov    $0x270f,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
mov    $0x1,%esi
call   1060 <open_port@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1206 <main+0xc6>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0xeb5(%rip),%rdx
mov    $0x1,%edi
call   1050 <error@plt>
mov    (%r14),%edi
lea    0xe22(%rip),%rsi
mov    $0x270f,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <open_port@plt>
test   %eax,%eax
js     123f <main+0xff>
lea    0xe16(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1050 <error@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    (%r14),%edi
lea    0xe7e(%rip),%rsi
mov    $0x270f,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <open_port@plt>
test   %eax,%eax
js     127f <main+0x13f>
lea    0xe8c(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1050 <error@plt>
mov    (%r14),%edi
lea    0xed4(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Opening 127.0.0.1:%d\0A\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"Couldn't open listen socket\00"
@.str.2 = [23 x i8] c"Opening INADDR_ANY:%d\0A\00"
@.str.3 = [80 x i8] c"Was allowed to create an ipv4 reuseport on a already bound non-reuseport socket\00"
@.str.4 = [24 x i8] c"Opening in6addr_any:%d\0A\00"
@.str.5 = [29 x i8] c"Couldn't open ipv6 reuseport\00"
@.str.6 = [49 x i8] c"Opening INADDR_ANY:%d after closing ipv6 socket\0A\00"
@.str.7 = [94 x i8] c"Was allowed to create an ipv4 reuseport on an already bound non-reuseport socket with no ipv6\00"
@.str.8 = [8 x i8] c"Success\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @stderr
%6 = call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 9999)
%7 = call i32 @open_port(i32 0, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = call i32 @error(i32 1, i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %10, %0
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 9999)
%16 = call i32 @open_port(i32 0, i32 1)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 9999)
%24 = call i32 @open_port(i32 1, i32 1)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = call i32 @error(i32 1, i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %27, %21
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 9999)
%33 = call i32 @open_port(i32 0, i32 1)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %30
%39 = load i32, i32* %3
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i32 9999)
%43 = call i32 @open_port(i32 0, i32 1)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp sge i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %38
%47 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str.7, i64 0, i64 0))
br label %48
48: ; preds = %46, %38
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Opening 127.0.0.1:%d\0A\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"Couldn't open listen socket\00"
@.str.2 = [23 x i8] c"Opening INADDR_ANY:%d\0A\00"
@.str.3 = [80 x i8] c"Was allowed to create an ipv4 reuseport on a already bound non-reuseport socket\00"
@.str.4 = [24 x i8] c"Opening in6addr_any:%d\0A\00"
@.str.5 = [29 x i8] c"Couldn't open ipv6 reuseport\00"
@.str.6 = [49 x i8] c"Opening INADDR_ANY:%d after closing ipv6 socket\0A\00"
@.str.7 = [94 x i8] c"Was allowed to create an ipv4 reuseport on an already bound non-reuseport socket with no ipv6\00"
@.str.8 = [8 x i8] c"Success\00"
define i32 @main() {
%1 = load i32, i32* @stderr
%2 = tail call i32 (i32, i8*, ...) @fprintf(i32 %1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 9999)
%3 = tail call i32 @open_port(i32 0, i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 @error(i32 1, i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %5, %0
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 9999)
%11 = tail call i32 @open_port(i32 0, i32 1)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 9999)
%18 = tail call i32 @open_port(i32 1, i32 1)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @errno
%22 = tail call i32 @error(i32 1, i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %20, %15
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 9999)
%26 = tail call i32 @open_port(i32 0, i32 1)
%27 = icmp sgt i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = tail call i32 @close(i32 %18)
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i32 9999)
%34 = tail call i32 @open_port(i32 0, i32 1)
%35 = icmp sgt i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str.7, i64 0, i64 0))
br label %38
38: ; preds = %36, %30
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}