#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_segleft;
  int ip6r0_len;
};
struct ip6_rthdr {
  int ip6r_type;
};
struct in6_addr {
  int dummy;
};

/* Variables and functions */
#define IPV6_RTHDR_TYPE_0 128
__attribute__((visibility("default"))) int
inet6_rth_add(void *bp, const struct in6_addr *addr) {
  struct ip6_rthdr *rth = (struct ip6_rthdr *)bp;
  struct ip6_rthdr0 *rth0;
  struct in6_addr *nextaddr;
  switch (rth->ip6r_type) {
  case IPV6_RTHDR_TYPE_0:
    rth0 = (struct ip6_rthdr0 *)rth;
    if (rth0->ip6r0_segleft == (rth0->ip6r0_len / 2))
      return (-1);
    nextaddr = (struct in6_addr *)(rth0 + 1) + rth0->ip6r0_segleft;
    *nextaddr = *addr;
    rth0->ip6r0_segleft++;
    break;
  default:
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<inet6_rth_add>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1199 <inet6_rth_add+0x99>
jmp    112a <inet6_rth_add+0x2a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jne    1163 <inet6_rth_add+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <inet6_rth_add+0xac>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    -0x28(%rbp),%rcx
movslq (%rcx),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    11a5 <inet6_rth_add+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <inet6_rth_add+0xac>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_add>:
mov    $0xffffffff,%eax
cmpl   $0x80,(%rdi)
jne    112b <inet6_rth_add+0x2b>
mov    0x4(%rdi),%ecx
and    $0xfffffffe,%ecx
cmp    $0x100,%ecx
je     112b <inet6_rth_add+0x2b>
mov    (%rsi),%eax
mov    %eax,0x208(%rdi)
movl   $0x81,(%rdi)
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32, i32 }
define i32 @inet6_rth_add(i8* %0, %struct.in6_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in6_addr*
%6 = alloca %struct.ip6_rthdr*
%7 = alloca %struct.ip6_rthdr0*
%8 = alloca %struct.in6_addr*
store i8* %0, i8** %4
store %struct.in6_addr* %1, %struct.in6_addr** %5
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to %struct.ip6_rthdr*
store %struct.ip6_rthdr* %10, %struct.ip6_rthdr** %6
%11 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %6
%12 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %11, i32 0, i32 0
%13 = load i32, i32* %12
switch i32 %13, label %43 [
i32 128, label %14
]
14: ; preds = %2
%15 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %6
%16 = bitcast %struct.ip6_rthdr* %15 to %struct.ip6_rthdr0*
store %struct.ip6_rthdr0* %16, %struct.ip6_rthdr0** %7
%17 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%18 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%21 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = sdiv i32 %22, 2
%24 = icmp eq i32 %19, %23
br i1 %24, label %25, label %26
25: ; preds = %14
store i32 -1, i32* %3
br label %45
26: ; preds = %14
%27 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%28 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %27, i64 1
%29 = bitcast %struct.ip6_rthdr0* %28 to %struct.in6_addr*
%30 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%31 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %29, i64 %33
store %struct.in6_addr* %34, %struct.in6_addr** %8
%35 = load %struct.in6_addr*, %struct.in6_addr** %8
%36 = load %struct.in6_addr*, %struct.in6_addr** %5
%37 = bitcast %struct.in6_addr* %35 to i8*
%38 = bitcast %struct.in6_addr* %36 to i8*
%39 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%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
br label %44
43: ; preds = %2
store i32 -1, i32* %3
br label %45
44: ; preds = %26
store i32 0, i32* %3
br label %45
45: ; preds = %44, %43, %25
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
define i32 @inet6_rth_add(i8* nocapture %0, %struct.in6_addr* nocapture %1) {
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 128
br i1 %5, label %6, label %17
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 4
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
%10 = and i32 %9, -2
%11 = icmp eq i32 %10, 256
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = getelementptr %struct.in6_addr, %struct.in6_addr* %1, i64 0, i32 0
%14 = getelementptr inbounds i8, i8* %0, i64 520
%15 = bitcast i8* %14 to i32*
%16 = load i32, i32* %13
store i32 %16, i32* %15
store i32 129, i32* %3
br label %17
17: ; preds = %2, %6, %12
%18 = phi i32 [ 0, %12 ], [ -1, %6 ], [ -1, %2 ]
ret i32 %18
}