#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 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
typedef int in_port_t;
typedef int /*<<< orphan*/ addrString;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int htons(int /*<<< orphan*/);
int /*<<< orphan*/ *inet_ntop(int, void *, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ sysWarn(char *);
void printSocketAddress(struct sockaddr *addrStruct) {
  if (addrStruct == NULL) {
    sysWarn("failed to get address info");
    return;
  }
  void *addrPointer;
  char addrString[INET6_ADDRSTRLEN];
  in_port_t port;
  switch (addrStruct->sa_family) {
  case AF_INET:
    addrPointer = &((struct sockaddr_in *)addrStruct)->sin_addr;
    port = htons(((struct sockaddr_in *)addrStruct)->sin_port);
    break;
  case AF_INET6:
    addrPointer = &((struct sockaddr_in6 *)addrStruct)->sin6_addr;
    port = htons(((struct sockaddr_in6 *)addrStruct)->sin6_port);
    break;
  default:
    sysWarn("failed to detect address protocol");
    return;
  }
  if (inet_ntop(addrStruct->sa_family, addrPointer, addrString,
                sizeof(addrString)) == NULL) {
    sysWarn("failed to retrieve address");
    return;
  }
  fprintf(stdout, "Address: [%s]:%d\n", addrString, port);
}


x86 assembly (clang, -O0):
<printSocketAddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1168 <printSocketAddress+0x28>
lea    0xea2(%rip),%rdi
call   1050 <sysWarn@plt>
jmp    127f <printSocketAddress+0x13f>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     11e3 <printSocketAddress+0xa3>
jmp    11b1 <printSocketAddress+0x71>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
jne    1202 <printSocketAddress+0xc2>
jmp    11c4 <printSocketAddress+0x84>
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
jmp    121a <printSocketAddress+0xda>
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
jmp    121a <printSocketAddress+0xda>
lea    0xe12(%rip),%rdi
call   1050 <sysWarn@plt>
movl   $0x1,-0x28(%rbp)
jmp    1278 <printSocketAddress+0x138>
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    (%rcx),%edi
mov    -0x10(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    1255 <printSocketAddress+0x115>
lea    0xdf9(%rip),%rdi
call   1050 <sysWarn@plt>
movl   $0x1,-0x28(%rbp)
jmp    1278 <printSocketAddress+0x138>
mov    -0x38(%rbp),%rdx
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%ecx
lea    0xdec(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printSocketAddress>:
test   %rdi,%rdi
je     11cd <printSocketAddress+0x8d>
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%r13
mov    0x2e84(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r14
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    (%rdi),%edi
cmp    $0x81,%edi
je     118f <printSocketAddress+0x4f>
cmp    $0x80,%edi
jne    11d9 <printSocketAddress+0x99>
mov    $0x80,%edi
call   1040 <htons@plt>
mov    %eax,%r12d
mov    (%rbx),%edi
add    $0x4,%rbx
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15d,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     11e2 <printSocketAddress+0xa2>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
mov    %r14,%rdx
mov    %r12d,%ecx
call   1060 <fprintf@plt>
jmp    11ee <printSocketAddress+0xae>
lea    0xe2c(%rip),%rdi
jmp    1050 <sysWarn@plt>
lea    0xe3b(%rip),%rdi
jmp    11e9 <printSocketAddress+0xa9>
lea    0xe54(%rip),%rdi
call   1050 <sysWarn@plt>
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@.str = [27 x i8] c"failed to get address info\00"
@INET6_ADDRSTRLEN = global i32 0
@.str.1 = [34 x i8] c"failed to detect address protocol\00"
@.str.2 = [27 x i8] c"failed to retrieve address\00"
@stdout = global i32 0
@.str.3 = [18 x i8] c"Address: [%s]:%d\0A\00"
define void @printSocketAddress(%struct.sockaddr* %0) {
%2 = alloca %struct.sockaddr*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %2
%8 = load %struct.sockaddr*, %struct.sockaddr** %2
%9 = icmp eq %struct.sockaddr* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @sysWarn(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %59
12: ; preds = %1
%13 = load i32, i32* @INET6_ADDRSTRLEN
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %4
%16 = alloca i8, i64 %14
store i64 %14, i64* %5
%17 = load %struct.sockaddr*, %struct.sockaddr** %2
%18 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %17, i32 0, i32 0
%19 = load i32, i32* %18
switch i32 %19, label %40 [
i32 129, label %20
i32 128, label %30
]
20: ; preds = %12
%21 = load %struct.sockaddr*, %struct.sockaddr** %2
%22 = bitcast %struct.sockaddr* %21 to %struct.sockaddr_in*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 1
%24 = bitcast i32* %23 to i8*
store i8* %24, i8** %3
%25 = load %struct.sockaddr*, %struct.sockaddr** %2
%26 = bitcast %struct.sockaddr* %25 to %struct.sockaddr_in*
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @htons(i32 %28)
store i32 %29, i32* %6
br label %42
30: ; preds = %12
%31 = load %struct.sockaddr*, %struct.sockaddr** %2
%32 = bitcast %struct.sockaddr* %31 to %struct.sockaddr_in6*
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %32, i32 0, i32 1
%34 = bitcast i32* %33 to i8*
store i8* %34, i8** %3
%35 = load %struct.sockaddr*, %struct.sockaddr** %2
%36 = bitcast %struct.sockaddr* %35 to %struct.sockaddr_in6*
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @htons(i32 %38)
store i32 %39, i32* %6
br label %42
40: ; preds = %12
%41 = call i32 @sysWarn(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %7
br label %56
42: ; preds = %30, %20
%43 = load %struct.sockaddr*, %struct.sockaddr** %2
%44 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = load i8*, i8** %3
%47 = trunc i64 %14 to i32
%48 = call i32* @inet_ntop(i32 %45, i8* %46, i8* %16, i32 %47)
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 @sysWarn(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %7
br label %56
52: ; preds = %42
%53 = load i32, i32* @stdout
%54 = load i32, i32* %6
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %16, i32 %54)
store i32 0, i32* %7
br label %56
56: ; preds = %52, %50, %40
%57 = load i8*, i8** %4
%58 = load i32, i32* %7
switch i32 %58, label %60 [
i32 0, label %59
i32 1, label %59
]
59: ; preds = %10, %56, %56
ret void
60: ; preds = %56
unreachable
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"failed to get address info\00"
@INET6_ADDRSTRLEN = global i32 0
@.str.1 = [34 x i8] c"failed to detect address protocol\00"
@.str.2 = [27 x i8] c"failed to retrieve address\00"
@stdout = global i32 0
@.str.3 = [18 x i8] c"Address: [%s]:%d\0A\00"
define void @printSocketAddress(%struct.sockaddr* %0) {
%2 = icmp eq %struct.sockaddr* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @sysWarn(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %29
5: ; preds = %1
%6 = load i32, i32* @INET6_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%11 = load i32, i32* %10
switch i32 %11, label %13 [
i32 129, label %15
i32 128, label %12
]
12: ; preds = %5
br label %15
13: ; preds = %5
%14 = tail call i32 @sysWarn(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %28
15: ; preds = %5, %12
%16 = phi i32 [ 128, %12 ], [ %11, %5 ]
%17 = tail call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
%19 = bitcast i32* %18 to i8*
%20 = load i32, i32* %10
%21 = call i32* @inet_ntop(i32 %20, i8* %19, i8* %9, i32 %6)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @sysWarn(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %28
25: ; preds = %15
%26 = load i32, i32* @stdout
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %9, i32 %17)
br label %28
28: ; preds = %25, %23, %13
br label %29
29: ; preds = %28, %3
ret void
}