#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ sockaddr_tostring(struct sockaddr_storage *, char *, int,
                                     int *);
int get_sock_name(int fd, char *ip, int ip_len, int *port) {
  socklen_t salen;
  struct sockaddr_storage sa;
  salen = sizeof(sa);
  if (getsockname(fd, (struct sockaddr *)&sa, &salen) == -1) {
    if (port != NULL)
      *port = 0;
    if (ip != NULL) {
      ip[0] = '?';
      ip[1] = '\0';
    }
    return -1;
  }
  sockaddr_tostring(&sa, ip, ip_len, port);
  return 0;
}


x86 assembly (clang, -O0):
<get_sock_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    1191 <get_sock_name+0x71>
cmpq   $0x0,-0x20(%rbp)
je     116b <get_sock_name+0x4b>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1185 <get_sock_name+0x65>
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <get_sock_name+0x8c>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdi
call   1030 <sockaddr_tostring@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sock_name>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %rsi,%r14
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1040 <getsockname@plt>
cmp    $0xffffffff,%eax
je     1162 <get_sock_name+0x42>
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <sockaddr_tostring@plt>
xor    %eax,%eax
jmp    117d <get_sock_name+0x5d>
test   %rbx,%rbx
je     116d <get_sock_name+0x4d>
movl   $0x0,(%rbx)
mov    $0xffffffff,%eax
test   %r14,%r14
je     117d <get_sock_name+0x5d>
movw   $0x3f,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @get_sock_name(i32 %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca %struct.sockaddr_storage
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 4, i32* %10
%12 = load i32, i32* %6
%13 = bitcast %struct.sockaddr_storage* %11 to %struct.sockaddr*
%14 = call i32 @getsockname(i32 %12, %struct.sockaddr* %13, i32* %10)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %30
16: ; preds = %4
%17 = load i32*, i32** %9
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = load i32*, i32** %9
store i32 0, i32* %20
br label %21
21: ; preds = %19, %16
%22 = load i8*, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i64 0
store i8 63, i8* %26
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8 0, i8* %28
br label %29
29: ; preds = %24, %21
store i32 -1, i32* %5
br label %35
30: ; preds = %4
%31 = load i8*, i8** %7
%32 = load i32, i32* %8
%33 = load i32*, i32** %9
%34 = call i32 @sockaddr_tostring(%struct.sockaddr_storage* %11, i8* %31, i32 %32, i32* %33)
store i32 0, i32* %5
br label %35
35: ; preds = %30, %29
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @get_sock_name(i32 %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_storage
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.sockaddr_storage* %6 to i8*
store i32 4, i32* %5
%9 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%10 = call i32 @getsockname(i32 %0, %struct.sockaddr* %9, i32* %5)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %4
%13 = icmp eq i32* %3, null
br i1 %13, label %15, label %14
14: ; preds = %12
store i32 0, i32* %3
br label %15
15: ; preds = %14, %12
%16 = icmp eq i8* %1, null
br i1 %16, label %21, label %17
17: ; preds = %15
store i8 63, i8* %1
%18 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %18
br label %21
19: ; preds = %4
%20 = call i32 @sockaddr_tostring(%struct.sockaddr_storage* %6, i8* %1, i32 %2, i32* %3)
br label %21
21: ; preds = %15, %17, %19
%22 = phi i32 [ 0, %19 ], [ -1, %17 ], [ -1, %15 ]
ret i32 %22
}