#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 /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ htonl(int);
int rand();
in_addr_t random_ipv4_address(void) {
  int addr = 0;
  int class = 0;
  switch (rand() % 9) {
  case 0:
    addr = 0;
    break;
  case 1:
    addr = 0x0a000000;
    class = 8;
    break;
  case 2:
    addr = 0x7f000001;
    class = 8;
    break;
  case 3:
    addr = 0xa9fe0000;
    class = 16;
    break;
  case 4:
    addr = 0xac100000;
    class = 12;
    break;
  case 5:
    addr = 0xc0586300;
    class = 24;
    break;
  case 6:
    addr = 0xc0a80000;
    class = 16;
    break;
  case 7:
    addr = 0xe0000000;
    class = 4;
    break;
  case 8:
    addr = 0xffffffff;
    break;
  default:
    break;
  }
  if (rand() % 100 < 50) {
    switch (class) {
    case 4:
      addr |= rand() % 0xfffffff;
      break;
    case 8:
      addr |= rand() % 0xffffff;
      break;
    case 12:
      addr |= rand() % 0xfffff;
      break;
    case 16:
      addr |= rand() % 0xffff;
      break;
    case 24:
      addr |= rand() % 0xff;
      break;
    default:
      break;
    }
  }
  return htonl(addr);
}


x86 assembly (clang, -O0):
<random_ipv4_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
xor    %eax,%eax
call   1040 <rand@plt>
movslq %eax,%rcx
imul   $0x38e38e39,%rcx,%rdx
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rdx
add    %ecx,%edx
mov    %edx,%ecx
lea    (%rcx,%rcx,8),%ecx
sub    %ecx,%eax
mov    %eax,%ecx
mov    %rcx,-0x10(%rbp)
sub    $0x8,%eax
ja     121b <random_ipv4_address+0xfb>
mov    -0x10(%rbp),%rax
lea    0xe8b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x0,-0x4(%rbp)
jmp    1220 <random_ipv4_address+0x100>
movl   $0xa000000,-0x4(%rbp)
movl   $0x8,-0x8(%rbp)
jmp    1220 <random_ipv4_address+0x100>
movl   $0x7f000001,-0x4(%rbp)
movl   $0x8,-0x8(%rbp)
jmp    1220 <random_ipv4_address+0x100>
movl   $0xa9fe0000,-0x4(%rbp)
movl   $0x10,-0x8(%rbp)
jmp    1220 <random_ipv4_address+0x100>
movl   $0xac100000,-0x4(%rbp)
movl   $0xc,-0x8(%rbp)
jmp    1220 <random_ipv4_address+0x100>
movl   $0xc0586300,-0x4(%rbp)
movl   $0x18,-0x8(%rbp)
jmp    1220 <random_ipv4_address+0x100>
movl   $0xc0a80000,-0x4(%rbp)
movl   $0x10,-0x8(%rbp)
jmp    1220 <random_ipv4_address+0x100>
movl   $0xe0000000,-0x4(%rbp)
movl   $0x4,-0x8(%rbp)
jmp    1220 <random_ipv4_address+0x100>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <random_ipv4_address+0x100>
jmp    1220 <random_ipv4_address+0x100>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
cmp    $0x32,%edx
jge    12ed <random_ipv4_address+0x1cd>
mov    -0x8(%rbp),%eax
add    $0xfffffffc,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x14,%eax
ja     12e3 <random_ipv4_address+0x1c3>
mov    -0x18(%rbp),%rax
lea    0xdcc(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0xfffffff,%ecx
cltd
idiv   %ecx
or     -0x4(%rbp),%edx
mov    %edx,-0x4(%rbp)
jmp    12e8 <random_ipv4_address+0x1c8>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0xffffff,%ecx
cltd
idiv   %ecx
or     -0x4(%rbp),%edx
mov    %edx,-0x4(%rbp)
jmp    12e8 <random_ipv4_address+0x1c8>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0xfffff,%ecx
cltd
idiv   %ecx
or     -0x4(%rbp),%edx
mov    %edx,-0x4(%rbp)
jmp    12e8 <random_ipv4_address+0x1c8>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0xffff,%ecx
cltd
idiv   %ecx
or     -0x4(%rbp),%edx
mov    %edx,-0x4(%rbp)
jmp    12e8 <random_ipv4_address+0x1c8>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0xff,%ecx
cltd
idiv   %ecx
or     -0x4(%rbp),%edx
mov    %edx,-0x4(%rbp)
jmp    12e8 <random_ipv4_address+0x1c8>
jmp    12e8 <random_ipv4_address+0x1c8>
jmp    12ed <random_ipv4_address+0x1cd>
mov    -0x4(%rbp),%edi
call   1030 <htonl@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_ipv4_address>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0x38e38e39,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,8),%ecx
not    %ecx
add    %ecx,%eax
mov    $0x3fffffff,%ebp
cmp    $0x7,%eax
ja     116a <random_ipv4_address+0x4a>
cltq
lea    0xea3(%rip),%rcx
mov    (%rcx,%rax,4),%ebx
lea    0xeb9(%rip),%rcx
mov    (%rcx,%rax,4),%ebp
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
cmp    $0x31,%eax
jg     11b8 <random_ipv4_address+0x98>
cmp    $0x5,%ebp
ja     11b8 <random_ipv4_address+0x98>
mov    $0x2f,%eax
bt     %ebp,%eax
jae    11b8 <random_ipv4_address+0x98>
movslq %ebp,%rbp
lea    0xe96(%rip),%r14
xor    %eax,%eax
call   1040 <rand@plt>
cltd
idivl  (%r14,%rbp,4)
or     %edx,%ebx
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <htonl@plt>

