#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  scalar_t__ *s6_addr32;
};
struct sockaddr_in6 {
  int sin6_port;
  TYPE_1__ sin6_addr;
  int /*<<< orphan*/ sin6_family;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET6;
scalar_t__ htonl(int);
struct sockaddr_in6 *malloc(int);
int rand();
void gen_ipv6(unsigned long *addr, unsigned long *addrlen) {
  struct sockaddr_in6 *ipv6;
  ipv6 = malloc(sizeof(struct sockaddr_in6));
  if (ipv6 == NULL)
    return;
  ipv6->sin6_family = PF_INET6;
  ipv6->sin6_addr.s6_addr32[0] = 0;
  ipv6->sin6_addr.s6_addr32[1] = 0;
  ipv6->sin6_addr.s6_addr32[2] = 0;
  ipv6->sin6_addr.s6_addr32[3] = htonl(1);
  ipv6->sin6_port = rand() % 65535;
  *addr = (unsigned long)ipv6;
  *addrlen = sizeof(struct sockaddr_in6);
}


x86 assembly (clang, -O0):
<gen_ipv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x18,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115e <gen_ipv6+0x2e>
jmp    11e1 <gen_ipv6+0xb1>
mov    0x2e83(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,0x10(%rax)
mov    $0x1,%edi
call   1030 <htonl@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,0x18(%rax)
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0xffff,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
mov    %edx,(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x18,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_ipv6>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x18,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11b6 <gen_ipv6+0x86>
mov    %rax,%r12
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%r12)
mov    0x8(%r12),%rbx
xorps  %xmm0,%xmm0
movups %xmm0,(%rbx)
movq   $0x0,0x10(%rbx)
mov    $0x1,%edi
call   1030 <htonl@plt>
mov    %rax,0x18(%rbx)
xor    %eax,%eax
call   1050 <rand@plt>
cltq
imul   $0xffffffff80008001,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0xf,%ecx
add    %edx,%ecx
mov    %ecx,%edx
shl    $0x10,%edx
sub    %edx,%ecx
add    %eax,%ecx
mov    %ecx,(%r12)
mov    %r12,(%r15)
movq   $0x18,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@PF_INET6 = global i32 0
define void @gen_ipv6(i64* %0, i64* %1) {
%3 = alloca i64*
%4 = alloca i64*
%5 = alloca %struct.sockaddr_in6*
store i64* %0, i64** %3
store i64* %1, i64** %4
%6 = call %struct.sockaddr_in6* @malloc(i32 24)
store %struct.sockaddr_in6* %6, %struct.sockaddr_in6** %5
%7 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%8 = icmp eq %struct.sockaddr_in6* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
br label %43
10: ; preds = %2
%11 = load i32, i32* @PF_INET6
%12 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%13 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i32 0, i32 2
store i32 %11, i32* %13
%14 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %14, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
%17 = load i64*, i64** %16
%18 = getelementptr inbounds i64, i64* %17, i64 0
store i64 0, i64* %18
%19 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%20 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %19, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
%22 = load i64*, i64** %21
%23 = getelementptr inbounds i64, i64* %22, i64 1
store i64 0, i64* %23
%24 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %24, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i64*, i64** %26
%28 = getelementptr inbounds i64, i64* %27, i64 2
store i64 0, i64* %28
%29 = call i64 @htonl(i32 1)
%30 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %30, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
%33 = load i64*, i64** %32
%34 = getelementptr inbounds i64, i64* %33, i64 3
store i64 %29, i64* %34
%35 = call i32 (...) @rand()
%36 = srem i32 %35, 65535
%37 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%38 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %37, i32 0, i32 0
store i32 %36, i32* %38
%39 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%40 = ptrtoint %struct.sockaddr_in6* %39 to i64
%41 = load i64*, i64** %3
store i64 %40, i64* %41
%42 = load i64*, i64** %4
store i64 24, i64* %42
br label %43
43: ; preds = %10, %9
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@PF_INET6 = global i32 0
define void @gen_ipv6(i64* nocapture writeonly %0, i64* nocapture writeonly %1) {
%3 = tail call %struct.sockaddr_in6* @malloc(i32 24)
%4 = icmp eq %struct.sockaddr_in6* %3, null
br i1 %4, label %17, label %5
5: ; preds = %2
%6 = load i32, i32* @PF_INET6
%7 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1, i32 0
%9 = load i64*, i64** %8
%10 = bitcast i64* %9 to i8*
%11 = tail call i64 @htonl(i32 1)
%12 = getelementptr inbounds i64, i64* %9, i64 3
store i64 %11, i64* %12
%13 = tail call i32 (...) @rand()
%14 = srem i32 %13, 65535
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = ptrtoint %struct.sockaddr_in6* %3 to i64
store i64 %16, i64* %0
store i64 24, i64* %1
br label %17
17: ; preds = %2, %5
ret void
}