#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct TYPE_2__ {
  int sa_family;
};
struct ifreq {
  int /*<<< orphan*/ ifr_name;
  TYPE_1__ ifr_addr;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ strncmp(char const *, int /*<<< orphan*/, int);
int chkifr(const struct ifreq *ifr, const char *ifname, const size_t addrlen,
           size_t *offset) {
  size_t sa_len = 0;
  assert(ifr && ifname && offset);
  switch (ifr->ifr_addr.sa_family) {
  case AF_INET6:
    sa_len = sizeof(struct sockaddr_in6);
    break;
  case AF_INET:
    sa_len = sizeof(struct sockaddr);
    break;
  default:
    sa_len = 0;
    break;
  }
  if (sa_len > 0) {
    if ((ifr->ifr_addr.sa_family == AF_INET) &&
        (0 == strncmp(ifname, ifr->ifr_name, sizeof(struct ifreq))) &&
        (addrlen >= sa_len)) {
      *offset = sa_len;
      return 0;
    }
  }
  *offset = sizeof(*ifr);
  return -1;
}


x86 assembly (clang, -O0):
<chkifr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x31(%rbp)
je     116b <chkifr+0x4b>
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x31(%rbp)
je     116b <chkifr+0x4b>
cmpq   $0x0,-0x28(%rbp)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
movzbl %al,%edi
and    $0x1,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x38(%rbp)
sub    $0x80,%eax
je     11a6 <chkifr+0x86>
jmp    1193 <chkifr+0x73>
mov    -0x38(%rbp),%eax
sub    $0x81,%eax
je     11b3 <chkifr+0x93>
jmp    11c0 <chkifr+0xa0>
movq   $0x4,-0x30(%rbp)
jmp    11c8 <chkifr+0xa8>
movq   $0x4,-0x30(%rbp)
jmp    11c8 <chkifr+0xa8>
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    1230 <chkifr+0x110>
mov    -0x10(%rbp),%rax
cmpl   $0x81,0x4(%rax)
jne    122b <chkifr+0x10b>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    $0x8,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    122b <chkifr+0x10b>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jb     122b <chkifr+0x10b>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1242 <chkifr+0x122>
jmp    1230 <chkifr+0x110>
mov    -0x28(%rbp),%rax
movq   $0x8,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chkifr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
test   %rdi,%rdi
setne  %al
test   %rsi,%rsi
setne  %cl
and    %al,%cl
test   %r15,%r15
setne  %al
and    %cl,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x81,0x4(%rbx)
jne    117e <chkifr+0x5e>
mov    (%rbx),%esi
mov    %r12,%rdi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x4,%r14
jb     117e <chkifr+0x5e>
test   %rax,%rax
jne    117e <chkifr+0x5e>
xor    %eax,%eax
mov    $0x4,%ecx
jmp    1188 <chkifr+0x68>
mov    $0xffffffff,%eax
mov    $0x8,%ecx
mov    %rcx,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
define i32 @chkifr(%struct.ifreq* %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca %struct.ifreq*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i64
store %struct.ifreq* %0, %struct.ifreq** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64* %3, i64** %9
store i64 0, i64* %10
%11 = load %struct.ifreq*, %struct.ifreq** %6
%12 = icmp ne %struct.ifreq* %11, null
br i1 %12, label %13, label %19
13: ; preds = %4
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i64*, i64** %9
%18 = icmp ne i64* %17, null
br label %19
19: ; preds = %16, %13, %4
%20 = phi i1 [ false, %13 ], [ false, %4 ], [ %18, %16 ]
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load %struct.ifreq*, %struct.ifreq** %6
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %23, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = load i32, i32* %25
switch i32 %26, label %29 [
i32 128, label %27
i32 129, label %28
]
27: ; preds = %19
store i64 4, i64* %10
br label %30
28: ; preds = %19
store i64 4, i64* %10
br label %30
29: ; preds = %19
store i64 0, i64* %10
br label %30
30: ; preds = %29, %28, %27
%31 = load i64, i64* %10
%32 = icmp ugt i64 %31, 0
br i1 %32, label %33, label %54
33: ; preds = %30
%34 = load %struct.ifreq*, %struct.ifreq** %6
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %34, i32 0, i32 1
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = icmp eq i32 %37, 129
br i1 %38, label %39, label %53
39: ; preds = %33
%40 = load i8*, i8** %7
%41 = load %struct.ifreq*, %struct.ifreq** %6
%42 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i64 @strncmp(i8* %40, i32 %43, i32 8)
%45 = icmp eq i64 0, %44
br i1 %45, label %46, label %53
46: ; preds = %39
%47 = load i64, i64* %8
%48 = load i64, i64* %10
%49 = icmp uge i64 %47, %48
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i64, i64* %10
%52 = load i64*, i64** %9
store i64 %51, i64* %52
store i32 0, i32* %5
br label %56
53: ; preds = %46, %39, %33
br label %54
54: ; preds = %53, %30
%55 = load i64*, i64** %9
store i64 8, i64* %55
store i32 -1, i32* %5
br label %56
56: ; preds = %54, %50
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
%struct.ifreq = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
define i32 @chkifr(%struct.ifreq* %0, i8* %1, i64 %2, i64* writeonly %3) {
%5 = icmp ne %struct.ifreq* %0, null
%6 = icmp ne i8* %1, null
%7 = and i1 %5, %6
%8 = icmp ne i64* %3, null
%9 = and i1 %7, %8
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %0, i64 0, i32 1, i32 0
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 129
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %0, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i64 @strncmp(i8* %1, i32 %17, i32 8)
%19 = icmp ne i64 %18, 0
%20 = icmp ult i64 %2, 4
%21 = or i1 %20, %19
br i1 %21, label %22, label %23
22: ; preds = %15, %4
br label %23
23: ; preds = %15, %22
%24 = phi i64 [ 8, %22 ], [ 4, %15 ]
%25 = phi i32 [ -1, %22 ], [ 0, %15 ]
store i64 %24, i64* %3
ret i32 %25
}