#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;
typedef scalar_t__ socklen_t;

/* Variables and functions */
scalar_t__ inet6_rth_space(int, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, scalar_t__);
void *inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments) {
  socklen_t needlen;
  needlen = inet6_rth_space(type, segments);
  if ((needlen == 0) || (bp_len < needlen))
    return NULL;
  memset(bp, 0, needlen);
  ((uint8_t *)bp)[1] = segments * 2;
  ((uint8_t *)bp)[2] = type;
  return bp;
}


x86 assembly (clang, -O0):
<inet6_rth_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <inet6_rth_space@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     115e <inet6_rth_init+0x3e>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    116b <inet6_rth_init+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    1199 <inet6_rth_init+0x79>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%ecx
shl    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %edx,%edi
mov    %ecx,%esi
call   1030 <inet6_rth_space@plt>
xor    %ecx,%ecx
test   %rax,%rax
je     1160 <inet6_rth_init+0x40>
cmp    %r15,%rax
jg     1160 <inet6_rth_init+0x40>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rax,%rdx
call   1040 <memset@plt>
add    %ebp,%ebp
mov    %ebp,0x4(%rbx)
mov    %r14d,0x8(%rbx)
mov    %rbx,%rcx
mov    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @inet6_rth_init(i8* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %8
%12 = load i32, i32* %9
%13 = call i64 @inet6_rth_space(i32 %11, i32 %12)
store i64 %13, i64* %10
%14 = load i64, i64* %10
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %4
%17 = load i64, i64* %7
%18 = load i64, i64* %10
%19 = icmp slt i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16, %4
store i8* null, i8** %5
br label %35
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = load i64, i64* %10
%24 = call i32 @memset(i8* %22, i32 0, i64 %23)
%25 = load i32, i32* %9
%26 = mul nsw i32 %25, 2
%27 = load i8*, i8** %6
%28 = bitcast i8* %27 to i32*
%29 = getelementptr inbounds i32, i32* %28, i64 1
store i32 %26, i32* %29
%30 = load i32, i32* %8
%31 = load i8*, i8** %6
%32 = bitcast i8* %31 to i32*
%33 = getelementptr inbounds i32, i32* %32, i64 2
store i32 %30, i32* %33
%34 = load i8*, i8** %6
store i8* %34, i8** %5
br label %35
35: ; preds = %21, %20
%36 = load i8*, i8** %5
ret i8* %36
}

LLVM IR (-O2):
define i8* @inet6_rth_init(i8* %0, i64 %1, i32 %2, i32 %3) {
%5 = tail call i64 @inet6_rth_space(i32 %2, i32 %3)
%6 = icmp eq i64 %5, 0
%7 = icmp sgt i64 %5, %1
%8 = or i1 %6, %7
br i1 %8, label %16, label %9
9: ; preds = %4
%10 = tail call i32 @memset(i8* %0, i32 0, i64 %5)
%11 = shl nsw i32 %3, 1
%12 = getelementptr inbounds i8, i8* %0, i64 4
%13 = bitcast i8* %12 to i32*
store i32 %11, i32* %13
%14 = getelementptr inbounds i8, i8* %0, i64 8
%15 = bitcast i8* %14 to i32*
store i32 %2, i32* %15
br label %16
16: ; preds = %4, %9
%17 = phi i8* [ %0, %9 ], [ null, %4 ]
ret i8* %17
}