#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 /*<<< orphan*/ u_int;
struct ip6_rthdr0 {
  int ip6r0_segleft;
  int ip6r0_len;
};
struct ip6_rthdr {
  int ip6r_type;
};
struct in6_addr {
  int dummy;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_len;
};
typedef int /*<<< orphan*/ caddr_t;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int /*<<< orphan*/ IPV6_RTHDR_LOOSE;
int /*<<< orphan*/ IPV6_RTHDR_STRICT;
#define IPV6_RTHDR_TYPE_0 128
int /*<<< orphan*/ bcopy(struct in6_addr const *, int /*<<< orphan*/, int);
int inet6_rthdr_add(struct cmsghdr *cmsg, const struct in6_addr *addr,
                    u_int flags) {
  struct ip6_rthdr *rthdr;
  rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg);
  switch (rthdr->ip6r_type) {
  case IPV6_RTHDR_TYPE_0: {
    struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT)
      return (-1);
    if (rt0->ip6r0_segleft == 23)
      return (-1);
    if (flags != IPV6_RTHDR_LOOSE)
      return (-1);
    rt0->ip6r0_segleft++;
    bcopy(addr, (caddr_t)rt0 + ((rt0->ip6r0_len + 1) << 3),
          sizeof(struct in6_addr));
    rt0->ip6r0_len += sizeof(struct in6_addr) >> 3;
    cmsg->cmsg_len = CMSG_LEN((rt0->ip6r0_len + 1) << 3);
    break;
  }
  default:
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<inet6_rthdr_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1231 <inet6_rthdr_add+0x101>
jmp    1166 <inet6_rthdr_add+0x36>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%eax
je     119e <inet6_rthdr_add+0x6e>
mov    -0x1c(%rbp),%eax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%eax
je     119e <inet6_rthdr_add+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <inet6_rthdr_add+0x114>
mov    -0x30(%rbp),%rax
cmpl   $0x17,(%rax)
jne    11b7 <inet6_rthdr_add+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <inet6_rthdr_add+0x114>
mov    -0x1c(%rbp),%eax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%eax
je     11d5 <inet6_rthdr_add+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <inet6_rthdr_add+0x114>
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x1,%eax
shl    $0x3,%eax
add    %eax,%esi
mov    $0x4,%edx
call   1050 <bcopy@plt>
mov    -0x30(%rbp),%rax
movslq 0x4(%rax),%rcx
add    $0x0,%rcx
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
add    $0x1,%edi
shl    $0x3,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    123d <inet6_rthdr_add+0x10d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <inet6_rthdr_add+0x114>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rthdr_add>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <CMSG_DATA@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
cmpl   $0x80,(%rbx)
jne    11a6 <inet6_rthdr_add+0x76>
mov    0x2e8d(%rip),%rcx
mov    (%rcx),%ecx
cmp    %ebp,%ecx
je     116c <inet6_rthdr_add+0x3c>
mov    0x2e70(%rip),%rdx
cmp    %ebp,(%rdx)
jne    11a6 <inet6_rthdr_add+0x76>
mov    (%rbx),%edx
cmp    $0x17,%edx
je     11a6 <inet6_rthdr_add+0x76>
cmp    %ebp,%ecx
jne    11a6 <inet6_rthdr_add+0x76>
add    $0x1,%edx
mov    %edx,(%rbx)
mov    0x4(%rbx),%eax
lea    (%rbx,%rax,8),%esi
add    $0x8,%esi
mov    %r14,%rdi
mov    $0x4,%edx
call   1050 <bcopy@plt>
mov    0x4(%rbx),%eax
lea    0x8(,%rax,8),%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i32 }
%struct.in6_addr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32, i32 }
@IPV6_RTHDR_LOOSE = global i32 0
@IPV6_RTHDR_STRICT = global i32 0
define i32 @inet6_rthdr_add(%struct.cmsghdr* %0, %struct.in6_addr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.cmsghdr*
%6 = alloca %struct.in6_addr*
%7 = alloca i32
%8 = alloca %struct.ip6_rthdr*
%9 = alloca %struct.ip6_rthdr0*
store %struct.cmsghdr* %0, %struct.cmsghdr** %5
store %struct.in6_addr* %1, %struct.in6_addr** %6
store i32 %2, i32* %7
%10 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%11 = call i64 @CMSG_DATA(%struct.cmsghdr* %10)
%12 = inttoptr i64 %11 to %struct.ip6_rthdr*
store %struct.ip6_rthdr* %12, %struct.ip6_rthdr** %8
%13 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %8
%14 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %13, i32 0, i32 0
%15 = load i32, i32* %14
switch i32 %15, label %67 [
i32 128, label %16
]
16: ; preds = %3
%17 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %8
%18 = bitcast %struct.ip6_rthdr* %17 to %struct.ip6_rthdr0*
store %struct.ip6_rthdr0* %18, %struct.ip6_rthdr0** %9
%19 = load i32, i32* %7
%20 = load i32, i32* @IPV6_RTHDR_LOOSE
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i32, i32* %7
%24 = load i32, i32* @IPV6_RTHDR_STRICT
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %4
br label %69
27: ; preds = %22, %16
%28 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %9
%29 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = icmp eq i32 %30, 23
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 -1, i32* %4
br label %69
33: ; preds = %27
%34 = load i32, i32* %7
%35 = load i32, i32* @IPV6_RTHDR_LOOSE
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 -1, i32* %4
br label %69
38: ; preds = %33
%39 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %9
%40 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = add nsw i32 %41, 1
store i32 %42, i32* %40
%43 = load %struct.in6_addr*, %struct.in6_addr** %6
%44 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %9
%45 = ptrtoint %struct.ip6_rthdr0* %44 to i32
%46 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %9
%47 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = add nsw i32 %48, 1
%50 = shl i32 %49, 3
%51 = add nsw i32 %45, %50
%52 = call i32 @bcopy(%struct.in6_addr* %43, i32 %51, i32 4)
%53 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %9
%54 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = sext i32 %55 to i64
%57 = add i64 %56, 0
%58 = trunc i64 %57 to i32
store i32 %58, i32* %54
%59 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %9
%60 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %59, i32 0, i32 1
%61 = load i32, i32* %60
%62 = add nsw i32 %61, 1
%63 = shl i32 %62, 3
%64 = call i32 @CMSG_LEN(i32 %63)
%65 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%66 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %65, i32 0, i32 0
store i32 %64, i32* %66
br label %68
67: ; preds = %3
store i32 -1, i32* %4
br label %69
68: ; preds = %38
store i32 0, i32* %4
br label %69
69: ; preds = %68, %67, %37, %32, %26
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
%struct.cmsghdr = type { i32 }
%struct.in6_addr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32, i32 }
@IPV6_RTHDR_LOOSE = global i32 0
@IPV6_RTHDR_STRICT = global i32 0
define i32 @inet6_rthdr_add(%struct.cmsghdr* %0, %struct.in6_addr* %1, i32 %2) {
%4 = tail call i64 @CMSG_DATA(%struct.cmsghdr* %0)
%5 = inttoptr i64 %4 to %struct.ip6_rthdr*
%6 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %5, i64 0, i32 0
%7 = load i32, i32* %6
%8 = icmp eq i32 %7, 128
br i1 %8, label %9, label %35
9: ; preds = %3
%10 = inttoptr i64 %4 to %struct.ip6_rthdr0*
%11 = load i32, i32* @IPV6_RTHDR_LOOSE
%12 = icmp eq i32 %11, %2
%13 = load i32, i32* @IPV6_RTHDR_STRICT
%14 = icmp eq i32 %13, %2
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %35
16: ; preds = %9
%17 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %10, i64 0, i32 0
%18 = load i32, i32* %17
%19 = icmp ne i32 %18, 23
%20 = select i1 %19, i1 %12, i1 false
br i1 %20, label %21, label %35
21: ; preds = %16
%22 = add nsw i32 %18, 1
store i32 %22, i32* %17
%23 = trunc i64 %4 to i32
%24 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %10, i64 0, i32 1
%25 = load i32, i32* %24
%26 = shl i32 %25, 3
%27 = add i32 %23, 8
%28 = add i32 %27, %26
%29 = tail call i32 @bcopy(%struct.in6_addr* %1, i32 %28, i32 4)
%30 = load i32, i32* %24
%31 = shl i32 %30, 3
%32 = add i32 %31, 8
%33 = tail call i32 @CMSG_LEN(i32 %32)
%34 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %0, i64 0, i32 0
store i32 %33, i32* %34
br label %35
35: ; preds = %16, %9, %21, %3
%36 = phi i32 [ -1, %3 ], [ 0, %21 ], [ -1, %9 ], [ -1, %16 ]
ret i32 %36
}