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

/* Variables and functions */
int INET6_ADDRSTRLEN;
#define PF_INET 129
#define PF_INET6 128
int /*<<< orphan*/ *af_sa_to_str(struct sockaddr const *, char *);
int snprintf(char *, int, char *, int, char *);
int logging_sa_to_str(const struct sockaddr *sa, char *dst, int max_l) {
  int ipv;
  char buf[INET6_ADDRSTRLEN];
  if (af_sa_to_str(sa, buf) == NULL) {
    return (0);
  }
  switch (sa->sa_family) {
  case PF_INET:
    ipv = 4;
    break;
  case PF_INET6:
    ipv = 6;
    break;
  default:
    return (0);
  }
  return (snprintf(dst, max_l, "ipv%d, addr: %s", ipv, buf));
}


x86 assembly (clang, -O0):
<logging_sa_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x40(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <af_sa_to_str@plt>
cmp    $0x0,%rax
jne    1185 <logging_sa_to_str+0x65>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1200 <logging_sa_to_str+0xe0>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
sub    $0x80,%eax
je     11bd <logging_sa_to_str+0x9d>
jmp    119e <logging_sa_to_str+0x7e>
mov    -0x44(%rbp),%eax
sub    $0x81,%eax
jne    11c9 <logging_sa_to_str+0xa9>
jmp    11b1 <logging_sa_to_str+0x91>
movl   $0x4,-0x20(%rbp)
jmp    11dc <logging_sa_to_str+0xbc>
movl   $0x6,-0x20(%rbp)
jmp    11dc <logging_sa_to_str+0xbc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1200 <logging_sa_to_str+0xe0>
mov    -0x40(%rbp),%r8
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%ecx
lea    0xe0f(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logging_sa_to_str>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1040 <af_sa_to_str@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1195 <logging_sa_to_str+0x75>
mov    (%r12),%ecx
cmp    $0x81,%ecx
je     117b <logging_sa_to_str+0x5b>
cmp    $0x80,%ecx
jne    1195 <logging_sa_to_str+0x75>
mov    $0x6,%ecx
jmp    1180 <logging_sa_to_str+0x60>
mov    $0x4,%ecx
lea    0xe79(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%r8
call   1030 <snprintf@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [16 x i8] c"ipv%d, addr: %s\00"
define i32 @logging_sa_to_str(%struct.sockaddr* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i32, i32* @INET6_ADDRSTRLEN
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %9
%15 = alloca i8, i64 %13
store i64 %13, i64* %10
%16 = load %struct.sockaddr*, %struct.sockaddr** %5
%17 = call i32* @af_sa_to_str(%struct.sockaddr* %16, i8* %15)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 0, i32* %4
store i32 1, i32* %11
br label %32
20: ; preds = %3
%21 = load %struct.sockaddr*, %struct.sockaddr** %5
%22 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %21, i32 0, i32 0
%23 = load i32, i32* %22
switch i32 %23, label %26 [
i32 129, label %24
i32 128, label %25
]
24: ; preds = %20
store i32 4, i32* %8
br label %27
25: ; preds = %20
store i32 6, i32* %8
br label %27
26: ; preds = %20
store i32 0, i32* %4
store i32 1, i32* %11
br label %32
27: ; preds = %25, %24
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = call i32 @snprintf(i8* %28, i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %30, i8* %15)
store i32 %31, i32* %4
store i32 1, i32* %11
br label %32
32: ; preds = %27, %26, %19
%33 = load i8*, i8** %9
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [16 x i8] c"ipv%d, addr: %s\00"
define i32 @logging_sa_to_str(%struct.sockaddr* %0, i8* %1, i32 %2) {
%4 = load i32, i32* @INET6_ADDRSTRLEN
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32* @af_sa_to_str(%struct.sockaddr* %0, i8* %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %16, label %9
9: ; preds = %3
%10 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%11 = load i32, i32* %10
switch i32 %11, label %16 [
i32 129, label %13
i32 128, label %12
]
12: ; preds = %9
br label %13
13: ; preds = %9, %12
%14 = phi i32 [ 6, %12 ], [ 4, %9 ]
%15 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14, i8* %6)
br label %16
16: ; preds = %9, %3, %13
%17 = phi i32 [ %15, %13 ], [ 0, %3 ], [ 0, %9 ]
ret i32 %17
}