#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 /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int sa_family;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int ls_addr_cmp6(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int ntohs(int /*<<< orphan*/);
int ls_sockaddr_cmp(const struct sockaddr *sa, const struct sockaddr *sb) {
  int diff;
  struct sockaddr_in *a_in, *b_in;
  struct sockaddr_in6 *a_in6, *b_in6;
  if (sa == NULL) {
    if (sb == NULL) {
      return 0;
    }
    return -1;
  }
  if (sb == NULL) {
    return 1;
  }
  diff = sa->sa_family - sb->sa_family;
  if (diff != 0) {
    return diff;
  }
  switch (sa->sa_family) {
  case AF_INET:
    a_in = (struct sockaddr_in *)sa;
    b_in = (struct sockaddr_in *)sb;
    diff = a_in->sin_addr.s_addr - b_in->sin_addr.s_addr;
    if (diff != 0) {
      return diff;
    }
    return ntohs(a_in->sin_port) - ntohs(b_in->sin_port);
    break;
  case AF_INET6:
    a_in6 = (struct sockaddr_in6 *)sa;
    b_in6 = (struct sockaddr_in6 *)sb;
    diff = ls_addr_cmp6(&a_in6->sin6_addr, &b_in6->sin6_addr);
    if (diff != 0) {
      return diff;
    }
    return ntohs(a_in6->sin6_port) - ntohs(b_in6->sin6_port);
    break;
  default:
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ls_sockaddr_cmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115e <ls_sockaddr_cmp+0x3e>
cmpq   $0x0,-0x18(%rbp)
jne    1152 <ls_sockaddr_cmp+0x32>
movl   $0x0,-0x4(%rbp)
jmp    1294 <ls_sockaddr_cmp+0x174>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <ls_sockaddr_cmp+0x174>
cmpq   $0x0,-0x18(%rbp)
jne    1175 <ls_sockaddr_cmp+0x55>
movl   $0x1,-0x4(%rbp)
jmp    1294 <ls_sockaddr_cmp+0x174>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1199 <ls_sockaddr_cmp+0x79>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <ls_sockaddr_cmp+0x174>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
sub    $0x80,%eax
je     1223 <ls_sockaddr_cmp+0x103>
jmp    11b2 <ls_sockaddr_cmp+0x92>
mov    -0x44(%rbp),%eax
sub    $0x81,%eax
jne    1288 <ls_sockaddr_cmp+0x168>
jmp    11c5 <ls_sockaddr_cmp+0xa5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x30(%rbp),%rcx
sub    0x4(%rcx),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11fb <ls_sockaddr_cmp+0xdb>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <ls_sockaddr_cmp+0x174>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <ls_sockaddr_cmp+0x174>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
add    $0x4,%rdi
mov    -0x40(%rbp),%rsi
add    $0x4,%rsi
call   1030 <ls_addr_cmp6@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1260 <ls_sockaddr_cmp+0x140>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <ls_sockaddr_cmp+0x174>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <ls_sockaddr_cmp+0x174>
jmp    128d <ls_sockaddr_cmp+0x16d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_sockaddr_cmp>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
test   %rdi,%rdi
je     115d <ls_sockaddr_cmp+0x3d>
test   %r14,%r14
je     1164 <ls_sockaddr_cmp+0x44>
mov    %rdi,%rbx
mov    (%rdi),%eax
mov    %eax,%ebp
sub    (%r14),%ebp
jne    1169 <ls_sockaddr_cmp+0x49>
cmp    $0x80,%eax
je     1170 <ls_sockaddr_cmp+0x50>
xor    %ebp,%ebp
cmp    $0x81,%eax
jne    1169 <ls_sockaddr_cmp+0x49>
mov    0x4(%rbx),%ebp
sub    0x4(%r14),%ebp
jne    1169 <ls_sockaddr_cmp+0x49>
mov    $0x81,%edi
jmp    1185 <ls_sockaddr_cmp+0x65>
neg    %r14
sbb    %ebp,%ebp
jmp    1169 <ls_sockaddr_cmp+0x49>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0x4(%rbx),%rdi
lea    0x4(%r14),%rsi
call   1030 <ls_addr_cmp6@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1169 <ls_sockaddr_cmp+0x49>
mov    (%rbx),%edi
call   1040 <ntohs@plt>
mov    %eax,%ebp
mov    (%r14),%edi
call   1040 <ntohs@plt>
sub    %eax,%ebp
jmp    1169 <ls_sockaddr_cmp+0x49>

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define i32 @ls_sockaddr_cmp(%struct.sockaddr* %0, %struct.sockaddr* %1) {
%3 = icmp eq %struct.sockaddr* %0, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = icmp ne %struct.sockaddr* %1, null
%6 = sext i1 %5 to i32
br label %40
7: ; preds = %2
%8 = icmp eq %struct.sockaddr* %1, null
br i1 %8, label %40, label %9
9: ; preds = %7
%10 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = sub nsw i32 %11, %13
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %40
16: ; preds = %9
switch i32 %11, label %40 [
i32 129, label %17
i32 128, label %29
]
17: ; preds = %16
%18 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 1, i32 0
%21 = load i32, i32* %20
%22 = sub nsw i32 %19, %21
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %17
%25 = tail call i32 @ntohs(i32 129)
%26 = load i32, i32* %12
%27 = tail call i32 @ntohs(i32 %26)
%28 = sub nsw i32 %25, %27
br label %40
29: ; preds = %16
%30 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
%31 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 1, i32 0
%32 = tail call i32 @ls_addr_cmp6(i32* %30, i32* %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i32, i32* %10
%36 = tail call i32 @ntohs(i32 %35)
%37 = load i32, i32* %12
%38 = tail call i32 @ntohs(i32 %37)
%39 = sub nsw i32 %36, %38
br label %40
40: ; preds = %16, %29, %17, %9, %7, %4, %34, %24
%41 = phi i32 [ %39, %34 ], [ %28, %24 ], [ %6, %4 ], [ 1, %7 ], [ %14, %9 ], [ %22, %17 ], [ %32, %29 ], [ 0, %16 ]
ret i32 %41
}