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

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


x86 assembly (clang, -O0):
<inet6_rth_add>:
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    $0x80,%eax
jne    117e <inet6_rth_add+0x6e>
jmp    113e <inet6_rth_add+0x2e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x28(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1185 <inet6_rth_add+0x75>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_add>:
push   %rax
mov    $0xffffffff,%eax
cmpl   $0x80,(%rdi)
jne    113c <inet6_rth_add+0x2c>
movl   $0x81,(%rdi)
mov    $0x200,%eax
add    0x8(%rdi),%rax
mov    %rax,%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* }
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*
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 %27 [
i32 128, 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 1
%18 = load i32*, i32** %17
%19 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%20 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
store i32 %22, i32* %20
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i32, i32* %18, i64 %23
%25 = load %struct.in6_addr*, %struct.in6_addr** %5
%26 = call i32 @memcpy(i32* %24, %struct.in6_addr* %25, i32 4)
store i32 0, i32* %3
br label %28
27: ; preds = %2
store i32 -1, i32* %3
br label %28
28: ; preds = %27, %13
%29 = load i32, i32* %3
ret i32 %29
}

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