#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 */
typedef int /*<<< orphan*/ tmphostname;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ inA;

/* Variables and functions */
int gethostname(char *, int);
scalar_t__ getsockname(int, struct sockaddr *, int /*<<< orphan*/ *);
int ntohs(int /*<<< orphan*/);
char *strdup(char *);
int cf_socket_name_get(int sd, char **host, int *port) {
  struct sockaddr_in inA;
  int addr_len = sizeof(inA);
  char tmphostname[1024];
  int result;
  if (getsockname(sd, (struct sockaddr *)&inA, (socklen_t *)&addr_len) != 0) {
    host = NULL;
    return -1;
  }
  result = gethostname(tmphostname, sizeof(tmphostname));
  if (result == -1) {
    host = NULL;
    return -1;
  }
  *port = ntohs(inA.sin_port);
  *host = strdup(tmphostname);
  return 0;
}


x86 assembly (clang, -O0):
<cf_socket_name_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <getsockname@plt>
cmp    $0x0,%rax
je     118b <cf_socket_name_get+0x4b>
movq   $0x0,-0x10(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <cf_socket_name_get+0xb0>
lea    -0x430(%rbp),%rdi
mov    $0x400,%esi
call   1050 <gethostname@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0xffffffff,-0x434(%rbp)
jne    11c3 <cf_socket_name_get+0x83>
movq   $0x0,-0x10(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <cf_socket_name_get+0xb0>
mov    -0x20(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x430(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cf_socket_name_get>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1040 <getsockname@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11a0 <cf_socket_name_get+0x60>
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   1050 <gethostname@plt>
cmp    $0xffffffff,%eax
je     11a0 <cf_socket_name_get+0x60>
mov    0x8(%rsp),%edi
call   1030 <ntohs@plt>
mov    %eax,(%rbx)
lea    0x10(%rsp),%rdi
call   1060 <strdup@plt>
mov    %rax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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