#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_nl {
  scalar_t__ nl_groups;
  int /*<<< orphan*/ nl_pid;
  int /*<<< orphan*/ nl_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ local;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_RAW;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ diep(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int *malloc(int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
size_t optind;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rand();
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int i;
  int *fds;
  int num_ports;
  struct sockaddr_nl local;
  srand(strtoul(argv[2], NULL, 0));
  if (argc < 3) {
    fprintf(stderr, "Usage: %s <PORTS> <SEED>\n", argv[0]);
    exit(1);
  }
  num_ports = atoi(argv[optind]);
  printf("Create %u ports\n", num_ports);
  fds = malloc(sizeof(int) * num_ports);
  for (i = 1; i <= num_ports; i++) {
    if (!(fds[i] = socket(PF_NETLINK, SOCK_RAW, 0)))
      diep("socket");
    memset(&local, 0, sizeof(local));
    local.nl_family = AF_NETLINK;
    local.nl_pid = rand();
    local.nl_groups = 0;
    if (bind(fds[i], (struct sockaddr *)&local, sizeof(local)) != 0) {
      diep("socket");
    }
    if (!(i % 1000))
      printf("Created %u ports\n", i);
  }
  printf("Ports successfully created, terminating\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    %eax,%edi
call   1050 <srand@plt>
cmpl   $0x3,-0x8(%rbp)
jge    1220 <main+0x60>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdef(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2db5(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     133f <main+0x17f>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rdx
mov    %eax,(%rcx,%rdx,4)
cmp    $0x0,%eax
jne    12aa <main+0xea>
lea    0xd86(%rip),%rdi
call   1060 <diep@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   10d0 <rand@plt>
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x38(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1307 <main+0x147>
lea    0xd29(%rip),%rdi
call   1060 <diep@plt>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    132c <main+0x16c>
mov    -0x14(%rbp),%esi
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1331 <main+0x171>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1265 <main+0xa5>
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x10(%rsi),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %eax,%edi
call   1060 <srand@plt>
cmp    $0x2,%ebp
jle    1332 <main+0x162>
mov    0x2dd9(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,8),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r15d
lea    0xdfd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x0(,%r15,4),%edi
call   1090 <malloc@plt>
mov    %rax,(%rsp)
test   %r15d,%r15d
jle    1315 <main+0x145>
add    $0x1,%r15d
mov    $0x1,%ebx
lea    0x8(%rsp),%r13
jmp    125d <main+0x8d>
add    $0x1,%rbx
cmp    %rbx,%r15
je     1315 <main+0x145>
mov    %ebx,%eax
imul   $0x10624dd3,%rax,%rbp
shr    $0x26,%rbp
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
mov    (%rsp),%rax
mov    %r14d,(%rax,%rbx,4)
test   %r14d,%r14d
jne    129f <main+0xcf>
lea    0xd91(%rip),%rdi
call   1070 <diep@plt>
imul   $0xfffffc18,%ebp,%r12d
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
xor    %eax,%eax
call   10e0 <rand@plt>
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
test   %rax,%rax
je     12f7 <main+0x127>
lea    0xd39(%rip),%rdi
call   1070 <diep@plt>
add    %ebx,%r12d
jne    1250 <main+0x80>
lea    0xd2b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1250 <main+0x80>
lea    0xd28(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xcbb(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_nl = type { i64, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Usage: %s <PORTS> <SEED>\0A\00"
@optind = global i64 0
@.str.1 = [17 x i8] c"Create %u ports\0A\00"
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@AF_NETLINK = global i32 0
@.str.3 = [18 x i8] c"Created %u ports\0A\00"
@.str.4 = [41 x i8] c"Ports successfully created, terminating\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 %struct.sockaddr_nl
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 2
%12 = load i8*, i8** %11
%13 = call i32 @strtoul(i8* %12, i32* null, i32 0)
%14 = call i32 @srand(i32 %13)
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %24
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = load i64, i64* @optind
%27 = getelementptr inbounds i8*, i8** %25, i64 %26
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = mul i64 4, %33
%35 = trunc i64 %34 to i32
%36 = call i32* @malloc(i32 %35)
store i32* %36, i32** %7
store i32 1, i32* %6
br label %37
37: ; preds = %77, %24
%38 = load i32, i32* %6
%39 = load i32, i32* %8
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %80
41: ; preds = %37
%42 = load i32, i32* @PF_NETLINK
%43 = load i32, i32* @SOCK_RAW
%44 = call i32 @socket(i32 %42, i32 %43, i32 0)
%45 = load i32*, i32** %7
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 %44, i32* %48
%49 = icmp ne i32 %44, 0
br i1 %49, label %52, label %50
50: ; preds = %41
%51 = call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %41
%53 = call i32 @memset(%struct.sockaddr_nl* %9, i32 0, i32 16)
%54 = load i32, i32* @AF_NETLINK
%55 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %9, i32 0, i32 2
store i32 %54, i32* %55
%56 = call i32 (...) @rand()
%57 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %9, i32 0, i32 1
store i32 %56, i32* %57
%58 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %9, i32 0, i32 0
store i64 0, i64* %58
%59 = load i32*, i32** %7
%60 = load i32, i32* %6
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32, i32* %59, i64 %61
%63 = load i32, i32* %62
%64 = bitcast %struct.sockaddr_nl* %9 to %struct.sockaddr*
%65 = call i64 @bind(i32 %63, %struct.sockaddr* %64, i32 16)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %52
%68 = call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %69
69: ; preds = %67, %52
%70 = load i32, i32* %6
%71 = srem i32 %70, 1000
%72 = icmp ne i32 %71, 0
br i1 %72, label %76, label %73
73: ; preds = %69
%74 = load i32, i32* %6
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %74)
br label %76
76: ; preds = %73, %69
br label %77
77: ; preds = %76
%78 = load i32, i32* %6
%79 = add nsw i32 %78, 1
store i32 %79, i32* %6
br label %37
80: ; preds = %37
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i64, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Usage: %s <PORTS> <SEED>\0A\00"
@optind = global i64 0
@.str.1 = [17 x i8] c"Create %u ports\0A\00"
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@AF_NETLINK = global i32 0
@.str.3 = [18 x i8] c"Created %u ports\0A\00"
@str = [40 x i8] c"Ports successfully created, terminating\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_nl
%4 = bitcast %struct.sockaddr_nl* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 2
%6 = load i8*, i8** %5
%7 = tail call i32 @strtoul(i8* nocapture %6, i32* null, i32 0)
%8 = tail call i32 @srand(i32 %7)
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i64, i64* @optind
%17 = getelementptr inbounds i8*, i8** %1, i64 %16
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = shl i32 %19, 2
%22 = tail call i32* @malloc(i32 %21)
%23 = icmp slt i32 %19, 1
br i1 %23, label %57, label %24
24: ; preds = %15
%25 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 2
%26 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 1
%27 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
%28 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%29 = add nuw i32 %19, 1
%30 = zext i32 %29 to i64
br label %31
31: ; preds = %24, %54
%32 = phi i64 [ 1, %24 ], [ %55, %54 ]
%33 = load i32, i32* @PF_NETLINK
%34 = load i32, i32* @SOCK_RAW
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
%36 = getelementptr inbounds i32, i32* %22, i64 %32
store i32 %35, i32* %36
%37 = icmp eq i32 %35, 0
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %31
%41 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 16)
%42 = load i32, i32* @AF_NETLINK
store i32 %42, i32* %25
%43 = call i32 (...) @rand()
store i32 %43, i32* %26
store i64 0, i64* %27
%44 = call i64 @bind(i32 %35, %struct.sockaddr* %28, i32 16)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %40
%47 = call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %40
%49 = trunc i64 %32 to i32
%50 = urem i32 %49, 1000
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %49)
br label %54
54: ; preds = %48, %52
%55 = add nuw nsw i64 %32, 1
%56 = icmp eq i64 %55, %30
br i1 %56, label %57, label %31
57: ; preds = %54, %15
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
ret i32 0
}