#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 /*<<< orphan*/ ip6r0_segleft;
  int /*<<< orphan*/ *ip6r0_addr;
};
struct ip6_rthdr {
  int ip6r_type;
};
struct in6_addr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct in6_addr const *, int);
int inet6_rth_add2(void *bp, const struct in6_addr *addr) {
  struct ip6_rthdr *rthdr = (struct ip6_rthdr *)bp;
  switch (rthdr->ip6r_type) {
    struct ip6_rthdr0 *rthdr0;
  case 2:
    rthdr0 = (struct ip6_rthdr0 *)rthdr;
    if (rthdr0->ip6r0_len * 8 / sizeof(struct in6_addr) -
            rthdr0->ip6r0_segleft <
        1)
      return -1;
    memcpy(&rthdr0->ip6r0_addr[rthdr0->ip6r0_segleft++], addr,
           sizeof(struct in6_addr));
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<inet6_rth_add2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
sub    $0x2,%eax
jne    11ae <inet6_rth_add2+0x9e>
jmp    113c <inet6_rth_add2+0x2c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
shl    $0x3,%eax
cltq
shr    $0x2,%rax
mov    -0x28(%rbp),%rcx
movslq 0x4(%rcx),%rcx
sub    %rcx,%rax
cmp    $0x1,%rax
jae    1174 <inet6_rth_add2+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <inet6_rth_add2+0xa5>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x28(%rbp),%rcx
mov    0x4(%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,0x4(%rcx)
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11b5 <inet6_rth_add2+0xa5>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_add2>:
push   %rax
mov    $0xffffffff,%eax
cmpl   $0x2,(%rdi)
jne    1142 <inet6_rth_add2+0x32>
movslq 0x4(%rdi),%rcx
cmp    $0x4,%rcx
je     1142 <inet6_rth_add2+0x32>
lea    0x1(%rcx),%eax
mov    %eax,0x4(%rdi)
shl    $0x2,%rcx
add    0x8(%rdi),%rcx
mov    %rcx,%rdi
mov    $0x4,%edx
call   1030 <memcpy@plt>
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
define i32 @inet6_rth_add2(i8* nocapture %0, %struct.in6_addr* %1) {
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 2
br i1 %5, label %6, label %19
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 4
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
%10 = icmp eq i32 %9, 4
br i1 %10, label %19, label %11
11: ; preds = %6
%12 = sext i32 %9 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 8
%14 = bitcast i8* %13 to i32**
%15 = load i32*, i32** %14
%16 = add nsw i32 %9, 1
store i32 %16, i32* %8
%17 = getelementptr inbounds i32, i32* %15, i64 %12
%18 = tail call i32 @memcpy(i32* %17, %struct.in6_addr* %1, i32 4)
br label %19
19: ; preds = %2, %6, %11
%20 = phi i32 [ 0, %11 ], [ -1, %6 ], [ -1, %2 ]
ret i32 %20
}