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

/* Variables and functions */
#define IPV6_RTHDR_TYPE_0 128
int /*<<< orphan*/ memset(void *, char, int);
void *inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments) {
  struct ip6_rthdr *rthdr = (struct ip6_rthdr *)bp;
  switch (type) {
  case IPV6_RTHDR_TYPE_0:
    if (segments < 0 || segments > 127)
      break;
    socklen_t len =
        (sizeof(struct ip6_rthdr0) + segments * sizeof(struct in6_addr));
    if (len > bp_len)
      break;
    memset(bp, '\0', len);
    rthdr->ip6r_len = segments * sizeof(struct in6_addr) / 8;
    rthdr->ip6r_type = IPV6_RTHDR_TYPE_0;
    return bp;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<inet6_rth_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    11b3 <inet6_rth_init+0xa3>
jmp    1140 <inet6_rth_init+0x30>
cmpl   $0x0,-0x1c(%rbp)
jl     1154 <inet6_rth_init+0x44>
cmpl   $0x7f,-0x1c(%rbp)
jle    1159 <inet6_rth_init+0x49>
jmp    11b3 <inet6_rth_init+0xa3>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    $0x4,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1179 <inet6_rth_init+0x69>
jmp    11b3 <inet6_rth_init+0xa3>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
shr    $0x3,%rax
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
movl   $0x80,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bb <inet6_rth_init+0xab>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_init>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
cmp    $0x80,%edx
jne    114e <inet6_rth_init+0x3e>
mov    %ecx,%ebx
cmp    $0x7f,%ecx
ja     114e <inet6_rth_init+0x3e>
lea    0x4(,%rbx,4),%edx
cmp    %esi,%edx
jle    1134 <inet6_rth_init+0x24>
xor    %eax,%eax
jmp    114e <inet6_rth_init+0x3e>
mov    %rdi,%r14
xor    %esi,%esi
call   1030 <memset@plt>
shr    %ebx
mov    %ebx,(%r14)
movl   $0x80,0x4(%r14)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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