LLVM IR (-O0):
define i32 @random_ipv4_address() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%3 = call i32 (...) @rand()
%4 = srem i32 %3, 9
switch i32 %4, label %14 [
i32 0, label %5
i32 1, label %6
i32 2, label %7
i32 3, label %8
i32 4, label %9
i32 5, label %10
i32 6, label %11
i32 7, label %12
i32 8, label %13
]
5: ; preds = %0
store i32 0, i32* %1
br label %15
6: ; preds = %0
store i32 167772160, i32* %1
store i32 8, i32* %2
br label %15
7: ; preds = %0
store i32 2130706433, i32* %1
store i32 8, i32* %2
br label %15
8: ; preds = %0
store i32 -1442971648, i32* %1
store i32 16, i32* %2
br label %15
9: ; preds = %0
store i32 -1408237568, i32* %1
store i32 12, i32* %2
br label %15
10: ; preds = %0
store i32 -1067949312, i32* %1
store i32 24, i32* %2
br label %15
11: ; preds = %0
store i32 -1062731776, i32* %1
store i32 16, i32* %2
br label %15
12: ; preds = %0
store i32 -536870912, i32* %1
store i32 4, i32* %2
br label %15
13: ; preds = %0
store i32 -1, i32* %1
br label %15
14: ; preds = %0
br label %15
15: ; preds = %14, %13, %12, %11, %10, %9, %8, %7, %6, %5
%16 = call i32 (...) @rand()
%17 = srem i32 %16, 100
%18 = icmp slt i32 %17, 50
br i1 %18, label %19, label %48
19: ; preds = %15
%20 = load i32, i32* %2
switch i32 %20, label %46 [
i32 4, label %21
i32 8, label %26
i32 12, label %31
i32 16, label %36
i32 24, label %41
]
21: ; preds = %19
%22 = call i32 (...) @rand()
%23 = srem i32 %22, 268435455
%24 = load i32, i32* %1
%25 = or i32 %24, %23
store i32 %25, i32* %1
br label %47
26: ; preds = %19
%27 = call i32 (...) @rand()
%28 = srem i32 %27, 16777215
%29 = load i32, i32* %1
%30 = or i32 %29, %28
store i32 %30, i32* %1
br label %47
31: ; preds = %19
%32 = call i32 (...) @rand()
%33 = srem i32 %32, 1048575
%34 = load i32, i32* %1
%35 = or i32 %34, %33
store i32 %35, i32* %1
br label %47
36: ; preds = %19
%37 = call i32 (...) @rand()
%38 = srem i32 %37, 65535
%39 = load i32, i32* %1
%40 = or i32 %39, %38
store i32 %40, i32* %1
br label %47
41: ; preds = %19
%42 = call i32 (...) @rand()
%43 = srem i32 %42, 255
%44 = load i32, i32* %1
%45 = or i32 %44, %43
store i32 %45, i32* %1
br label %47
46: ; preds = %19
br label %47
47: ; preds = %46, %41, %36, %31, %26, %21
br label %48
48: ; preds = %47, %15
%49 = load i32, i32* %1
%50 = call i32 @htonl(i32 %49)
ret i32 %50
}

LLVM IR (-O2):
@switch.table.random_ipv4_address = [8 x i32] [i32 167772160, i32 2130706433, i32 -1442971648, i32 -1408237568, i32 -1067949312, i32 -1062731776, i32 -536870912, i32 -1]
@switch.table.random_ipv4_address.1 = [8 x i32] [i32 1, i32 1, i32 3, i32 2, i32 5, i32 3, i32 0, i32 1073741823]
@switch.table.random_ipv4_address.2 = [6 x i32] [i32 268435455, i32 16777215, i32 1048575, i32 65535, i32 268435455, i32 255]
define i32 @random_ipv4_address() {
%1 = tail call i32 (...) @rand()
%2 = srem i32 %1, 9
%3 = add nsw i32 %2, -1
%4 = icmp ult i32 %3, 8
br i1 %4, label %5, label %12
5: ; preds = %0
%6 = sext i32 %3 to i64
%7 = getelementptr inbounds [8 x i32], [8 x i32]* @switch.table.random_ipv4_address, i64 0, i64 %6
%8 = load i32, i32* %7
%9 = sext i32 %3 to i64
%10 = getelementptr inbounds [8 x i32], [8 x i32]* @switch.table.random_ipv4_address.1, i64 0, i64 %9
%11 = load i32, i32* %10
br label %12
12: ; preds = %5, %0
%13 = phi i32 [ 0, %0 ], [ %8, %5 ]
%14 = phi i32 [ 1073741823, %0 ], [ %11, %5 ]
%15 = tail call i32 (...) @rand()
%16 = srem i32 %15, 100
%17 = icmp slt i32 %16, 50
%18 = icmp ult i32 %14, 6
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %20, label %32
20: ; preds = %12
%21 = trunc i32 %14 to i8
%22 = lshr i8 47, %21
%23 = and i8 %22, 1
%24 = icmp eq i8 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %20
%26 = sext i32 %14 to i64
%27 = getelementptr inbounds [6 x i32], [6 x i32]* @switch.table.random_ipv4_address.2, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = tail call i32 (...) @rand()
%30 = srem i32 %29, %28
%31 = or i32 %30, %13
br label %32
32: ; preds = %20, %25, %12
%33 = phi i32 [ %13, %12 ], [ %31, %25 ], [ %13, %20 ]
%34 = tail call i32 @htonl(i32 %33)
ret i32 %34
}