#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 sockaddr_in6 {
  void *sin6_port;
};
struct sockaddr_in {
  void *sin_port;
};
struct sockaddr {
  int sa_family;
};
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
void *htons(int /*<<< orphan*/);
void sockaddr_set_port(struct sockaddr *sa, in_port_t port) {
  switch (sa->sa_family) {
  case AF_INET:
    ((struct sockaddr_in *)sa)->sin_port = htons(port);
    break;
  case AF_INET6:
    ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
    break;
  }
}


x86 assembly (clang, -O0):
<sockaddr_set_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
sub    $0x80,%eax
je     1162 <sockaddr_set_port+0x52>
jmp    1138 <sockaddr_set_port+0x28>
mov    -0x10(%rbp),%eax
sub    $0x81,%eax
jne    1174 <sockaddr_set_port+0x64>
jmp    114b <sockaddr_set_port+0x3b>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1174 <sockaddr_set_port+0x64>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr_set_port>:
push   %rbx
mov    (%rdi),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    112a <sockaddr_set_port+0x1a>
mov    %rdi,%rbx
mov    %esi,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i8* }
%struct.sockaddr_in6 = type { i8* }
define void @sockaddr_set_port(%struct.sockaddr* %0, i32 %1) {
%3 = alloca %struct.sockaddr*
%4 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %3
store i32 %1, i32* %4
%5 = load %struct.sockaddr*, %struct.sockaddr** %3
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i32 0, i32 0
%7 = load i32, i32* %6
switch i32 %7, label %20 [
i32 129, label %8
i32 128, label %14
]
8: ; preds = %2
%9 = load i32, i32* %4
%10 = call i8* @htons(i32 %9)
%11 = load %struct.sockaddr*, %struct.sockaddr** %3
%12 = bitcast %struct.sockaddr* %11 to %struct.sockaddr_in*
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i8* %10, i8** %13
br label %20
14: ; preds = %2
%15 = load i32, i32* %4
%16 = call i8* @htons(i32 %15)
%17 = load %struct.sockaddr*, %struct.sockaddr** %3
%18 = bitcast %struct.sockaddr* %17 to %struct.sockaddr_in6*
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %18, i32 0, i32 0
store i8* %16, i8** %19
br label %20
20: ; preds = %2, %14, %8
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define void @sockaddr_set_port(%struct.sockaddr* nocapture %0, i32 %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = and i32 %4, -2
%6 = icmp eq i32 %5, 128
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i8* @htons(i32 %1)
%9 = bitcast %struct.sockaddr* %0 to i8**
store i8* %8, i8** %9
br label %10
10: ; preds = %2, %7
ret void
}