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

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *inet_ntop(int const, int /*<<< orphan*/ *, char *, int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int getAddressFromSockAddr(struct sockaddr *addr, char **address, int *port) {
  struct sockaddr_in *sa4;
  struct sockaddr_in6 *sa6;
  char *addrBuf = malloc(256);
  memset(addrBuf, 0, 256);
  switch (addr->sa_family) {
  case AF_INET:
    sa4 = (struct sockaddr_in *)addr;
    if (inet_ntop(AF_INET, &sa4->sin_addr, addrBuf, 256) == NULL) {
      free(addrBuf);
      return -1;
    } else {
      *address = addrBuf;
      *port = sa4->sin_port;
    }
    break;
  case AF_INET6:
    sa6 = (struct sockaddr_in6 *)addr;
    if (inet_ntop(AF_INET6, &sa6->sin6_addr, addrBuf, 256) == NULL) {
      free(addrBuf);
      return -1;
    } else {
      *address = addrBuf;
      *port = sa6->sin6_port;
    }
    break;
  default:
    free(addrBuf);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<getAddressFromSockAddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x100,%edi
mov    %edi,-0x40(%rbp)
call   1060 <malloc@plt>
mov    -0x40(%rbp),%edx
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x80,%eax
je     11fd <getAddressFromSockAddr+0xbd>
jmp    118c <getAddressFromSockAddr+0x4c>
mov    -0x3c(%rbp),%eax
sub    $0x81,%eax
jne    125b <getAddressFromSockAddr+0x11b>
jmp    119f <getAddressFromSockAddr+0x5f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x38(%rbp),%rdx
mov    $0x81,%edi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    11e1 <getAddressFromSockAddr+0xa1>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <getAddressFromSockAddr+0x137>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1270 <getAddressFromSockAddr+0x130>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x38(%rbp),%rdx
mov    $0x80,%edi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    123f <getAddressFromSockAddr+0xff>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <getAddressFromSockAddr+0x137>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1270 <getAddressFromSockAddr+0x130>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <getAddressFromSockAddr+0x137>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getAddressFromSockAddr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x100,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
cmp    $0x80,%eax
je     118b <getAddressFromSockAddr+0x4b>
cmp    $0x81,%eax
jne    11b5 <getAddressFromSockAddr+0x75>
lea    0x4(%r12),%rsi
mov    $0x81,%edi
jmp    1195 <getAddressFromSockAddr+0x55>
lea    0x4(%r12),%rsi
mov    $0x80,%edi
mov    %rbx,%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     11b5 <getAddressFromSockAddr+0x75>
mov    %rbx,(%r15)
mov    (%r12),%eax
mov    %eax,(%r14)
xor    %eax,%eax
jmp    11c2 <getAddressFromSockAddr+0x82>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
define i32 @getAddressFromSockAddr(%struct.sockaddr* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca %struct.sockaddr_in*
%9 = alloca %struct.sockaddr_in6*
%10 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%11 = call i8* @malloc(i32 256)
store i8* %11, i8** %10
%12 = load i8*, i8** %10
%13 = call i32 @memset(i8* %12, i32 0, i32 256)
%14 = load %struct.sockaddr*, %struct.sockaddr** %5
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %14, i32 0, i32 0
%16 = load i32, i32* %15
switch i32 %16, label %55 [
i32 129, label %17
i32 128, label %36
]
17: ; preds = %3
%18 = load %struct.sockaddr*, %struct.sockaddr** %5
%19 = bitcast %struct.sockaddr* %18 to %struct.sockaddr_in*
store %struct.sockaddr_in* %19, %struct.sockaddr_in** %8
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 1
%22 = load i8*, i8** %10
%23 = call i32* @inet_ntop(i32 129, i32* %21, i8* %22, i32 256)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i8*, i8** %10
%27 = call i32 @free(i8* %26)
store i32 -1, i32* %4
br label %59
28: ; preds = %17
%29 = load i8*, i8** %10
%30 = load i8**, i8*** %6
store i8* %29, i8** %30
%31 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i32*, i32** %7
store i32 %33, i32* %34
br label %35
35: ; preds = %28
br label %58
36: ; preds = %3
%37 = load %struct.sockaddr*, %struct.sockaddr** %5
%38 = bitcast %struct.sockaddr* %37 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %38, %struct.sockaddr_in6** %9
%39 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %9
%40 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %39, i32 0, i32 1
%41 = load i8*, i8** %10
%42 = call i32* @inet_ntop(i32 128, i32* %40, i8* %41, i32 256)
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = load i8*, i8** %10
%46 = call i32 @free(i8* %45)
store i32 -1, i32* %4
br label %59
47: ; preds = %36
%48 = load i8*, i8** %10
%49 = load i8**, i8*** %6
store i8* %48, i8** %49
%50 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %9
%51 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load i32*, i32** %7
store i32 %52, i32* %53
br label %54
54: ; preds = %47
br label %58
55: ; preds = %3
%56 = load i8*, i8** %10
%57 = call i32 @free(i8* %56)
store i32 -1, i32* %4
br label %59
58: ; preds = %54, %35
store i32 0, i32* %4
br label %59
59: ; preds = %58, %55, %44, %25
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define i32 @getAddressFromSockAddr(%struct.sockaddr* %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%5 = tail call i32 @memset(i8* %4, i32 0, i32 256)
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%7 = load i32, i32* %6
switch i32 %7, label %20 [
i32 129, label %8
i32 128, label %14
]
8: ; preds = %3
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
%10 = tail call i32* @inet_ntop(i32 129, i32* %9, i8* %4, i32 256)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = tail call i32 @free(i8* %4)
br label %24
14: ; preds = %3
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
%16 = tail call i32* @inet_ntop(i32 128, i32* %15, i8* %4, i32 256)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = tail call i32 @free(i8* %4)
br label %24
20: ; preds = %3
%21 = tail call i32 @free(i8* %4)
br label %24
22: ; preds = %14, %8
store i8* %4, i8** %1
%23 = load i32, i32* %6
store i32 %23, i32* %2
br label %24
24: ; preds = %22, %20, %18, %12
%25 = phi i32 [ -1, %20 ], [ -1, %18 ], [ 0, %22 ], [ -1, %12 ]
ret i32 %25
}