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

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
unsigned int IPV6_RTHDR_LOOSE;
#define IPV6_RTHDR_TYPE_0 128
int inet6_rthdr_lasthop(struct cmsghdr *cmsg, unsigned int flags) {
  struct ip6_rthdr *rthdr;
  rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg);
  switch (rthdr->ip6r_type) {
  case IPV6_RTHDR_TYPE_0: {
    struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    if (rt0->ip6r0_segleft > 23)
      return (-1);
    if (flags != IPV6_RTHDR_LOOSE)
      return (-1);
    break;
  }
  default:
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<inet6_rthdr_lasthop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1186 <inet6_rthdr_lasthop+0x76>
jmp    1142 <inet6_rthdr_lasthop+0x32>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpl   $0x17,(%rax)
jle    1163 <inet6_rthdr_lasthop+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1199 <inet6_rthdr_lasthop+0x89>
mov    -0x14(%rbp),%eax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%eax
je     1181 <inet6_rthdr_lasthop+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1199 <inet6_rthdr_lasthop+0x89>
jmp    1192 <inet6_rthdr_lasthop+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    1199 <inet6_rthdr_lasthop+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rthdr_lasthop>:
push   %rbx
mov    %esi,%ebx
call   1030 <CMSG_DATA@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpl   $0x80,(%rcx)
jne    1141 <inet6_rthdr_lasthop+0x31>
cmpl   $0x18,(%rcx)
setge  %al
mov    0x2eb3(%rip),%rcx
cmp    %ebx,(%rcx)
setne  %cl
or     %al,%cl
movzbl %cl,%eax
neg    %eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32 }
@IPV6_RTHDR_LOOSE = global i32 0
define i32 @inet6_rthdr_lasthop(%struct.cmsghdr* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.cmsghdr*
%5 = alloca i32
%6 = alloca %struct.ip6_rthdr*
%7 = alloca %struct.ip6_rthdr0*
store %struct.cmsghdr* %0, %struct.cmsghdr** %4
store i32 %1, i32* %5
%8 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%9 = call i64 @CMSG_DATA(%struct.cmsghdr* %8)
%10 = inttoptr i64 %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 %28 [
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 = icmp sgt i32 %19, 23
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 -1, i32* %3
br label %30
22: ; preds = %14
%23 = load i32, i32* %5
%24 = load i32, i32* @IPV6_RTHDR_LOOSE
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %3
br label %30
27: ; preds = %22
br label %29
28: ; preds = %2
store i32 -1, i32* %3
br label %30
29: ; preds = %27
store i32 0, i32* %3
br label %30
30: ; preds = %29, %28, %26, %21
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.cmsghdr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32 }
@IPV6_RTHDR_LOOSE = global i32 0
define i32 @inet6_rthdr_lasthop(%struct.cmsghdr* %0, i32 %1) {
%3 = tail call i64 @CMSG_DATA(%struct.cmsghdr* %0)
%4 = inttoptr i64 %3 to %struct.ip6_rthdr*
%5 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %4, i64 0, i32 0
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 128
br i1 %7, label %8, label %17
8: ; preds = %2
%9 = inttoptr i64 %3 to %struct.ip6_rthdr0*
%10 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %9, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp sgt i32 %11, 23
%13 = load i32, i32* @IPV6_RTHDR_LOOSE
%14 = icmp ne i32 %13, %1
%15 = select i1 %12, i1 true, i1 %14
%16 = sext i1 %15 to i32
br label %17
17: ; preds = %8, %2
%18 = phi i32 [ -1, %2 ], [ %16, %8 ]
ret i32 %18
}