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

/* Variables and functions */
scalar_t__ AF_INET;
void *htons(int);
void set_port(struct sockaddr_storage *ss, int port) {
  if (ss->ss_family == AF_INET) {
    struct sockaddr_in *sin = (struct sockaddr_in *)ss;
    sin->sin_port = htons(port);
  } else {
    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ss;
    sin6->sin6_port = htons(port);
  }
}


x86 assembly (clang, -O0):
<set_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2ebb(%rip),%rcx
cmp    (%rcx),%rax
jne    1155 <set_port+0x45>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    116f <set_port+0x5f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_port>:
push   %rbx
mov    %rdi,%rbx
mov    %esi,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in = type { i8* }
%struct.sockaddr_in6 = type { i8* }
@AF_INET = global i64 0
define void @set_port(%struct.sockaddr_storage* %0, i32 %1) {
%3 = alloca %struct.sockaddr_storage*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.sockaddr_in6*
store %struct.sockaddr_storage* %0, %struct.sockaddr_storage** %3
store i32 %1, i32* %4
%7 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%8 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %7, i32 0, i32 0
%9 = load i64, i64* %8
%10 = load i64, i64* @AF_INET
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%14 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr_in*
store %struct.sockaddr_in* %14, %struct.sockaddr_in** %5
%15 = load i32, i32* %4
%16 = call i8* @htons(i32 %15)
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
store i8* %16, i8** %18
br label %26
19: ; preds = %2
%20 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%21 = bitcast %struct.sockaddr_storage* %20 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %21, %struct.sockaddr_in6** %6
%22 = load i32, i32* %4
%23 = call i8* @htons(i32 %22)
%24 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %6
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %24, i32 0, i32 0
store i8* %23, i8** %25
br label %26
26: ; preds = %19, %12
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
@AF_INET = global i64 0
define void @set_port(%struct.sockaddr_storage* nocapture %0, i32 %1) {
%3 = tail call i8* @htons(i32 %1)
%4 = bitcast %struct.sockaddr_storage* %0 to i8**
store i8* %3, i8** %4
ret void
}