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

/* Variables and functions */
#define IPV6_RTHDR_TYPE_0 128
int /*<<< orphan*/ memmove(void *, void *, int);
__attribute__((visibility("default"))) int inet6_rth_reverse(const void *in,
                                                             void *out) {
  struct ip6_rthdr *rth_in = (struct ip6_rthdr *)in;
  struct ip6_rthdr0 *rth0_in, *rth0_out;
  int i, segments;
  switch (rth_in->ip6r_type) {
  case IPV6_RTHDR_TYPE_0:
    rth0_in = (struct ip6_rthdr0 *)in;
    rth0_out = (struct ip6_rthdr0 *)out;
    if (rth0_in->ip6r0_len % 2)
      return (-1);
    segments = rth0_in->ip6r0_len / 2;
    memmove((void *)rth0_out, (void *)rth0_in, ((rth0_in->ip6r0_len) + 1) << 3);
    rth0_out->ip6r0_segleft = segments;
    for (i = 0; i < segments / 2; i++) {
      struct in6_addr addr_tmp, *addr1, *addr2;
      addr1 = (struct in6_addr *)(rth0_out + 1) + i;
      addr2 = (struct in6_addr *)(rth0_out + 1) + (segments - i - 1);
      addr_tmp = *addr1;
      *addr1 = *addr2;
      *addr2 = addr_tmp;
    }
    break;
  default:
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<inet6_rth_reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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    1231 <inet6_rth_reverse+0x121>
jmp    113e <inet6_rth_reverse+0x2e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1171 <inet6_rth_reverse+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <inet6_rth_reverse+0x134>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
add    $0x1,%edx
shl    $0x3,%edx
call   1030 <memmove@plt>
mov    -0x38(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x38(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
cmp    %ecx,%eax
jge    122c <inet6_rth_reverse+0x11c>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
movslq -0x34(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    -0x38(%rbp),%ecx
sub    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rax
mov    -0x40(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11ac <inet6_rth_reverse+0x9c>
jmp    123d <inet6_rth_reverse+0x12d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <inet6_rth_reverse+0x134>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_reverse>:
push   %rbx
mov    $0xffffffff,%eax
cmpl   $0x80,(%rdi)
jne    1178 <inet6_rth_reverse+0x68>
mov    %rsi,%rbx
mov    %rdi,%rsi
mov    %rbx,%rdi
mov    $0x408,%edx
call   1030 <memmove@plt>
movl   $0x40,0x4(%rbx)
lea    0x104(%rbx),%rcx
xor    %eax,%eax
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rbx,%rdx,4),%esi
mov    (%rcx),%edi
mov    %edi,0x8(%rbx,%rdx,4)
mov    %esi,(%rcx)
mov    0xc(%rbx,%rdx,4),%esi
mov    -0x4(%rcx),%edi
mov    %edi,0xc(%rbx,%rdx,4)
mov    %esi,-0x4(%rcx)
add    $0x2,%rdx
add    $0xfffffffffffffff8,%rcx
cmp    $0x20,%rdx
jne    1150 <inet6_rth_reverse+0x40>
pop    %rbx
ret

LLVM IR (-O0):
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32, i32 }
%struct.in6_addr = type { i32 }
define i32 @inet6_rth_reverse(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ip6_rthdr*
%7 = alloca %struct.ip6_rthdr0*
%8 = alloca %struct.ip6_rthdr0*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.in6_addr
%12 = alloca %struct.in6_addr*
%13 = alloca %struct.in6_addr*
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %4
%15 = bitcast i8* %14 to %struct.ip6_rthdr*
store %struct.ip6_rthdr* %15, %struct.ip6_rthdr** %6
%16 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %6
%17 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %16, i32 0, i32 0
%18 = load i32, i32* %17
switch i32 %18, label %83 [
i32 128, label %19
]
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = bitcast i8* %20 to %struct.ip6_rthdr0*
store %struct.ip6_rthdr0* %21, %struct.ip6_rthdr0** %7
%22 = load i8*, i8** %5
%23 = bitcast i8* %22 to %struct.ip6_rthdr0*
store %struct.ip6_rthdr0* %23, %struct.ip6_rthdr0** %8
%24 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%25 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = srem i32 %26, 2
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %19
store i32 -1, i32* %3
br label %85
30: ; preds = %19
%31 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%32 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = sdiv i32 %33, 2
store i32 %34, i32* %10
%35 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %8
%36 = bitcast %struct.ip6_rthdr0* %35 to i8*
%37 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%38 = bitcast %struct.ip6_rthdr0* %37 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
%43 = shl i32 %42, 3
%44 = call i32 @memmove(i8* %36, i8* %38, i32 %43)
%45 = load i32, i32* %10
%46 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %8
%47 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %46, i32 0, i32 1
store i32 %45, i32* %47
store i32 0, i32* %9
br label %48
48: ; preds = %79, %30
%49 = load i32, i32* %9
%50 = load i32, i32* %10
%51 = sdiv i32 %50, 2
%52 = icmp slt i32 %49, %51
br i1 %52, label %53, label %82
53: ; preds = %48
%54 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %8
%55 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %54, i64 1
%56 = bitcast %struct.ip6_rthdr0* %55 to %struct.in6_addr*
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %56, i64 %58
store %struct.in6_addr* %59, %struct.in6_addr** %12
%60 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %8
%61 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %60, i64 1
%62 = bitcast %struct.ip6_rthdr0* %61 to %struct.in6_addr*
%63 = load i32, i32* %10
%64 = load i32, i32* %9
%65 = sub nsw i32 %63, %64
%66 = sub nsw i32 %65, 1
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %62, i64 %67
store %struct.in6_addr* %68, %struct.in6_addr** %13
%69 = load %struct.in6_addr*, %struct.in6_addr** %12
%70 = bitcast %struct.in6_addr* %11 to i8*
%71 = bitcast %struct.in6_addr* %69 to i8*
%72 = load %struct.in6_addr*, %struct.in6_addr** %12
%73 = load %struct.in6_addr*, %struct.in6_addr** %13
%74 = bitcast %struct.in6_addr* %72 to i8*
%75 = bitcast %struct.in6_addr* %73 to i8*
%76 = load %struct.in6_addr*, %struct.in6_addr** %13
%77 = bitcast %struct.in6_addr* %76 to i8*
%78 = bitcast %struct.in6_addr* %11 to i8*
br label %79
79: ; preds = %53
%80 = load i32, i32* %9
%81 = add nsw i32 %80, 1
store i32 %81, i32* %9
br label %48
82: ; preds = %48
br label %84
83: ; preds = %2
store i32 -1, i32* %3
br label %85
84: ; preds = %82
store i32 0, i32* %3
br label %85
85: ; preds = %84, %83, %29
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
define i32 @inet6_rth_reverse(i8* %0, i8* %1) {
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 128
br i1 %5, label %6, label %27
6: ; preds = %2
%7 = tail call i32 @memmove(i8* %1, i8* %0, i32 1032)
%8 = getelementptr inbounds i8, i8* %1, i64 4
%9 = bitcast i8* %8 to i32*
store i32 64, i32* %9
%10 = getelementptr inbounds i8, i8* %1, i64 8
%11 = bitcast i8* %10 to %struct.in6_addr*
br label %12
12: ; preds = %12, %6
%13 = phi i64 [ 0, %6 ], [ %25, %12 ]
%14 = sub nuw nsw i64 63, %13
%15 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %11, i64 %13, i32 0
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %11, i64 %14, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %15
store i32 %16, i32* %17
%19 = or i64 %13, 1
%20 = sub nsw i64 62, %13
%21 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %11, i64 %19, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %11, i64 %20, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %21
store i32 %22, i32* %23
%25 = add nuw nsw i64 %13, 2
%26 = icmp eq i64 %25, 32
br i1 %26, label %27, label %12
27: ; preds = %12, %2
%28 = phi i32 [ -1, %2 ], [ 0, %12 ]
ret i32 %28
}