#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 */
typedef int /*<<< orphan*/ uint8_t;
struct sockaddr_in6 {
  int sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
struct in_addr {
  int dummy;
};
struct in6_addr {
  int dummy;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int memcmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int sock_cmpaddr(const struct sockaddr *addr1, const struct sockaddr *addr2,
                 int cmpport) {
  uint8_t *s1, *s2;
  int p1, p2;
  int slen;
  if (addr1->sa_family != addr2->sa_family) {
    return addr1->sa_family - addr2->sa_family;
  }
  switch (addr1->sa_family) {
  case AF_INET:
    if (cmpport) {
      p1 = ((struct sockaddr_in *)addr1)->sin_port;
      p2 = ((struct sockaddr_in *)addr2)->sin_port;
      if (p1 != p2)
        return p1 - p2;
    }
    s1 = (uint8_t *)&(((struct sockaddr_in *)addr1)->sin_addr);
    s2 = (uint8_t *)&(((struct sockaddr_in *)addr2)->sin_addr);
    slen = sizeof(struct in_addr);
    break;
  case AF_INET6:
    if (cmpport) {
      p1 = ((struct sockaddr_in6 *)addr1)->sin6_port;
      p2 = ((struct sockaddr_in6 *)addr2)->sin6_port;
      if (p1 != p2)
        return p1 - p2;
    }
    s1 = (uint8_t *)&(((struct sockaddr_in6 *)addr1)->sin6_addr);
    s2 = (uint8_t *)&(((struct sockaddr_in6 *)addr2)->sin6_addr);
    slen = sizeof(struct in6_addr);
    break;
  default:
    return -1;
  }
  return memcmp(s1, s2, slen);
}


x86 assembly (clang, -O0):
<sock_cmpaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     1149 <sock_cmpaddr+0x39>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1252 <sock_cmpaddr+0x142>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
sub    $0x80,%eax
je     11d4 <sock_cmpaddr+0xc4>
jmp    1162 <sock_cmpaddr+0x52>
mov    -0x40(%rbp),%eax
sub    $0x81,%eax
jne    1233 <sock_cmpaddr+0x123>
jmp    1175 <sock_cmpaddr+0x65>
cmpl   $0x0,-0x1c(%rbp)
je     11b0 <sock_cmpaddr+0xa0>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
je     11ab <sock_cmpaddr+0x9b>
mov    -0x34(%rbp),%eax
sub    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1252 <sock_cmpaddr+0x142>
jmp    11b0 <sock_cmpaddr+0xa0>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x3c(%rbp)
jmp    123f <sock_cmpaddr+0x12f>
cmpl   $0x0,-0x1c(%rbp)
je     120f <sock_cmpaddr+0xff>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
je     120a <sock_cmpaddr+0xfa>
mov    -0x34(%rbp),%eax
sub    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1252 <sock_cmpaddr+0x142>
jmp    120f <sock_cmpaddr+0xff>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x3c(%rbp)
jmp    123f <sock_cmpaddr+0x12f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <sock_cmpaddr+0x142>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <memcmp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_cmpaddr>:
mov    (%rdi),%ecx
mov    %ecx,%eax
sub    (%rsi),%eax
jne    113a <sock_cmpaddr+0x2a>
and    $0xfffffffe,%ecx
mov    $0xffffffff,%eax
cmp    $0x80,%ecx
jne    113a <sock_cmpaddr+0x2a>
add    $0x4,%rsi
add    $0x4,%rdi
mov    $0x4,%edx
jmp    1030 <memcmp@plt>
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
define i32 @sock_cmpaddr(%struct.sockaddr* %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca %struct.sockaddr*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32 %2, i32* %7
%13 = load %struct.sockaddr*, %struct.sockaddr** %5
%14 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load %struct.sockaddr*, %struct.sockaddr** %6
%17 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = icmp ne i32 %15, %18
br i1 %19, label %20, label %28
20: ; preds = %3
%21 = load %struct.sockaddr*, %struct.sockaddr** %5
%22 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load %struct.sockaddr*, %struct.sockaddr** %6
%25 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sub nsw i32 %23, %26
store i32 %27, i32* %4
br label %92
28: ; preds = %3
%29 = load %struct.sockaddr*, %struct.sockaddr** %5
%30 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %29, i32 0, i32 0
%31 = load i32, i32* %30
switch i32 %31, label %86 [
i32 129, label %32
i32 128, label %59
]
32: ; preds = %28
%33 = load i32, i32* %7
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %52
35: ; preds = %32
%36 = load %struct.sockaddr*, %struct.sockaddr** %5
%37 = bitcast %struct.sockaddr* %36 to %struct.sockaddr_in*
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %37, i32 0, i32 0
%39 = load i32, i32* %38
store i32 %39, i32* %10
%40 = load %struct.sockaddr*, %struct.sockaddr** %6
%41 = bitcast %struct.sockaddr* %40 to %struct.sockaddr_in*
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %41, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %11
%44 = load i32, i32* %10
%45 = load i32, i32* %11
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %35
%48 = load i32, i32* %10
%49 = load i32, i32* %11
%50 = sub nsw i32 %48, %49
store i32 %50, i32* %4
br label %92
51: ; preds = %35
br label %52
52: ; preds = %51, %32
%53 = load %struct.sockaddr*, %struct.sockaddr** %5
%54 = bitcast %struct.sockaddr* %53 to %struct.sockaddr_in*
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i32 0, i32 1
store i32* %55, i32** %8
%56 = load %struct.sockaddr*, %struct.sockaddr** %6
%57 = bitcast %struct.sockaddr* %56 to %struct.sockaddr_in*
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %57, i32 0, i32 1
store i32* %58, i32** %9
store i32 4, i32* %12
br label %87
59: ; preds = %28
%60 = load i32, i32* %7
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %79
62: ; preds = %59
%63 = load %struct.sockaddr*, %struct.sockaddr** %5
%64 = bitcast %struct.sockaddr* %63 to %struct.sockaddr_in6*
%65 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %64, i32 0, i32 0
%66 = load i32, i32* %65
store i32 %66, i32* %10
%67 = load %struct.sockaddr*, %struct.sockaddr** %6
%68 = bitcast %struct.sockaddr* %67 to %struct.sockaddr_in6*
%69 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %68, i32 0, i32 0
%70 = load i32, i32* %69
store i32 %70, i32* %11
%71 = load i32, i32* %10
%72 = load i32, i32* %11
%73 = icmp ne i32 %71, %72
br i1 %73, label %74, label %78
74: ; preds = %62
%75 = load i32, i32* %10
%76 = load i32, i32* %11
%77 = sub nsw i32 %75, %76
store i32 %77, i32* %4
br label %92
78: ; preds = %62
br label %79
79: ; preds = %78, %59
%80 = load %struct.sockaddr*, %struct.sockaddr** %5
%81 = bitcast %struct.sockaddr* %80 to %struct.sockaddr_in6*
%82 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %81, i32 0, i32 1
store i32* %82, i32** %8
%83 = load %struct.sockaddr*, %struct.sockaddr** %6
%84 = bitcast %struct.sockaddr* %83 to %struct.sockaddr_in6*
%85 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %84, i32 0, i32 1
store i32* %85, i32** %9
store i32 4, i32* %12
br label %87
86: ; preds = %28
store i32 -1, i32* %4
br label %92
87: ; preds = %79, %52
%88 = load i32*, i32** %8
%89 = load i32*, i32** %9
%90 = load i32, i32* %12
%91 = call i32 @memcmp(i32* %88, i32* %89, i32 %90)
store i32 %91, i32* %4
br label %92
92: ; preds = %87, %86, %74, %47, %20
%93 = load i32, i32* %4
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define i32 @sock_cmpaddr(%struct.sockaddr* %0, %struct.sockaddr* %1, i32 %2) {
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%7 = load i32, i32* %6
%8 = icmp eq i32 %5, %7
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = sub nsw i32 %5, %7
br label %18
11: ; preds = %3
%12 = and i32 %5, -2
%13 = icmp eq i32 %12, 128
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 1, i32 0
%16 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
%17 = tail call i32 @memcmp(i32* %16, i32* %15, i32 4)
br label %18
18: ; preds = %11, %14, %9
%19 = phi i32 [ %10, %9 ], [ %17, %14 ], [ -1, %11 ]
ret i32 %19
}