#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 {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
void *htons(int);
int setipaddr(int, char const *, struct sockaddr *, size_t *);
int setipaddr_withport(int af, const char *name, int port,
                       struct sockaddr *addr_ret, size_t *addr_ret_size) {
  int res = setipaddr(af, name, addr_ret, addr_ret_size);
  if (!res) {
    return res;
  }
  if (addr_ret->sa_family == AF_INET) {
    struct sockaddr_in *addr_in = (struct sockaddr_in *)addr_ret;
    addr_in->sin_port = htons(port);
  }
  if (addr_ret->sa_family == AF_INET6) {
    struct sockaddr_in6 *addr_in = (struct sockaddr_in6 *)addr_ret;
    addr_in->sin6_port = htons(port);
  }
  return res;
}


x86 assembly (clang, -O0):
<setipaddr_withport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <setipaddr@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1166 <setipaddr_withport+0x46>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <setipaddr_withport+0xae>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
jne    1197 <setipaddr_withport+0x77>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    11c8 <setipaddr_withport+0xa8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setipaddr_withport>:
push   %rbp
push   %r14
push   %rbx
mov    %rcx,%rbx
mov    %edx,%r14d
mov    %rcx,%rdx
mov    %r8,%rcx
call   1040 <setipaddr@plt>
mov    %eax,%ebp
test   %eax,%eax
je     116c <setipaddr_withport+0x4c>
mov    (%rbx),%rax
mov    0x2ea3(%rip),%rcx
cmp    (%rcx),%rax
jne    1155 <setipaddr_withport+0x35>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
mov    0x2e84(%rip),%rcx
cmp    (%rcx),%rax
jne    116c <setipaddr_withport+0x4c>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i8* }
%struct.sockaddr_in6 = type { i8* }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @setipaddr_withport(i32 %0, i8* %1, i32 %2, %struct.sockaddr* %3, i64* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr*
%11 = alloca i64*
%12 = alloca i32
%13 = alloca %struct.sockaddr_in*
%14 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store %struct.sockaddr* %3, %struct.sockaddr** %10
store i64* %4, i64** %11
%15 = load i32, i32* %7
%16 = load i8*, i8** %8
%17 = load %struct.sockaddr*, %struct.sockaddr** %10
%18 = load i64*, i64** %11
%19 = call i32 @setipaddr(i32 %15, i8* %16, %struct.sockaddr* %17, i64* %18)
store i32 %19, i32* %12
%20 = load i32, i32* %12
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %5
%23 = load i32, i32* %12
store i32 %23, i32* %6
br label %52
24: ; preds = %5
%25 = load %struct.sockaddr*, %struct.sockaddr** %10
%26 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @AF_INET
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %37
30: ; preds = %24
%31 = load %struct.sockaddr*, %struct.sockaddr** %10
%32 = bitcast %struct.sockaddr* %31 to %struct.sockaddr_in*
store %struct.sockaddr_in* %32, %struct.sockaddr_in** %13
%33 = load i32, i32* %9
%34 = call i8* @htons(i32 %33)
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
store i8* %34, i8** %36
br label %37
37: ; preds = %30, %24
%38 = load %struct.sockaddr*, %struct.sockaddr** %10
%39 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @AF_INET6
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %50
43: ; preds = %37
%44 = load %struct.sockaddr*, %struct.sockaddr** %10
%45 = bitcast %struct.sockaddr* %44 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %45, %struct.sockaddr_in6** %14
%46 = load i32, i32* %9
%47 = call i8* @htons(i32 %46)
%48 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%49 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %48, i32 0, i32 0
store i8* %47, i8** %49
br label %50
50: ; preds = %43, %37
%51 = load i32, i32* %12
store i32 %51, i32* %6
br label %52
52: ; preds = %50, %22
%53 = load i32, i32* %6
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @setipaddr_withport(i32 %0, i8* %1, i32 %2, %struct.sockaddr* %3, i64* %4) {
%6 = tail call i32 @setipaddr(i32 %0, i8* %1, %struct.sockaddr* %3, i64* %4)
%7 = icmp eq i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %5
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %3, i64 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @AF_INET
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = tail call i8* @htons(i32 %2)
%15 = bitcast %struct.sockaddr* %3 to i8**
store i8* %14, i8** %15
%16 = ptrtoint i8* %14 to i64
br label %17
17: ; preds = %13, %8
%18 = phi i64 [ %16, %13 ], [ %10, %8 ]
%19 = load i64, i64* @AF_INET6
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = tail call i8* @htons(i32 %2)
%23 = bitcast %struct.sockaddr* %3 to i8**
store i8* %22, i8** %23
br label %24
24: ; preds = %17, %21, %5
ret i32 %6
}