#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 */
typedef int uint8_t;
struct timespec {
  int tv_nsec;
};
struct in_addr {
  int *s6_addr;
  int /*<<< orphan*/ s_addr;
};
struct in6_addr {
  int *s6_addr;
  int /*<<< orphan*/ s_addr;
};
typedef int /*<<< orphan*/ ipbuf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ CLOCK_PROCESS_CPUTIME_ID;
int asprintf(char **, char *, char *, unsigned int);
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ *inet_ntop(int /*<<< orphan*/, struct in_addr *, char *,
                              int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int *, int);
scalar_t__ randomize(int *, int);
char *generate_ip_network(int ipv6, unsigned prefix) {
  struct timespec ts;
  char ipbuf[64];
  char *p = NULL;
  if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) < 0)
    return NULL;
  if (ipv6) {
    struct in6_addr net;
    net.s6_addr[0] = 0xfc;
    net.s6_addr[0] |= ts.tv_nsec & 1;
    if (randomize(&net.s6_addr[1], 15) < 0)
      return NULL;
    if (inet_ntop(AF_INET6, &net, ipbuf, sizeof(ipbuf)) == NULL)
      return NULL;
  } else {
    struct in_addr net;
    unsigned c = ts.tv_nsec % 4;
    uint8_t bytes[4];
    if (randomize(bytes, 4) < 0)
      return NULL;
    if (prefix >= 16 && c < 2) {
      if (c == 1) {
        bytes[0] = 192;
        bytes[1] = 168;
      } else {
        bytes[0] = 172;
        bytes[1] = 16 | ((ts.tv_nsec >> 4) & 0x0f);
      }
    } else {
      bytes[0] = 10;
    }
    memcpy(&net.s_addr, bytes, 4);
    if (inet_ntop(AF_INET, &net, ipbuf, sizeof(ipbuf)) == NULL)
      return NULL;
  }
  if (asprintf(&p, "%s/%u", ipbuf, prefix) == -1)
    return NULL;
  return p;
}


