#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 */
int /*<<< orphan*/ memset(void *, char, int);
void *inet6_rth_init2(void *bp, socklen_t bp_len, int type, int segments) {
  struct ip6_rthdr *rthdr = (struct ip6_rthdr *)bp;
  switch (type) {
  case 2:
    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 = type;
    return bp;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<inet6_rth_init2>:
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    $0x2,%eax
jne    11b0 <inet6_rth_init2+0xa0>
jmp    113e <inet6_rth_init2+0x2e>
cmpl   $0x0,-0x1c(%rbp)
jl     1152 <inet6_rth_init2+0x42>
cmpl   $0x7f,-0x1c(%rbp)
jle    1157 <inet6_rth_init2+0x47>
jmp    11b0 <inet6_rth_init2+0xa0>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    $0x4,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1177 <inet6_rth_init2+0x67>
jmp    11b0 <inet6_rth_init2+0xa0>
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    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b8 <inet6_rth_init2+0xa8>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_init2>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
cmp    $0x2,%edx
jne    114b <inet6_rth_init2+0x3b>
mov    %ecx,%ebx
cmp    $0x7f,%ecx
ja     114b <inet6_rth_init2+0x3b>
lea    0x4(,%rbx,4),%edx
cmp    %esi,%edx
jle    1131 <inet6_rth_init2+0x21>
xor    %eax,%eax
jmp    114b <inet6_rth_init2+0x3b>
mov    %rdi,%r14
xor    %esi,%esi
call   1030 <memset@plt>
shr    %ebx
mov    %ebx,(%r14)
movl   $0x2,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_init2(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 %47 [
i32 2, 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 %47
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 %47
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 i32, i32* %8
%44 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %10
%45 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %44, i32 0, i32 1
store i32 %43, i32* %45
%46 = load i8*, i8** %6
store i8* %46, i8** %5
br label %48
47: ; preds = %4, %31, %21
store i8* null, i8** %5
br label %48
48: ; preds = %47, %32
%49 = load i8*, i8** %5
ret i8* %49
}

LLVM IR (-O2):
define i8* @inet6_rth_init2(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp ne i32 %2, 2
%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 2, i32* %17
br label %18
18: ; preds = %4, %8, %12
%19 = phi i8* [ %0, %12 ], [ null, %8 ], [ null, %4 ]
ret i8* %19
}