#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 && addr2) {
    return -1;
  }
  if (!addr2 && addr1) {
    return 1;
  }
  if (!addr1 && !addr2)
    return 0;
  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)
cmpq   $0x0,-0x10(%rbp)
jne    1145 <sock_cmpaddr+0x35>
cmpq   $0x0,-0x18(%rbp)
je     1145 <sock_cmpaddr+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <sock_cmpaddr+0x1a8>
cmpq   $0x0,-0x18(%rbp)
jne    1167 <sock_cmpaddr+0x57>
cmpq   $0x0,-0x10(%rbp)
je     1167 <sock_cmpaddr+0x57>
movl   $0x1,-0x4(%rbp)
jmp    12b8 <sock_cmpaddr+0x1a8>
cmpq   $0x0,-0x10(%rbp)
jne    1189 <sock_cmpaddr+0x79>
cmpq   $0x0,-0x18(%rbp)
jne    1189 <sock_cmpaddr+0x79>
movl   $0x0,-0x4(%rbp)
jmp    12b8 <sock_cmpaddr+0x1a8>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     11af <sock_cmpaddr+0x9f>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <sock_cmpaddr+0x1a8>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
sub    $0x80,%eax
je     123a <sock_cmpaddr+0x12a>
jmp    11c8 <sock_cmpaddr+0xb8>
mov    -0x40(%rbp),%eax
sub    $0x81,%eax
jne    1299 <sock_cmpaddr+0x189>
jmp    11db <sock_cmpaddr+0xcb>
cmpl   $0x0,-0x1c(%rbp)
je     1216 <sock_cmpaddr+0x106>
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     1211 <sock_cmpaddr+0x101>
mov    -0x34(%rbp),%eax
sub    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <sock_cmpaddr+0x1a8>
jmp    1216 <sock_cmpaddr+0x106>
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    12a5 <sock_cmpaddr+0x195>
cmpl   $0x0,-0x1c(%rbp)
je     1275 <sock_cmpaddr+0x165>
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     1270 <sock_cmpaddr+0x160>
mov    -0x34(%rbp),%eax
sub    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <sock_cmpaddr+0x1a8>
jmp    1275 <sock_cmpaddr+0x165>
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    12a5 <sock_cmpaddr+0x195>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <sock_cmpaddr+0x1a8>
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    $0xffffffff,%eax
test   %rdi,%rdi
jne    111f <sock_cmpaddr+0xf>
test   %rsi,%rsi
jne    1162 <sock_cmpaddr+0x52>
test   %rdi,%rdi
je     112e <sock_cmpaddr+0x1e>
mov    $0x1,%eax
test   %rsi,%rsi
je     1162 <sock_cmpaddr+0x52>
xor    %eax,%eax
mov    %rdi,%rcx
or     %rsi,%rcx
je     1162 <sock_cmpaddr+0x52>
mov    (%rdi),%ecx
mov    %ecx,%eax
sub    (%rsi),%eax
jne    1162 <sock_cmpaddr+0x52>
and    $0xfffffffe,%ecx
mov    $0xffffffff,%eax
cmp    $0x80,%ecx
jne    1162 <sock_cmpaddr+0x52>
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 = icmp ne %struct.sockaddr* %13, null
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = load %struct.sockaddr*, %struct.sockaddr** %6
%17 = icmp ne %struct.sockaddr* %16, null
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %4
br label %113
19: ; preds = %15, %3
%20 = load %struct.sockaddr*, %struct.sockaddr** %6
%21 = icmp ne %struct.sockaddr* %20, null
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = load %struct.sockaddr*, %struct.sockaddr** %5
%24 = icmp ne %struct.sockaddr* %23, null
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 1, i32* %4
br label %113
26: ; preds = %22, %19
%27 = load %struct.sockaddr*, %struct.sockaddr** %5
%28 = icmp ne %struct.sockaddr* %27, null
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = load %struct.sockaddr*, %struct.sockaddr** %6
%31 = icmp ne %struct.sockaddr* %30, null
br i1 %31, label %33, label %32
32: ; preds = %29
store i32 0, i32* %4
br label %113
33: ; preds = %29, %26
%34 = load %struct.sockaddr*, %struct.sockaddr** %5
%35 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load %struct.sockaddr*, %struct.sockaddr** %6
%38 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = icmp ne i32 %36, %39
br i1 %40, label %41, label %49
41: ; preds = %33
%42 = load %struct.sockaddr*, %struct.sockaddr** %5
%43 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load %struct.sockaddr*, %struct.sockaddr** %6
%46 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = sub nsw i32 %44, %47
store i32 %48, i32* %4
br label %113
49: ; preds = %33
%50 = load %struct.sockaddr*, %struct.sockaddr** %5
%51 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %50, i32 0, i32 0
%52 = load i32, i32* %51
switch i32 %52, label %107 [
i32 129, label %53
i32 128, label %80
]
53: ; preds = %49
%54 = load i32, i32* %7
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %73
56: ; preds = %53
%57 = load %struct.sockaddr*, %struct.sockaddr** %5
%58 = bitcast %struct.sockaddr* %57 to %struct.sockaddr_in*
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %58, i32 0, i32 0
%60 = load i32, i32* %59
store i32 %60, i32* %10
%61 = load %struct.sockaddr*, %struct.sockaddr** %6
%62 = bitcast %struct.sockaddr* %61 to %struct.sockaddr_in*
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %62, i32 0, i32 0
%64 = load i32, i32* %63
store i32 %64, i32* %11
%65 = load i32, i32* %10
%66 = load i32, i32* %11
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %72
68: ; preds = %56
%69 = load i32, i32* %10
%70 = load i32, i32* %11
%71 = sub nsw i32 %69, %70
store i32 %71, i32* %4
br label %113
72: ; preds = %56
br label %73
73: ; preds = %72, %53
%74 = load %struct.sockaddr*, %struct.sockaddr** %5
%75 = bitcast %struct.sockaddr* %74 to %struct.sockaddr_in*
%76 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %75, i32 0, i32 1
store i32* %76, i32** %8
%77 = load %struct.sockaddr*, %struct.sockaddr** %6
%78 = bitcast %struct.sockaddr* %77 to %struct.sockaddr_in*
%79 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %78, i32 0, i32 1
store i32* %79, i32** %9
store i32 4, i32* %12
br label %108
80: ; preds = %49
%81 = load i32, i32* %7
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %100
83: ; preds = %80
%84 = load %struct.sockaddr*, %struct.sockaddr** %5
%85 = bitcast %struct.sockaddr* %84 to %struct.sockaddr_in6*
%86 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %85, i32 0, i32 0
%87 = load i32, i32* %86
store i32 %87, i32* %10
%88 = load %struct.sockaddr*, %struct.sockaddr** %6
%89 = bitcast %struct.sockaddr* %88 to %struct.sockaddr_in6*
%90 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %89, i32 0, i32 0
%91 = load i32, i32* %90
store i32 %91, i32* %11
%92 = load i32, i32* %10
%93 = load i32, i32* %11
%94 = icmp ne i32 %92, %93
br i1 %94, label %95, label %99
95: ; preds = %83
%96 = load i32, i32* %10
%97 = load i32, i32* %11
%98 = sub nsw i32 %96, %97
store i32 %98, i32* %4
br label %113
99: ; preds = %83
br label %100
100: ; preds = %99, %80
%101 = load %struct.sockaddr*, %struct.sockaddr** %5
%102 = bitcast %struct.sockaddr* %101 to %struct.sockaddr_in6*
%103 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %102, i32 0, i32 1
store i32* %103, i32** %8
%104 = load %struct.sockaddr*, %struct.sockaddr** %6
%105 = bitcast %struct.sockaddr* %104 to %struct.sockaddr_in6*
%106 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %105, i32 0, i32 1
store i32* %106, i32** %9
store i32 4, i32* %12
br label %108
107: ; preds = %49
store i32 -1, i32* %4
br label %113
108: ; preds = %100, %73
%109 = load i32*, i32** %8
%110 = load i32*, i32** %9
%111 = load i32, i32* %12
%112 = call i32 @memcmp(i32* %109, i32* %110, i32 %111)
store i32 %112, i32* %4
br label %113
113: ; preds = %108, %107, %95, %68, %41, %32, %25, %18
%114 = load i32, i32* %4
ret i32 %114
}

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