x86 assembly (clang, -O0):
<generate_ip_network>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x68(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0x0,%rax
jge    1192 <generate_ip_network+0x42>
movq   $0x0,-0x8(%rbp)
jmp    1340 <generate_ip_network+0x1f0>
cmpl   $0x0,-0xc(%rbp)
je     1214 <generate_ip_network+0xc4>
mov    -0x78(%rbp),%rax
movl   $0xfc,(%rax)
mov    -0x18(%rbp),%ecx
and    $0x1,%ecx
mov    -0x78(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x78(%rbp),%rdi
add    $0x4,%rdi
mov    $0xf,%esi
call   1070 <randomize@plt>
cmp    $0x0,%rax
jge    11dd <generate_ip_network+0x8d>
movq   $0x0,-0x8(%rbp)
jmp    1340 <generate_ip_network+0x1f0>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    -0x78(%rbp),%rsi
lea    -0x60(%rbp),%rdx
mov    $0x40,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    120f <generate_ip_network+0xbf>
movq   $0x0,-0x8(%rbp)
jmp    1340 <generate_ip_network+0x1f0>
jmp    130b <generate_ip_network+0x1bb>
mov    -0x18(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,-0x8c(%rbp)
lea    -0xa0(%rbp),%rdi
mov    $0x4,%esi
call   1070 <randomize@plt>
cmp    $0x0,%rax
jge    124d <generate_ip_network+0xfd>
movq   $0x0,-0x8(%rbp)
jmp    1340 <generate_ip_network+0x1f0>
cmpl   $0x10,-0x10(%rbp)
jb     12ab <generate_ip_network+0x15b>
cmpl   $0x2,-0x8c(%rbp)
jae    12ab <generate_ip_network+0x15b>
cmpl   $0x1,-0x8c(%rbp)
jne    128a <generate_ip_network+0x13a>
movl   $0xc0,-0xa0(%rbp)
movl   $0xa8,-0x9c(%rbp)
jmp    12a6 <generate_ip_network+0x156>
movl   $0xac,-0xa0(%rbp)
mov    -0x18(%rbp),%eax
sar    $0x4,%eax
and    $0xf,%eax
or     $0x10,%eax
mov    %eax,-0x9c(%rbp)
jmp    12b5 <generate_ip_network+0x165>
movl   $0xa,-0xa0(%rbp)
lea    -0x88(%rbp),%rdi
add    $0x8,%rdi
lea    -0xa0(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    -0x88(%rbp),%rsi
mov    $0x40,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    1306 <generate_ip_network+0x1b6>
movq   $0x0,-0x8(%rbp)
jmp    1340 <generate_ip_network+0x1f0>
jmp    130b <generate_ip_network+0x1bb>
lea    -0x60(%rbp),%rdx
mov    -0x10(%rbp),%ecx
lea    -0x68(%rbp),%rdi
lea    0xce3(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1338 <generate_ip_network+0x1e8>
movq   $0x0,-0x8(%rbp)
jmp    1340 <generate_ip_network+0x1f0>
mov    -0x68(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_ip_network>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %esi,%r14d
movq   $0x0,0x28(%rsp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %rax,%rax
js     1248 <generate_ip_network+0xf8>
mov    0x8(%rsp),%ebx
lea    0x3(%rbx),%ebp
test   %ebx,%ebx
cmovns %ebx,%ebp
lea    0x10(%rsp),%rdi
mov    $0x4,%esi
call   1070 <randomize@plt>
test   %rax,%rax
js     1248 <generate_ip_network+0xf8>
cmp    $0x10,%r14d
jb     11ca <generate_ip_network+0x7a>
and    $0xfffffffc,%ebp
sub    %ebp,%ebx
cmp    $0x1,%ebx
ja     11ca <generate_ip_network+0x7a>
jne    11d4 <generate_ip_network+0x84>
movabs $0xa8000000c0,%rax
mov    %rax,0x10(%rsp)
jmp    11ed <generate_ip_network+0x9d>
movl   $0xa,0x10(%rsp)
jmp    11ed <generate_ip_network+0x9d>
movl   $0xac,0x10(%rsp)
mov    0x8(%rsp),%eax
shr    $0x4,%eax
and    $0xf,%eax
or     $0x10,%eax
mov    %eax,0x14(%rsp)
lea    0x38(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0x30(%rsp),%rsi
lea    0x40(%rsp),%rdx
mov    $0x40,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     1248 <generate_ip_network+0xf8>
lea    0xdd6(%rip),%rsi
lea    0x28(%rsp),%rdi
lea    0x40(%rsp),%rdx
mov    %r14d,%ecx
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1248 <generate_ip_network+0xf8>
mov    0x28(%rsp),%rax
jmp    124a <generate_ip_network+0xfa>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.in6_addr = type { i32*, i32 }
%struct.in_addr = type { i32*, i32 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@.str = [6 x i8] c"%s/%u\00"
define i8* @generate_ip_network(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timespec
%7 = alloca [64 x i8]
%8 = alloca i8*
%9 = alloca %struct.in6_addr
%10 = alloca %struct.in_addr
%11 = alloca i32
%12 = alloca [4 x i32]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* null, i8** %8
%13 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%14 = call i64 @clock_gettime(i32 %13, %struct.timespec* %6)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* null, i8** %3
br label %95
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %46
20: ; preds = %17
%21 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %9, i32 0, i32 0
%22 = load i32*, i32** %21
%23 = getelementptr inbounds i32, i32* %22, i64 0
store i32 252, i32* %23
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = and i32 %25, 1
%27 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %9, i32 0, i32 0
%28 = load i32*, i32** %27
%29 = getelementptr inbounds i32, i32* %28, i64 0
%30 = load i32, i32* %29
%31 = or i32 %30, %26
store i32 %31, i32* %29
%32 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %9, i32 0, i32 0
%33 = load i32*, i32** %32
%34 = getelementptr inbounds i32, i32* %33, i64 1
%35 = call i64 @randomize(i32* %34, i32 15)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %20
store i8* null, i8** %3
br label %95
38: ; preds = %20
%39 = load i32, i32* @AF_INET6
%40 = bitcast %struct.in6_addr* %9 to %struct.in_addr*
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%42 = call i32* @inet_ntop(i32 %39, %struct.in_addr* %40, i8* %41, i32 64)
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %45
44: ; preds = %38
store i8* null, i8** %3
br label %95
45: ; preds = %38
br label %87
46: ; preds = %17
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%48 = load i32, i32* %47
%49 = srem i32 %48, 4
store i32 %49, i32* %11
%50 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 0
%51 = call i64 @randomize(i32* %50, i32 4)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %46
store i8* null, i8** %3
br label %95
54: ; preds = %46
%55 = load i32, i32* %5
%56 = icmp uge i32 %55, 16
br i1 %56, label %57, label %75
57: ; preds = %54
%58 = load i32, i32* %11
%59 = icmp ult i32 %58, 2
br i1 %59, label %60, label %75
60: ; preds = %57
%61 = load i32, i32* %11
%62 = icmp eq i32 %61, 1
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 0
store i32 192, i32* %64
%65 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 1
store i32 168, i32* %65
br label %74
66: ; preds = %60
%67 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 0
store i32 172, i32* %67
%68 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%69 = load i32, i32* %68
%70 = ashr i32 %69, 4
%71 = and i32 %70, 15
%72 = or i32 16, %71
%73 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 1
store i32 %72, i32* %73
br label %74
74: ; preds = %66, %63
br label %77
75: ; preds = %57, %54
%76 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 0
store i32 10, i32* %76
br label %77
77: ; preds = %75, %74
%78 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %10, i32 0, i32 1
%79 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 0
%80 = call i32 @memcpy(i32* %78, i32* %79, i32 4)
%81 = load i32, i32* @AF_INET
%82 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%83 = call i32* @inet_ntop(i32 %81, %struct.in_addr* %10, i8* %82, i32 64)
%84 = icmp eq i32* %83, null
br i1 %84, label %85, label %86
85: ; preds = %77
store i8* null, i8** %3
br label %95
86: ; preds = %77
br label %87
87: ; preds = %86, %45
%88 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%89 = load i32, i32* %5
%90 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %88, i32 %89)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %93
92: ; preds = %87
store i8* null, i8** %3
br label %95
93: ; preds = %87
%94 = load i8*, i8** %8
store i8* %94, i8** %3
br label %95
95: ; preds = %93, %92, %85, %53, %44, %37, %16
%96 = load i8*, i8** %3
ret i8* %96
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
%struct.in_addr = type { i32*, i32 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@.str = [6 x i8] c"%s/%u\00"
define i8* @generate_ip_network(i32 %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = alloca [64 x i8]
%5 = alloca i8*
%6 = alloca %struct.in_addr
%7 = alloca [4 x i32]
%8 = bitcast %struct.timespec* %3 to i8*
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%10 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%11 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%12 = call i64 @clock_gettime(i32 %11, %struct.timespec* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %51, label %14
14: ; preds = %2
%15 = icmp eq i32 %0, 0
%16 = bitcast %struct.in_addr* %6 to i8*
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = srem i32 %18, 4
%20 = bitcast [4 x i32]* %7 to i8*
%21 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 0
%22 = call i64 @randomize(i32* %21, i32 4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %50, label %24
24: ; preds = %14
%25 = icmp ugt i32 %1, 15
%26 = icmp ult i32 %19, 2
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = icmp eq i32 %19, 1
br i1 %29, label %30, label %32
30: ; preds = %28
store i32 192, i32* %21
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 1
store i32 168, i32* %31
br label %39
32: ; preds = %28
store i32 172, i32* %21
%33 = load i32, i32* %17
%34 = lshr i32 %33, 4
%35 = and i32 %34, 15
%36 = or i32 %35, 16
%37 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 1
store i32 %36, i32* %37
br label %39
38: ; preds = %24
store i32 10, i32* %21
br label %39
39: ; preds = %30, %32, %38
%40 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %6, i64 0, i32 1
%41 = call i32 @memcpy(i32* %40, i32* %21, i32 4)
%42 = load i32, i32* @AF_INET
%43 = call i32* @inet_ntop(i32 %42, %struct.in_addr* %6, i8* %9, i32 64)
%44 = icmp eq i32* %43, null
br i1 %44, label %51, label %45
45: ; preds = %39
%46 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %1)
%47 = icmp eq i32 %46, -1
%48 = load i8*, i8** %5
%49 = select i1 %47, i8* null, i8* %48
br label %51
50: ; preds = %14
br label %51
51: ; preds = %45, %50, %2, %39
%52 = phi i8* [ null, %39 ], [ null, %2 ], [ null, %50 ], [ %49, %45 ]
ret i8* %52
}