#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int getsockname(int, struct sockaddr *, int *);
int listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  if (argc != 5) {
    printf("usage: %s nsockets tcp|udp listen|nolisten reuseaddr|noreuseaddr\n",
           argv[0]);
    return 1;
  }
  int num_sockets = atoi(argv[1]);
  int enable_tcp = strcmp(argv[2], "tcp") == 0;
  int enable_listen = strcmp(argv[3], "listen") == 0;
  int enable_reuse = strcmp(argv[4], "reuseaddr") == 0;
  if (num_sockets > 10000) {
    num_sockets = 10000;
  }
  int conflicts = 0;
  int port_array[10000] = {};
  int i, j;
  printf("protocol = %s\n", enable_tcp ? "tcp" : "udp");
  printf("listen = %s\n", enable_listen ? "yes" : "no");
  printf("reuseaddr = %s\n", enable_reuse ? "yes" : "no");
  for (i = 0; i < num_sockets; i++) {
    int sock;
    if (enable_tcp) {
      sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    } else {
      sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    }
    if (!(sock != -1)) {
      perror("sock != -1");
      exit(1);
    };
    if (enable_reuse) {
      int yes = 1;
      if (!(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) !=
            -1)) {
        perror("setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) "
               "!= -1");
        exit(1);
      };
    }
    struct sockaddr_in addr = {};
    addr.sin_family = AF_INET;
    if (!(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != -1)) {
      perror("bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != -1");
      exit(1);
    };
    if (enable_listen) {
      if (!(listen(sock, 1) != -1)) {
        perror("listen(sock, 1) != -1");
        exit(1);
      };
    }
    socklen_t addrlen = sizeof(addr);
    if (!(getsockname(sock, (struct sockaddr *)&addr, &addrlen) != -1)) {
      perror("getsockname(sock, (struct sockaddr*)&addr, &addrlen) != -1");
      exit(1);
    };
    port_array[i] = ntohs(addr.sin_port);
    for (j = 0; j < i; j++) {
      if (port_array[j] == port_array[i]) {
        printf("conflict: port[%04d] = port[%04d] = %d\n", j, i, port_array[i]);
        conflicts++;
      }
    }
  }
  printf("conflicts = %d\n", conflicts);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x9c90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     1204 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15c2 <main+0x402>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe1f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe00(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
lea    0xde4(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x2710,-0x14(%rbp)
jle    1291 <main+0xd1>
movl   $0x2710,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x9c70(%rbp),%rdi
xor    %esi,%esi
mov    $0x9c40,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%ecx
lea    0xdb1(%rip),%rsi
lea    0xd86(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%ecx
lea    0xda0(%rip),%rsi
lea    0xd95(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%ecx
lea    0xd7a(%rip),%rsi
lea    0xd6f(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x9c74(%rbp)
mov    -0x9c74(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    15aa <main+0x3ea>
cmpl   $0x0,-0x18(%rbp)
je     136b <main+0x1ab>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
mov    0x2c70(%rip),%rax
mov    (%rax),%esi
mov    0x2c7f(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x9c7c(%rbp)
jmp    1391 <main+0x1d1>
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
mov    0x2c55(%rip),%rax
mov    (%rax),%esi
mov    0x2c24(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x9c7c(%rbp)
cmpl   $0xffffffff,-0x9c7c(%rbp)
jne    13b4 <main+0x1f4>
lea    0xce9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
je     1416 <main+0x256>
movl   $0x1,-0x9c80(%rbp)
mov    -0x9c7c(%rbp),%edi
mov    0x2c13(%rip),%rax
mov    (%rax),%esi
mov    0x2bda(%rip),%rax
mov    (%rax),%edx
lea    -0x9c80(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1411 <main+0x251>
lea    0xc97(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    1416 <main+0x256>
lea    -0x9c88(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2bb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x9c84(%rbp)
mov    -0x9c7c(%rbp),%edi
lea    -0x9c88(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    146e <main+0x2ae>
lea    0xc7e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     14ac <main+0x2ec>
mov    -0x9c7c(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    14a7 <main+0x2e7>
lea    0xc7d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    14ac <main+0x2ec>
movl   $0x8,-0x9c8c(%rbp)
mov    -0x9c7c(%rbp),%edi
lea    -0x9c88(%rbp),%rsi
lea    -0x9c8c(%rbp),%rdx
call   10b0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    14ee <main+0x32e>
lea    0xc4c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x9c88(%rbp),%edi
call   1080 <ntohs@plt>
mov    %eax,%ecx
movslq -0x9c74(%rbp),%rax
mov    %ecx,-0x9c70(%rbp,%rax,4)
movl   $0x0,-0x9c78(%rbp)
mov    -0x9c78(%rbp),%eax
cmp    -0x9c74(%rbp),%eax
jge    1591 <main+0x3d1>
movslq -0x9c78(%rbp),%rax
mov    -0x9c70(%rbp,%rax,4),%eax
movslq -0x9c74(%rbp),%rcx
cmp    -0x9c70(%rbp,%rcx,4),%eax
jne    1578 <main+0x3b8>
mov    -0x9c78(%rbp),%esi
mov    -0x9c74(%rbp),%edx
movslq -0x9c74(%rbp),%rax
mov    -0x9c70(%rbp,%rax,4),%ecx
lea    0xbfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    157d <main+0x3bd>
mov    -0x9c78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c78(%rbp)
jmp    1513 <main+0x353>
jmp    1596 <main+0x3d6>
mov    -0x9c74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c74(%rbp)
jmp    1327 <main+0x167>
mov    -0x24(%rbp),%esi
lea    0xbda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x9c90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x9c78,%rsp
mov    %rsi,%rbp
cmp    $0x5,%edi
jne    141a <main+0x25a>
mov    0x8(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r15d
mov    0x10(%rbp),%rdi
lea    0xe4e(%rip),%r14
mov    %r14,%rsi
call   1060 <strcmp@plt>
mov    %rax,%rbx
mov    0x18(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1060 <strcmp@plt>
mov    %rax,%r12
mov    0x20(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1060 <strcmp@plt>
mov    %rax,%r13
lea    0x30(%rsp),%rdi
mov    $0x9c40,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,0x28(%rsp)
test   %rbx,%rbx
lea    0xe21(%rip),%rsi
cmove  %r14,%rsi
lea    0xe07(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,0x18(%rsp)
test   %r12,%r12
lea    0xe11(%rip),%rbx
lea    0xe0e(%rip),%rbp
mov    %rbp,%rsi
cmove  %rbx,%rsi
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,0x20(%rsp)
test   %r13,%r13
cmove  %rbx,%rbp
lea    0xde9(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x0,%r13d
test   %r15d,%r15d
jle    1433 <main+0x273>
cmp    $0x2710,%r15d
mov    $0x2710,%eax
cmovb  %r15d,%eax
mov    %rax,0x10(%rsp)
mov    0x2d16(%rip),%rbx
mov    %rsp,%rbp
lea    0xe92(%rip),%r14
xor    %r15d,%r15d
xor    %r13d,%r13d
jmp    12f5 <main+0x135>
nopl   0x0(%rax)
add    $0x1,%r15
cmp    0x10(%rsp),%r15
mov    %r12,%rbx
mov    %rsp,%rbp
je     1433 <main+0x273>
cmpq   $0x0,0x28(%rsp)
mov    (%rbx),%edi
mov    0x2ccc(%rip),%rax
cmove  0x2cb4(%rip),%rax
mov    (%rax),%esi
mov    0x2c93(%rip),%rax
cmove  0x2cbb(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     1458 <main+0x298>
mov    %eax,%r12d
cmpq   $0x0,0x20(%rsp)
jne    136b <main+0x1ab>
movl   $0x1,(%rsp)
mov    0x2ca2(%rip),%rax
mov    (%rax),%esi
mov    0x2c69(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %rbp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1473 <main+0x2b3>
movq   $0x0,(%rsp)
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1461 <main+0x2a1>
cmpq   $0x0,0x18(%rsp)
jne    13b0 <main+0x1f0>
mov    %r12d,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     147c <main+0x2bc>
movl   $0x8,0xc(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
lea    0xc(%rsp),%rdx
call   10b0 <getsockname@plt>
cmp    $0xffffffff,%eax
je     146a <main+0x2aa>
mov    %rbx,%r12
mov    (%rsp),%edi
call   1080 <ntohs@plt>
mov    %eax,0x30(%rsp,%r15,4)
test   %r15,%r15
je     12e0 <main+0x120>
mov    %eax,%ebx
xor    %ebp,%ebp
jmp    13fd <main+0x23d>
add    $0x1,%rbp
cmp    %rbp,%r15
je     12e0 <main+0x120>
cmp    %ebx,0x30(%rsp,%rbp,4)
jne    13f0 <main+0x230>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r13d
jmp    13f0 <main+0x230>
mov    0x0(%rbp),%rsi
lea    0xbdb(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
jmp    1446 <main+0x286>
lea    0xd54(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x9c78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc2f(%rip),%rdi
jmp    1483 <main+0x2c3>
lea    0xc75(%rip),%rdi
jmp    1483 <main+0x2c3>
lea    0xcba(%rip),%rdi
jmp    1483 <main+0x2c3>
lea    0xc1f(%rip),%rdi
jmp    1483 <main+0x2c3>
lea    0xc92(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [66 x i8] c"usage: %s nsockets tcp|udp listen|nolisten reuseaddr|noreuseaddr\0A\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [10 x i8] c"reuseaddr\00"
@.str.4 = [15 x i8] c"protocol = %s\0A\00"
@.str.5 = [4 x i8] c"udp\00"
@.str.6 = [13 x i8] c"listen = %s\0A\00"
@.str.7 = [4 x i8] c"yes\00"
@.str.8 = [3 x i8] c"no\00"
@.str.9 = [16 x i8] c"reuseaddr = %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.10 = [11 x i8] c"sock != -1\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.11 = [68 x i8] c"setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != -1\00"
@.str.12 = [56 x i8] c"bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != -1\00"
@.str.13 = [22 x i8] c"listen(sock, 1) != -1\00"
@.str.14 = [59 x i8] c"getsockname(sock, (struct sockaddr*)&addr, &addrlen) != -1\00"
@.str.15 = [40 x i8] c"conflict: port[%04d] = port[%04d] = %d\0A\00"
@.str.16 = [16 x i8] c"conflicts = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [10000 x i32]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.sockaddr_in
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 5
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 1, i32* %3
br label %176
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %6
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
%35 = zext i1 %34 to i32
store i32 %35, i32* %7
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 3
%38 = load i8*, i8** %37
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
%41 = zext i1 %40 to i32
store i32 %41, i32* %8
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 4
%44 = load i8*, i8** %43
%45 = call i64 @strcmp(i8* %44, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
%47 = zext i1 %46 to i32
store i32 %47, i32* %9
%48 = load i32, i32* %6
%49 = icmp sgt i32 %48, 10000
br i1 %49, label %50, label %51
50: ; preds = %25
store i32 10000, i32* %6
br label %51
51: ; preds = %50, %25
store i32 0, i32* %10
%52 = bitcast [10000 x i32]* %11 to i8*
%53 = load i32, i32* %7
%54 = icmp ne i32 %53, 0
%55 = zext i1 %54 to i64
%56 = select i1 %54, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %56)
%58 = load i32, i32* %8
%59 = icmp ne i32 %58, 0
%60 = zext i1 %59 to i64
%61 = select i1 %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %61)
%63 = load i32, i32* %9
%64 = icmp ne i32 %63, 0
%65 = zext i1 %64 to i64
%66 = select i1 %64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i8* %66)
store i32 0, i32* %12
br label %68
68: ; preds = %170, %51
%69 = load i32, i32* %12
%70 = load i32, i32* %6
%71 = icmp slt i32 %69, %70
br i1 %71, label %72, label %173
72: ; preds = %68
%73 = load i32, i32* %7
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %72
%76 = load i32, i32* @AF_INET
%77 = load i32, i32* @SOCK_STREAM
%78 = load i32, i32* @IPPROTO_TCP
%79 = call i32 @socket(i32 %76, i32 %77, i32 %78)
store i32 %79, i32* %14
br label %85
80: ; preds = %72
%81 = load i32, i32* @AF_INET
%82 = load i32, i32* @SOCK_DGRAM
%83 = load i32, i32* @IPPROTO_UDP
%84 = call i32 @socket(i32 %81, i32 %82, i32 %83)
store i32 %84, i32* %14
br label %85
85: ; preds = %80, %75
%86 = load i32, i32* %14
%87 = icmp ne i32 %86, -1
br i1 %87, label %91, label %88
88: ; preds = %85
%89 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.10, i64 0, i64 0))
%90 = call i32 @exit(i32 1)
unreachable
91: ; preds = %85
%92 = load i32, i32* %9
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %104
94: ; preds = %91
store i32 1, i32* %15
%95 = load i32, i32* %14
%96 = load i32, i32* @SOL_SOCKET
%97 = load i32, i32* @SO_REUSEADDR
%98 = call i32 @setsockopt(i32 %95, i32 %96, i32 %97, i32* %15, i32 4)
%99 = icmp ne i32 %98, -1
br i1 %99, label %103, label %100
100: ; preds = %94
%101 = call i32 @perror(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.11, i64 0, i64 0))
%102 = call i32 @exit(i32 1)
unreachable
103: ; preds = %94
br label %104
104: ; preds = %103, %91
%105 = bitcast %struct.sockaddr_in* %16 to i8*
%106 = load i32, i32* @AF_INET
%107 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 1
store i32 %106, i32* %107
%108 = load i32, i32* %14
%109 = bitcast %struct.sockaddr_in* %16 to %struct.sockaddr*
%110 = call i32 @bind(i32 %108, %struct.sockaddr* %109, i32 8)
%111 = icmp ne i32 %110, -1
br i1 %111, label %115, label %112
112: ; preds = %104
%113 = call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.12, i64 0, i64 0))
%114 = call i32 @exit(i32 1)
unreachable
115: ; preds = %104
%116 = load i32, i32* %8
%117 = icmp ne i32 %116, 0
br i1 %117, label %118, label %126
118: ; preds = %115
%119 = load i32, i32* %14
%120 = call i32 @listen(i32 %119, i32 1)
%121 = icmp ne i32 %120, -1
br i1 %121, label %125, label %122
122: ; preds = %118
%123 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.13, i64 0, i64 0))
%124 = call i32 @exit(i32 1)
unreachable
125: ; preds = %118
br label %126
126: ; preds = %125, %115
store i32 8, i32* %17
%127 = load i32, i32* %14
%128 = bitcast %struct.sockaddr_in* %16 to %struct.sockaddr*
%129 = call i32 @getsockname(i32 %127, %struct.sockaddr* %128, i32* %17)
%130 = icmp ne i32 %129, -1
br i1 %130, label %134, label %131
131: ; preds = %126
%132 = call i32 @perror(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.14, i64 0, i64 0))
%133 = call i32 @exit(i32 1)
unreachable
134: ; preds = %126
%135 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 0
%136 = load i32, i32* %135
%137 = call i32 @ntohs(i32 %136)
%138 = load i32, i32* %12
%139 = sext i32 %138 to i64
%140 = getelementptr inbounds [10000 x i32], [10000 x i32]* %11, i64 0, i64 %139
store i32 %137, i32* %140
store i32 0, i32* %13
br label %141
141: ; preds = %166, %134
%142 = load i32, i32* %13
%143 = load i32, i32* %12
%144 = icmp slt i32 %142, %143
br i1 %144, label %145, label %169
145: ; preds = %141
%146 = load i32, i32* %13
%147 = sext i32 %146 to i64
%148 = getelementptr inbounds [10000 x i32], [10000 x i32]* %11, i64 0, i64 %147
%149 = load i32, i32* %148
%150 = load i32, i32* %12
%151 = sext i32 %150 to i64
%152 = getelementptr inbounds [10000 x i32], [10000 x i32]* %11, i64 0, i64 %151
%153 = load i32, i32* %152
%154 = icmp eq i32 %149, %153
br i1 %154, label %155, label %165
155: ; preds = %145
%156 = load i32, i32* %13
%157 = load i32, i32* %12
%158 = load i32, i32* %12
%159 = sext i32 %158 to i64
%160 = getelementptr inbounds [10000 x i32], [10000 x i32]* %11, i64 0, i64 %159
%161 = load i32, i32* %160
%162 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.15, i64 0, i64 0), i32 %156, i32 %157, i32 %161)
%163 = load i32, i32* %10
%164 = add nsw i32 %163, 1
store i32 %164, i32* %10
br label %165
165: ; preds = %155, %145
br label %166
166: ; preds = %165
%167 = load i32, i32* %13
%168 = add nsw i32 %167, 1
store i32 %168, i32* %13
br label %141
169: ; preds = %141
br label %170
170: ; preds = %169
%171 = load i32, i32* %12
%172 = add nsw i32 %171, 1
store i32 %172, i32* %12
br label %68
173: ; preds = %68
%174 = load i32, i32* %10
%175 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.16, i64 0, i64 0), i32 %174)
store i32 0, i32* %3
br label %176
176: ; preds = %173, %20
%177 = load i32, i32* %3
ret i32 %177
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [66 x i8] c"usage: %s nsockets tcp|udp listen|nolisten reuseaddr|noreuseaddr\0A\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [10 x i8] c"reuseaddr\00"
@.str.4 = [15 x i8] c"protocol = %s\0A\00"
@.str.5 = [4 x i8] c"udp\00"
@.str.6 = [13 x i8] c"listen = %s\0A\00"
@.str.7 = [4 x i8] c"yes\00"
@.str.8 = [3 x i8] c"no\00"
@.str.9 = [16 x i8] c"reuseaddr = %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.10 = [11 x i8] c"sock != -1\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.11 = [68 x i8] c"setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != -1\00"
@.str.12 = [56 x i8] c"bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != -1\00"
@.str.13 = [22 x i8] c"listen(sock, 1) != -1\00"
@.str.14 = [59 x i8] c"getsockname(sock, (struct sockaddr*)&addr, &addrlen) != -1\00"
@.str.15 = [40 x i8] c"conflict: port[%04d] = port[%04d] = %d\0A\00"
@.str.16 = [16 x i8] c"conflicts = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10000 x i32]
%4 = alloca i32
%5 = alloca i64
%6 = bitcast i64* %5 to %struct.sockaddr_in*
%7 = alloca i32
%8 = icmp eq i32 %0, 5
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %119
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
%20 = getelementptr inbounds i8*, i8** %1, i64 3
%21 = load i8*, i8** %20
%22 = tail call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
%24 = getelementptr inbounds i8*, i8** %1, i64 4
%25 = load i8*, i8** %24
%26 = tail call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
%28 = bitcast [10000 x i32]* %3 to i8*
%29 = select i1 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = select i1 %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %31)
%33 = select i1 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i8* %33)
%35 = icmp sgt i32 %15, 0
br i1 %35, label %36, label %116
36: ; preds = %12
%37 = icmp ult i32 %15, 10000
%38 = select i1 %37, i32 %15, i32 10000
%39 = bitcast i32* %4 to i8*
%40 = bitcast i64* %5 to i8*
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%42 = bitcast i64* %5 to %struct.sockaddr*
%43 = bitcast i32* %7 to i8*
%44 = bitcast i64* %5 to i32*
%45 = zext i32 %38 to i64
br label %46
46: ; preds = %36, %112
%47 = phi i64 [ 0, %36 ], [ %114, %112 ]
%48 = phi i32 [ 0, %36 ], [ %113, %112 ]
%49 = load i32, i32* @AF_INET
%50 = load i32, i32* @SOCK_STREAM
%51 = load i32, i32* @SOCK_DGRAM
%52 = select i1 %19, i32 %50, i32 %51
%53 = load i32, i32* @IPPROTO_TCP
%54 = load i32, i32* @IPPROTO_UDP
%55 = select i1 %19, i32 %53, i32 %54
%56 = call i32 @socket(i32 %49, i32 %52, i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %46
%59 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.10, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %46
br i1 %27, label %62, label %71
62: ; preds = %61
store i32 1, i32* %4
%63 = load i32, i32* @SOL_SOCKET
%64 = load i32, i32* @SO_REUSEADDR
%65 = call i32 @setsockopt(i32 %56, i32 %63, i32 %64, i32* %4, i32 4)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = call i32 @perror(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.11, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %62
br label %71
71: ; preds = %70, %61
store i64 0, i64* %5
%72 = load i32, i32* @AF_INET
store i32 %72, i32* %41
%73 = call i32 @bind(i32 %56, %struct.sockaddr* %42, i32 8)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %78
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.12, i64 0, i64 0))
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %71
br i1 %23, label %79, label %85
79: ; preds = %78
%80 = call i32 @listen(i32 %56, i32 1)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.13, i64 0, i64 0))
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %79, %78
store i32 8, i32* %7
%86 = call i32 @getsockname(i32 %56, %struct.sockaddr* %42, i32* %7)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = call i32 @perror(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.14, i64 0, i64 0))
%90 = call i32 @exit(i32 1)
unreachable
91: ; preds = %85
%92 = load i32, i32* %44
%93 = call i32 @ntohs(i32 %92)
%94 = getelementptr inbounds [10000 x i32], [10000 x i32]* %3, i64 0, i64 %47
store i32 %93, i32* %94
%95 = icmp eq i64 %47, 0
br i1 %95, label %112, label %96
96: ; preds = %91
%97 = trunc i64 %47 to i32
br label %98
98: ; preds = %96, %108
%99 = phi i64 [ 0, %96 ], [ %110, %108 ]
%100 = phi i32 [ %48, %96 ], [ %109, %108 ]
%101 = getelementptr inbounds [10000 x i32], [10000 x i32]* %3, i64 0, i64 %99
%102 = load i32, i32* %101
%103 = icmp eq i32 %102, %93
br i1 %103, label %104, label %108
104: ; preds = %98
%105 = trunc i64 %99 to i32
%106 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.15, i64 0, i64 0), i32 %105, i32 %97, i32 %93)
%107 = add nsw i32 %100, 1
br label %108
108: ; preds = %98, %104
%109 = phi i32 [ %107, %104 ], [ %100, %98 ]
%110 = add nuw nsw i64 %99, 1
%111 = icmp eq i64 %110, %47
br i1 %111, label %112, label %98
112: ; preds = %108, %91
%113 = phi i32 [ %48, %91 ], [ %109, %108 ]
%114 = add nuw nsw i64 %47, 1
%115 = icmp eq i64 %114, %45
br i1 %115, label %116, label %46
116: ; preds = %112, %12
%117 = phi i32 [ 0, %12 ], [ %113, %112 ]
%118 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.16, i64 0, i64 0), i32 %117)
br label %119
119: ; preds = %116, %9
%120 = phi i32 [ 1, %9 ], [ 0, %116 ]
ret i32 %120
}