#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__ uint32_t;
struct ip6_rthdr {
  int ip6r_len;
  int ip6r_type;
  scalar_t__ ip6r_segleft;
  scalar_t__ ip6r_nxt;
};
struct in6_addr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int IPV6_RTHDR_TYPE_0;
void *inet6_rth_init(void *bp, socklen_t bplen, int type, int segments) {
  struct ip6_rthdr *rth = (struct ip6_rthdr *)bp;
  uint8_t type_len = 0;
  if (type == IPV6_RTHDR_TYPE_0) {
    type_len = 8;
    *(uint32_t *)(rth + 1) = 0;
  } else if (type == 2) {
    type_len = 8;
    *(uint32_t *)(rth + 1) = 0;
  } else
    return NULL;
  if (bplen < type_len + segments * sizeof(struct in6_addr))
    return NULL;
  rth->ip6r_nxt = 0;
  rth->ip6r_len = segments << 1;
  rth->ip6r_type = type;
  rth->ip6r_segleft = 0;
  return bp;
}


x86 assembly (clang, -O0):
<inet6_rth_init>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2ebe(%rip),%rcx
cmp    (%rcx),%eax
jne    114a <inet6_rth_init+0x4a>
movl   $0x8,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movq   $0x0,0x18(%rax)
jmp    117e <inet6_rth_init+0x7e>
cmpl   $0x2,-0x18(%rbp)
jne    116c <inet6_rth_init+0x6c>
movl   $0x8,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movq   $0x0,0x18(%rax)
jmp    1179 <inet6_rth_init+0x79>
movq   $0x0,-0x8(%rbp)
jmp    11dd <inet6_rth_init+0xdd>
jmp    117e <inet6_rth_init+0x7e>
movslq -0x14(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    11a7 <inet6_rth_init+0xa7>
movq   $0x0,-0x8(%rbp)
jmp    11dd <inet6_rth_init+0xdd>
mov    -0x28(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_init>:
mov    %rdi,%rax
mov    0x2ede(%rip),%rdi
cmp    %edx,(%rdi)
je     1116 <inet6_rth_init+0x16>
cmp    $0x2,%edx
je     1116 <inet6_rth_init+0x16>
xor    %eax,%eax
ret
movq   $0x0,0x18(%rax)
movslq %esi,%rsi
movslq %ecx,%rdi
lea    0x8(,%rdi,4),%rdi
cmp    %rsi,%rdi
jbe    1134 <inet6_rth_init+0x34>
xor    %eax,%eax
ret
add    %ecx,%ecx
mov    %ecx,(%rax)
mov    %edx,0x4(%rax)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rax)
ret

LLVM IR (-O0):
%struct.ip6_rthdr = type { i32, i32, i64, i64 }
@IPV6_RTHDR_TYPE_0 = global i32 0
define i8* @inet6_rth_init(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.ip6_rthdr*
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to %struct.ip6_rthdr*
store %struct.ip6_rthdr* %13, %struct.ip6_rthdr** %10
store i32 0, i32* %11
%14 = load i32, i32* %8
%15 = load i32, i32* @IPV6_RTHDR_TYPE_0
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %4
store i32 8, i32* %11
%18 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %10
%19 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %18, i64 1
%20 = bitcast %struct.ip6_rthdr* %19 to i64*
store i64 0, i64* %20
br label %30
21: ; preds = %4
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %28
24: ; preds = %21
store i32 8, i32* %11
%25 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %10
%26 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %25, i64 1
%27 = bitcast %struct.ip6_rthdr* %26 to i64*
store i64 0, i64* %27
br label %29
28: ; preds = %21
store i8* null, i8** %5
br label %54
29: ; preds = %24
br label %30
30: ; preds = %29, %17
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 4
%38 = add i64 %34, %37
%39 = icmp ult i64 %32, %38
br i1 %39, label %40, label %41
40: ; preds = %30
store i8* null, i8** %5
br label %54
41: ; preds = %30
%42 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %10
%43 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %42, i32 0, i32 3
store i64 0, i64* %43
%44 = load i32, i32* %9
%45 = shl i32 %44, 1
%46 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %10
%47 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %46, i32 0, i32 0
store i32 %45, i32* %47
%48 = load i32, i32* %8
%49 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %10
%50 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %49, i32 0, i32 1
store i32 %48, i32* %50
%51 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %10
%52 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %51, i32 0, i32 2
store i64 0, i64* %52
%53 = load i8*, i8** %6
store i8* %53, i8** %5
br label %54
54: ; preds = %41, %40, %28
%55 = load i8*, i8** %5
ret i8* %55
}

LLVM IR (-O2):
@IPV6_RTHDR_TYPE_0 = global i32 0
define i8* @inet6_rth_init(i8* writeonly %0, i32 %1, i32 %2, i32 %3) {
%5 = load i32, i32* @IPV6_RTHDR_TYPE_0
%6 = icmp eq i32 %5, %2
%7 = icmp eq i32 %2, 2
%8 = or i1 %6, %7
br i1 %8, label %9, label %24
9: ; preds = %4
%10 = getelementptr inbounds i8, i8* %0, i64 24
%11 = bitcast i8* %10 to i64*
store i64 0, i64* %11
%12 = sext i32 %1 to i64
%13 = sext i32 %3 to i64
%14 = shl nsw i64 %13, 2
%15 = add nsw i64 %14, 8
%16 = icmp ugt i64 %15, %12
br i1 %16, label %24, label %17
17: ; preds = %9
%18 = shl i32 %3, 1
%19 = bitcast i8* %0 to i32*
store i32 %18, i32* %19
%20 = getelementptr inbounds i8, i8* %0, i64 4
%21 = bitcast i8* %20 to i32*
store i32 %2, i32* %21
%22 = getelementptr inbounds i8, i8* %0, i64 8
%23 = bitcast i8* %22 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %23
br label %24
24: ; preds = %4, %9, %17
%25 = phi i8* [ %0, %17 ], [ null, %9 ], [ null, %4 ]
ret i8* %25
}