#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*/ uint16_t;
struct sockaddr_storage {
  scalar_t__ ss_family;
};
struct sockaddr_in6 {
  void *sin6_port;
};
struct sockaddr_in {
  void *sin_port;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
void *htons(int /*<<< orphan*/);
void sockaddr_port_set(struct sockaddr_storage *ss, uint16_t port) {
  if (ss == NULL) {
    return;
  }
  if (ss->ss_family == AF_INET6) {
    ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
  } else if (ss->ss_family == AF_INET) {
    ((struct sockaddr_in *)ss)->sin_port = htons(port);
  }
}


x86 assembly (clang, -O0):
<sockaddr_port_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    112f <sockaddr_port_set+0x1f>
jmp    118b <sockaddr_port_set+0x7b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2ea3(%rip),%rcx
cmp    (%rcx),%rax
jne    115d <sockaddr_port_set+0x4d>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    118b <sockaddr_port_set+0x7b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e7d(%rip),%rcx
cmp    (%rcx),%rax
jne    1186 <sockaddr_port_set+0x76>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    118b <sockaddr_port_set+0x7b>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr_port_set>:
push   %rbx
test   %rdi,%rdi
je     1134 <sockaddr_port_set+0x24>
mov    %rdi,%rbx
mov    (%rdi),%rax
mov    0x2ebd(%rip),%rcx
cmp    (%rcx),%rax
je     1136 <sockaddr_port_set+0x26>
mov    0x2eb9(%rip),%rcx
cmp    (%rcx),%rax
je     1136 <sockaddr_port_set+0x26>
pop    %rbx
ret
mov    %esi,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in6 = type { i8* }
%struct.sockaddr_in = type { i8* }
@AF_INET6 = global i64 0
@AF_INET = global i64 0
define void @sockaddr_port_set(%struct.sockaddr_storage* %0, i32 %1) {
%3 = alloca %struct.sockaddr_storage*
%4 = alloca i32
store %struct.sockaddr_storage* %0, %struct.sockaddr_storage** %3
store i32 %1, i32* %4
%5 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%6 = icmp eq %struct.sockaddr_storage* %5, null
br i1 %6, label %7, label %8
7: ; preds = %2
br label %33
8: ; preds = %2
%9 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%10 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %9, i32 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* @AF_INET6
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = load i32, i32* %4
%16 = call i8* @htons(i32 %15)
%17 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%18 = bitcast %struct.sockaddr_storage* %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 %33
20: ; preds = %8
%21 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%22 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @AF_INET
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = load i32, i32* %4
%28 = call i8* @htons(i32 %27)
%29 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%30 = bitcast %struct.sockaddr_storage* %29 to %struct.sockaddr_in*
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %30, i32 0, i32 0
store i8* %28, i8** %31
br label %32
32: ; preds = %26, %20
br label %33
33: ; preds = %7, %32, %14
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
@AF_INET6 = global i64 0
@AF_INET = global i64 0
define void @sockaddr_port_set(%struct.sockaddr_storage* %0, i32 %1) {
%3 = icmp eq %struct.sockaddr_storage* %0, null
br i1 %3, label %15, label %4
4: ; preds = %2
%5 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 0, i32 0
%6 = load i64, i64* %5
%7 = load i64, i64* @AF_INET6
%8 = icmp eq i64 %6, %7
%9 = load i64, i64* @AF_INET
%10 = icmp eq i64 %6, %9
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = tail call i8* @htons(i32 %1)
%14 = bitcast %struct.sockaddr_storage* %0 to i8**
store i8* %13, i8** %14
br label %15
15: ; preds = %4, %12, %2
ret void
}