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

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
struct sockaddr_in *malloc(int);
int rand();
int /*<<< orphan*/ random_ipv4_address();
void gen_ipv4(unsigned long *addr, unsigned long *addrlen) {
  struct sockaddr_in *ipv4;
  ipv4 = malloc(sizeof(struct sockaddr_in));
  if (ipv4 == NULL)
    return;
  ipv4->sin_family = PF_INET;
  ipv4->sin_addr.s_addr = random_ipv4_address();
  ipv4->sin_port = rand() % 65535;
  *addr = (unsigned long)ipv4;
  *addrlen = sizeof(struct sockaddr_in);
}


x86 assembly (clang, -O0):
<gen_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xc,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115e <gen_ipv4+0x2e>
jmp    11a9 <gen_ipv4+0x79>
mov    0x2e7b(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    $0x0,%al
call   1040 <random_ipv4_address@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%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   $0xc,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_ipv4>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0xc,%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     1198 <gen_ipv4+0x68>
mov    %rax,%rbx
mov    0x2e8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
xor    %eax,%eax
call   1040 <random_ipv4_address@plt>
mov    %eax,0x4(%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,(%rbx)
mov    %rbx,(%r15)
movq   $0xc,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@PF_INET = global i32 0
define void @gen_ipv4(i64* %0, i64* %1) {
%3 = alloca i64*
%4 = alloca i64*
%5 = alloca %struct.sockaddr_in*
store i64* %0, i64** %3
store i64* %1, i64** %4
%6 = call %struct.sockaddr_in* @malloc(i32 12)
store %struct.sockaddr_in* %6, %struct.sockaddr_in** %5
%7 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%8 = icmp eq %struct.sockaddr_in* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
br label %26
10: ; preds = %2
%11 = load i32, i32* @PF_INET
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %11, i32* %13
%14 = call i32 (...) @random_ipv4_address()
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %14, i32* %17
%18 = call i32 (...) @rand()
%19 = srem i32 %18, 65535
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%23 = ptrtoint %struct.sockaddr_in* %22 to i64
%24 = load i64*, i64** %3
store i64 %23, i64* %24
%25 = load i64*, i64** %4
store i64 12, i64* %25
br label %26
26: ; preds = %10, %9
ret void
}

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