#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 scalar_t__ uint16_t;
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ htonl(long);
int /*<<< orphan*/ htons(scalar_t__);
scalar_t__ inet_aton(char const *, struct in_addr *);
int random();
int genrandomdest(const char *dstaddr, int dstnetpref,
                  struct sockaddr_in *sin) {
  struct in_addr raddr;
  long rnum;
  uint16_t rport;
  assert(sin->sin_family == AF_INET);
  if (inet_aton(dstaddr, &raddr) == 0) {
    return (-1);
  }
  rnum = random() >> dstnetpref;
  raddr.s_addr |= htonl(rnum);
  do {
    rport = (uint16_t)random();
  } while (rport == 0);
  sin->sin_addr = raddr;
  sin->sin_port = htons(rport);
  return (0);
}


x86 assembly (clang, -O0):
<genrandomdest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e77(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <inet_aton@plt>
cmp    $0x0,%rax
jne    11a4 <genrandomdest+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <genrandomdest+0xb0>
mov    $0x0,%al
call   1070 <random@plt>
mov    -0x14(%rbp),%ecx
sar    %cl,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <htonl@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <random@plt>
cltq
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11c5 <genrandomdest+0x75>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,0xc(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genrandomdest>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    (%rdx),%rax
mov    0x2e7d(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
sete   %dil
call   1050 <assert@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <inet_aton@plt>
test   %rax,%rax
je     11d1 <genrandomdest+0x81>
xor    %eax,%eax
call   1070 <random@plt>
mov    %r14d,%ecx
sar    %cl,%eax
movslq %eax,%rdi
call   1040 <htonl@plt>
or     %eax,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1070 <random@plt>
test   %eax,%eax
je     11b0 <genrandomdest+0x60>
movslq %eax,%rdi
mov    0x8(%rsp),%eax
mov    %eax,0xc(%rbx)
call   1030 <htons@plt>
mov    %eax,0x8(%rbx)
xor    %eax,%eax
jmp    11d6 <genrandomdest+0x86>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
@AF_INET = global i64 0
define i32 @genrandomdest(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.in_addr
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%11 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%13 = load i64, i64* %12
%14 = load i64, i64* @AF_INET
%15 = icmp eq i64 %13, %14
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8*, i8** %5
%19 = call i64 @inet_aton(i8* %18, %struct.in_addr* %8)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 -1, i32* %4
br label %47
22: ; preds = %3
%23 = call i32 (...) @random()
%24 = load i32, i32* %6
%25 = ashr i32 %23, %24
%26 = sext i32 %25 to i64
store i64 %26, i64* %9
%27 = load i64, i64* %9
%28 = call i32 @htonl(i64 %27)
%29 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %8, i32 0, i32 0
%30 = load i32, i32* %29
%31 = or i32 %30, %28
store i32 %31, i32* %29
br label %32
32: ; preds = %35, %22
%33 = call i32 (...) @random()
%34 = sext i32 %33 to i64
store i64 %34, i64* %10
br label %35
35: ; preds = %32
%36 = load i64, i64* %10
%37 = icmp eq i64 %36, 0
br i1 %37, label %32, label %38
38: ; preds = %35
%39 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %39, i32 0, i32 2
%41 = bitcast %struct.in_addr* %40 to i8*
%42 = bitcast %struct.in_addr* %8 to i8*
%43 = load i64, i64* %10
%44 = call i32 @htons(i64 %43)
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %45, i32 0, i32 1
store i32 %44, i32* %46
store i32 0, i32* %4
br label %47
47: ; preds = %38, %21
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
@AF_INET = global i64 0
define i32 @genrandomdest(i8* %0, i32 %1, %struct.sockaddr_in* nocapture %2) {
%4 = alloca %struct.in_addr
%5 = bitcast %struct.in_addr* %4 to i8*
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%7 = load i64, i64* %6
%8 = load i64, i64* @AF_INET
%9 = icmp eq i64 %7, %8
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = call i64 @inet_aton(i8* %0, %struct.in_addr* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %31, label %14
14: ; preds = %3
%15 = call i32 (...) @random()
%16 = ashr i32 %15, %1
%17 = sext i32 %16 to i64
%18 = call i32 @htonl(i64 %17)
%19 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
%20 = load i32, i32* %19
%21 = or i32 %20, %18
store i32 %21, i32* %19
br label %22
22: ; preds = %22, %14
%23 = call i32 (...) @random()
%24 = icmp eq i32 %23, 0
br i1 %24, label %22, label %25
25: ; preds = %22
%26 = sext i32 %23 to i64
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
%28 = load i32, i32* %19
store i32 %28, i32* %27
%29 = call i32 @htons(i64 %26)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %29, i32* %30
br label %31
31: ; preds = %3, %25
%32 = phi i32 [ 0, %25 ], [ -1, %3 ]
ret i32 %32
}