#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 */
struct ip6_rthdr0 {
  int ip6r0_len;
  int ip6r0_type;
  int /*<<< orphan*/ ip6r0_reserved;
  int /*<<< orphan*/ ip6r0_segleft;
};
struct ip6_rthdr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
#define IPV6_RTHDR_TYPE_0 128
int /*<<< orphan*/ inet6_rth_space(int const, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int /*<<< orphan*/);
void *inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments) {
  struct ip6_rthdr *rth = (struct ip6_rthdr *)bp;
  struct ip6_rthdr0 *rth0;
  switch (type) {
  case IPV6_RTHDR_TYPE_0:
    if (bp_len < inet6_rth_space(IPV6_RTHDR_TYPE_0, segments))
      return (NULL);
    if ((segments < 0) || (segments > 127))
      return (NULL);
    memset(bp, 0, bp_len);
    rth0 = (struct ip6_rthdr0 *)rth;
    rth0->ip6r0_len = segments * 2;
    rth0->ip6r0_type = IPV6_RTHDR_TYPE_0;
    rth0->ip6r0_segleft = 0;
    rth0->ip6r0_reserved = 0;
    break;
  default:
    return (NULL);
  }
  return (bp);
}


x86 assembly (clang, -O0):
<inet6_rth_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x80,%eax
jne    11e6 <inet6_rth_init+0xc6>
jmp    1150 <inet6_rth_init+0x30>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%esi
mov    $0x80,%edi
call   1030 <inet6_rth_space@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jge    117d <inet6_rth_init+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    11fb <inet6_rth_init+0xdb>
cmpl   $0x0,-0x1c(%rbp)
jl     1191 <inet6_rth_init+0x71>
cmpl   $0x7f,-0x1c(%rbp)
jle    119e <inet6_rth_init+0x7e>
movq   $0x0,-0x8(%rbp)
jmp    11fb <inet6_rth_init+0xdb>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
movl   $0x80,0x4(%rax)
mov    -0x30(%rbp),%rax
movl   $0x0,0xc(%rax)
mov    -0x30(%rbp),%rax
movl   $0x0,0x8(%rax)
jmp    11f3 <inet6_rth_init+0xd3>
movq   $0x0,-0x8(%rbp)
jmp    11fb <inet6_rth_init+0xdb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_init>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x80,%edx
jne    1173 <inet6_rth_init+0x53>
mov    %ecx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    $0x80,%edi
mov    %ecx,%esi
call   1030 <inet6_rth_space@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x7f,%ebp
ja     1175 <inet6_rth_init+0x55>
cmp    %r14d,%ecx
jg     1175 <inet6_rth_init+0x55>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1040 <memset@plt>
add    %ebp,%ebp
mov    %ebp,(%rbx)
movq   $0x80,0x4(%rbx)
movl   $0x0,0xc(%rbx)
mov    %rbx,%rax
jmp    1175 <inet6_rth_init+0x55>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32, i32, i32, i32 }
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 %struct.ip6_rthdr0*
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
%14 = load i32, i32* %8
switch i32 %14, label %44 [
i32 128, label %15
]
15: ; preds = %4
%16 = load i32, i32* %7
%17 = load i32, i32* %9
%18 = call i32 @inet6_rth_space(i32 128, i32 %17)
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %21
20: ; preds = %15
store i8* null, i8** %5
br label %47
21: ; preds = %15
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* %9
%26 = icmp sgt i32 %25, 127
br i1 %26, label %27, label %28
27: ; preds = %24, %21
store i8* null, i8** %5
br label %47
28: ; preds = %24
%29 = load i8*, i8** %6
%30 = load i32, i32* %7
%31 = call i32 @memset(i8* %29, i32 0, i32 %30)
%32 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %10
%33 = bitcast %struct.ip6_rthdr* %32 to %struct.ip6_rthdr0*
store %struct.ip6_rthdr0* %33, %struct.ip6_rthdr0** %11
%34 = load i32, i32* %9
%35 = mul nsw i32 %34, 2
%36 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %11
%37 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %11
%39 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %38, i32 0, i32 1
store i32 128, i32* %39
%40 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %11
%41 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %40, i32 0, i32 3
store i32 0, i32* %41
%42 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %11
%43 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %42, i32 0, i32 2
store i32 0, i32* %43
br label %45
44: ; preds = %4
store i8* null, i8** %5
br label %47
45: ; preds = %28
%46 = load i8*, i8** %6
store i8* %46, i8** %5
br label %47
47: ; preds = %45, %44, %27, %20
%48 = load i8*, i8** %5
ret i8* %48
}

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