#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 addrinfo {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
void *htons(int);
void fill_addrinfo_port(const struct addrinfo *addr, int port) {
  if (addr->ai_family == AF_INET) {
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)(addr->ai_addr);
    ipv4->sin_port = htons(port);
  } else if (addr->ai_family == AF_INET6) {
    struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)(addr->ai_addr);
    ipv6->sin6_port = htons(port);
  }
}


x86 assembly (clang, -O0):
<fill_addrinfo_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    1159 <fill_addrinfo_port+0x49>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1193 <fill_addrinfo_port+0x83>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
jne    118e <fill_addrinfo_port+0x7e>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    1193 <fill_addrinfo_port+0x83>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_addrinfo_port>:
push   %rbx
mov    (%rdi),%rax
mov    0x2ecd(%rip),%rcx
cmp    (%rcx),%rax
je     112c <fill_addrinfo_port+0x1c>
mov    0x2eb9(%rip),%rcx
cmp    (%rcx),%rax
jne    113a <fill_addrinfo_port+0x2a>
mov    0x8(%rdi),%rbx
mov    %esi,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64 }
%struct.sockaddr_in = type { i8* }
%struct.sockaddr_in6 = type { i8* }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define void @fill_addrinfo_port(%struct.addrinfo* nocapture %0, i32 %1) {
%3 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = load i64, i64* @AF_INET
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%9 = load i64, i64* %8
%10 = inttoptr i64 %9 to %struct.sockaddr_in*
%11 = tail call i8* @htons(i32 %1)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 0
store i8* %11, i8** %12
br label %22
13: ; preds = %2
%14 = load i64, i64* @AF_INET6
%15 = icmp eq i64 %4, %14
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%18 = load i64, i64* %17
%19 = inttoptr i64 %18 to %struct.sockaddr_in6*
%20 = tail call i8* @htons(i32 %1)
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %19, i64 0, i32 0
store i8* %20, i8** %21
br label %22
22: ; preds = %13, %16, %7
ret void
}