#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;
};

/* Variables and functions */
#define IPV6_RTHDR_TYPE_0 128
int inet6_rth_segments(const void *bp) {
  struct ip6_rthdr *rh = (struct ip6_rthdr *)bp;
  struct ip6_rthdr0 *rh0;
  int addrs;
  switch (rh->ip6r_type) {
  case IPV6_RTHDR_TYPE_0:
    rh0 = (struct ip6_rthdr0 *)bp;
    if ((rh0->ip6r0_len % 2) != 0 ||
        (addrs = (rh0->ip6r0_len >> 1)) < rh0->ip6r0_segleft)
      return (-1);
    return (addrs);
  default:
    return (-1);
  }
}


x86 assembly (clang, -O0):
<inet6_rth_segments>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1175 <inet6_rth_segments+0x75>
jmp    1126 <inet6_rth_segments+0x26>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    115e <inet6_rth_segments+0x5e>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
sar    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rcx
cmp    0x4(%rcx),%eax
jge    116a <inet6_rth_segments+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    117c <inet6_rth_segments+0x7c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117c <inet6_rth_segments+0x7c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_segments>:
mov    $0xffffffff,%eax
cmpl   $0x80,(%rdi)
jne    111e <inet6_rth_segments+0x1e>
cmpl   $0x41,0x4(%rdi)
mov    $0xffffffff,%ecx
mov    $0x40,%eax
cmovge %ecx,%eax
ret

LLVM IR (-O0):
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32, i32 }
define i32 @inet6_rth_segments(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ip6_rthdr*
%5 = alloca %struct.ip6_rthdr0*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = bitcast i8* %7 to %struct.ip6_rthdr*
store %struct.ip6_rthdr* %8, %struct.ip6_rthdr** %4
%9 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %4
%10 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %9, i32 0, i32 0
%11 = load i32, i32* %10
switch i32 %11, label %32 [
i32 128, label %12
]
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = bitcast i8* %13 to %struct.ip6_rthdr0*
store %struct.ip6_rthdr0* %14, %struct.ip6_rthdr0** %5
%15 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %5
%16 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = srem i32 %17, 2
%19 = icmp ne i32 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %12
%21 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %5
%22 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = ashr i32 %23, 1
store i32 %24, i32* %6
%25 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %5
%26 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = icmp slt i32 %24, %27
br i1 %28, label %29, label %30
29: ; preds = %20, %12
store i32 -1, i32* %2
br label %33
30: ; preds = %20
%31 = load i32, i32* %6
store i32 %31, i32* %2
br label %33
32: ; preds = %1
store i32 -1, i32* %2
br label %33
33: ; preds = %32, %30, %29
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
define i32 @inet6_rth_segments(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = icmp eq i32 %3, 128
br i1 %4, label %5, label %11
5: ; preds = %1
%6 = getelementptr inbounds i8, i8* %0, i64 4
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
%9 = icmp sgt i32 %8, 64
%10 = select i1 %9, i32 -1, i32 64
br label %11
11: ; preds = %5, %1
%12 = phi i32 [ -1, %1 ], [ %10, %5 ]
ret i32 %12
}