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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int htons(int);
int /*<<< orphan*/ inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
char *sock_addr_to_str(char *s, int slen, struct sockaddr *addr) {
  char addr_str[100];
  int port;
  if (addr->sa_family == AF_INET) {
    inet_ntop(AF_INET, &((struct sockaddr_in *)addr)->sin_addr, addr_str,
              sizeof(addr_str));
    port = ((struct sockaddr_in *)addr)->sin_port;
  } else if (addr->sa_family == AF_INET6) {
    inet_ntop(AF_INET6, &((struct sockaddr_in6 *)addr)->sin6_addr, addr_str,
              sizeof(addr_str));
    port = ((struct sockaddr_in6 *)addr)->sin6_port;
  } else {
    snprintf(s, slen, "Invalid AddrFamily %d", addr->sa_family);
    return s;
  }
  snprintf(s, slen, "%s:%d", addr_str, htons(port));
  return s;
}


x86 assembly (clang, -O0):
<sock_addr_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <sock_addr_to_str+0x61>
mov    0x2e84(%rip),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
lea    -0x90(%rbp),%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
jmp    120a <sock_addr_to_str+0xda>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
jne    11dc <sock_addr_to_str+0xac>
mov    0x2e31(%rip),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
lea    -0x90(%rbp),%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
jmp    1205 <sock_addr_to_str+0xd5>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xe0f(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1264 <sock_addr_to_str+0x134>
jmp    120a <sock_addr_to_str+0xda>
mov    -0x10(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x94(%rbp),%edi
call   1040 <htons@plt>
mov    -0xb0(%rbp),%rdi
mov    -0xa4(%rbp),%esi
mov    -0xa0(%rbp),%rcx
mov    %eax,%r8d
lea    0xdc1(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_addr_to_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    (%rdx),%rcx
mov    0x2e9b(%rip),%rax
cmp    (%rax),%rcx
je     115e <sock_addr_to_str+0x2e>
mov    0x2e87(%rip),%rax
cmp    (%rax),%rcx
jne    1199 <sock_addr_to_str+0x69>
lea    0x4(%rbp),%rsi
mov    %rsp,%r15
mov    %rcx,%rdi
mov    %r15,%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    0x0(%rbp),%edi
call   1040 <htons@plt>
lea    0xe92(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    11ad <sock_addr_to_str+0x7d>
lea    0xe60(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [22 x i8] c"Invalid AddrFamily %d\00"
@.str.1 = [6 x i8] c"%s:%d\00"
define i8* @sock_addr_to_str(i8* %0, i32 %1, %struct.sockaddr* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca [100 x i8]
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr* %2, %struct.sockaddr** %7
%10 = load %struct.sockaddr*, %struct.sockaddr** %7
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @AF_INET
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %26
15: ; preds = %3
%16 = load i64, i64* @AF_INET
%17 = load %struct.sockaddr*, %struct.sockaddr** %7
%18 = bitcast %struct.sockaddr* %17 to %struct.sockaddr_in*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 1
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%21 = call i32 @inet_ntop(i64 %16, i32* %19, i8* %20, i32 100)
%22 = load %struct.sockaddr*, %struct.sockaddr** %7
%23 = bitcast %struct.sockaddr* %22 to %struct.sockaddr_in*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %9
br label %53
26: ; preds = %3
%27 = load %struct.sockaddr*, %struct.sockaddr** %7
%28 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @AF_INET6
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %43
32: ; preds = %26
%33 = load i64, i64* @AF_INET6
%34 = load %struct.sockaddr*, %struct.sockaddr** %7
%35 = bitcast %struct.sockaddr* %34 to %struct.sockaddr_in6*
%36 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %35, i32 0, i32 1
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%38 = call i32 @inet_ntop(i64 %33, i32* %36, i8* %37, i32 100)
%39 = load %struct.sockaddr*, %struct.sockaddr** %7
%40 = bitcast %struct.sockaddr* %39 to %struct.sockaddr_in6*
%41 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %40, i32 0, i32 0
%42 = load i32, i32* %41
store i32 %42, i32* %9
br label %52
43: ; preds = %26
%44 = load i8*, i8** %5
%45 = load i32, i32* %6
%46 = load %struct.sockaddr*, %struct.sockaddr** %7
%47 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to i8*
%50 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %44, i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %49)
%51 = load i8*, i8** %5
store i8* %51, i8** %4
br label %61
52: ; preds = %32
br label %53
53: ; preds = %52, %15
%54 = load i8*, i8** %5
%55 = load i32, i32* %6
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%57 = load i32, i32* %9
%58 = call i32 @htons(i32 %57)
%59 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %54, i32 %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %56, i32 %58)
%60 = load i8*, i8** %5
store i8* %60, i8** %4
br label %61
61: ; preds = %53, %43
%62 = load i8*, i8** %4
ret i8* %62
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [22 x i8] c"Invalid AddrFamily %d\00"
@.str.1 = [6 x i8] c"%s:%d\00"
define i8* @sock_addr_to_str(i8* returned %0, i32 %1, %struct.sockaddr* %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%7 = load i64, i64* %6
%8 = load i64, i64* @AF_INET
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = bitcast %struct.sockaddr* %2 to %struct.sockaddr_in*
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i64 0, i32 1
br label %22
13: ; preds = %3
%14 = load i64, i64* @AF_INET6
%15 = icmp eq i64 %7, %14
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = bitcast %struct.sockaddr* %2 to %struct.sockaddr_in6*
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %17, i64 0, i32 1
br label %22
19: ; preds = %13
%20 = inttoptr i64 %7 to i8*
%21 = tail call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %29
22: ; preds = %16, %10
%23 = phi i32* [ %18, %16 ], [ %12, %10 ]
%24 = call i32 @inet_ntop(i64 %7, i32* %23, i8* %5, i32 100)
%25 = bitcast %struct.sockaddr* %2 to i32*
%26 = load i32, i32* %25
%27 = call i32 @htons(i32 %26)
%28 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 %27)
br label %29
29: ; preds = %22, %19
ret i8* %0
}