#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;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
void *htons(int);
int sock_setport(struct sockaddr *addr, int port) {
  switch (addr->sa_family) {
  case AF_INET:
    ((struct sockaddr_in *)addr)->sin_port = htons(port);
    break;
  case AF_INET6:
    ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
    break;
  default:
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<sock_setport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
sub    $0x80,%eax
je     1162 <sock_setport+0x52>
jmp    1138 <sock_setport+0x28>
mov    -0x18(%rbp),%eax
sub    $0x81,%eax
jne    1179 <sock_setport+0x69>
jmp    114b <sock_setport+0x3b>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1185 <sock_setport+0x75>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1185 <sock_setport+0x75>
movl   $0x0,-0x4(%rbp)
jmp    118c <sock_setport+0x7c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_setport>:
push   %rbx
mov    (%rdi),%ecx
and    $0xfffffffe,%ecx
xor    %eax,%eax
cmp    $0x80,%ecx
jne    1132 <sock_setport+0x22>
mov    %rdi,%rbx
mov    %esi,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
mov    $0x1,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define i32 @sock_setport(%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
%11 = phi i32 [ 0, %2 ], [ 1, %7 ]
ret i32 %11
}