#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
char *CopyStr(char *, char *);
char *IPtoStr(int /*<<< orphan*/);
int getpeername(int, struct sockaddr *, int *);
int getsockname(int, struct sockaddr *, int *);
int ntohs(int);
int GetSockDetails(int sock, char **LocalAddress, int *LocalPort,
                   char **RemoteAddress, int *RemotePort) {
  int salen, result;
  struct sockaddr_in sa;
  *LocalPort = 0;
  *RemotePort = 0;
  *LocalAddress = CopyStr(*LocalAddress, "");
  *RemoteAddress = CopyStr(*RemoteAddress, "");
  salen = sizeof(struct sockaddr_in);
  result = getsockname(sock, (struct sockaddr *)&sa, &salen);
  if (result == 0) {
    *LocalAddress = CopyStr(*LocalAddress, IPtoStr(sa.sin_addr.s_addr));
    *LocalPort = ntohs(sa.sin_port);
    result = getpeername(sock, (struct sockaddr *)&sa, &salen);
    if (result == 0) {
      *RemoteAddress = CopyStr(*RemoteAddress, IPtoStr(sa.sin_addr.s_addr));
      *RemotePort = sa.sin_port;
    }
    result = 0;
  }
  if (result == 0)
    return (1);
  return (0);
}


x86 assembly (clang, -O0):
<GetSockDetails>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe73(%rip),%rsi
call   1040 <CopyStr@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe56(%rip),%rsi
call   1040 <CopyStr@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x8,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <getsockname@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    126c <GetSockDetails+0x11c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <IPtoStr@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
call   1040 <CopyStr@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1030 <getpeername@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1265 <GetSockDetails+0x115>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <IPtoStr@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
call   1040 <CopyStr@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1282 <GetSockDetails+0x132>
movl   $0x1,-0x4(%rbp)
jmp    1289 <GetSockDetails+0x139>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetSockDetails>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r14
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %edi,%r15d
movl   $0x0,(%rdx)
movl   $0x0,(%r8)
mov    (%rsi),%rdi
lea    0xe7c(%rip),%rbx
mov    %rbx,%rsi
call   1040 <CopyStr@plt>
mov    %rax,0x0(%rbp)
mov    0x0(%r13),%rdi
mov    %rbx,%rsi
call   1040 <CopyStr@plt>
mov    %rax,0x0(%r13)
movl   $0x8,0x14(%rsp)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %r15d,%edi
call   1070 <getsockname@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    1227 <GetSockDetails+0xd7>
mov    0x0(%rbp),%rbx
mov    0xc(%rsp),%edi
call   1050 <IPtoStr@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <CopyStr@plt>
mov    %rax,0x0(%rbp)
mov    0x8(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,(%r12)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %r15d,%edi
call   1030 <getpeername@plt>
mov    $0x1,%ebx
test   %eax,%eax
jne    1227 <GetSockDetails+0xd7>
mov    0x0(%r13),%rbp
mov    0xc(%rsp),%edi
call   1050 <IPtoStr@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <CopyStr@plt>
mov    %rax,0x0(%r13)
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [1 x i8] zeroinitializer
define i32 @GetSockDetails(i32 %0, i8** %1, i32* %2, i8** %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i8**
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.sockaddr_in
store i32 %0, i32* %7
store i8** %1, i8*** %8
store i32* %2, i32** %9
store i8** %3, i8*** %10
store i32* %4, i32** %11
%15 = load i32*, i32** %9
store i32 0, i32* %15
%16 = load i32*, i32** %11
store i32 0, i32* %16
%17 = load i8**, i8*** %8
%18 = load i8*, i8** %17
%19 = call i8* @CopyStr(i8* %18, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%20 = load i8**, i8*** %8
store i8* %19, i8** %20
%21 = load i8**, i8*** %10
%22 = load i8*, i8** %21
%23 = call i8* @CopyStr(i8* %22, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%24 = load i8**, i8*** %10
store i8* %23, i8** %24
store i32 8, i32* %12
%25 = load i32, i32* %7
%26 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%27 = call i32 @getsockname(i32 %25, %struct.sockaddr* %26, i32* %12)
store i32 %27, i32* %13
%28 = load i32, i32* %13
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %61
30: ; preds = %5
%31 = load i8**, i8*** %8
%32 = load i8*, i8** %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i8* @IPtoStr(i32 %35)
%37 = call i8* @CopyStr(i8* %32, i8* %36)
%38 = load i8**, i8*** %8
store i8* %37, i8** %38
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @ntohs(i32 %40)
%42 = load i32*, i32** %9
store i32 %41, i32* %42
%43 = load i32, i32* %7
%44 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%45 = call i32 @getpeername(i32 %43, %struct.sockaddr* %44, i32* %12)
store i32 %45, i32* %13
%46 = load i32, i32* %13
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %60
48: ; preds = %30
%49 = load i8**, i8*** %10
%50 = load i8*, i8** %49
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i8* @IPtoStr(i32 %53)
%55 = call i8* @CopyStr(i8* %50, i8* %54)
%56 = load i8**, i8*** %10
store i8* %55, i8** %56
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%58 = load i32, i32* %57
%59 = load i32*, i32** %11
store i32 %58, i32* %59
br label %60
60: ; preds = %48, %30
store i32 0, i32* %13
br label %61
61: ; preds = %60, %5
%62 = load i32, i32* %13
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %61
store i32 1, i32* %6
br label %66
65: ; preds = %61
store i32 0, i32* %6
br label %66
66: ; preds = %65, %64
%67 = load i32, i32* %6
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [1 x i8] zeroinitializer
define i32 @GetSockDetails(i32 %0, i8** nocapture %1, i32* nocapture writeonly %2, i8** nocapture %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = bitcast i32* %6 to i8*
%9 = bitcast %struct.sockaddr_in* %7 to i8*
store i32 0, i32* %2
store i32 0, i32* %4
%10 = load i8*, i8** %1
%11 = tail call i8* @CopyStr(i8* %10, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %1
%12 = load i8*, i8** %3
%13 = tail call i8* @CopyStr(i8* %12, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %3
store i32 8, i32* %6
%14 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%15 = call i32 @getsockname(i32 %0, %struct.sockaddr* %14, i32* %6)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %5
%18 = load i8*, i8** %1
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1, i32 0
%20 = load i32, i32* %19
%21 = call i8* @IPtoStr(i32 %20)
%22 = call i8* @CopyStr(i8* %18, i8* %21)
store i8* %22, i8** %1
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @ntohs(i32 %24)
store i32 %25, i32* %2
%26 = call i32 @getpeername(i32 %0, %struct.sockaddr* %14, i32* %6)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %17
%29 = load i8*, i8** %3
%30 = load i32, i32* %19
%31 = call i8* @IPtoStr(i32 %30)
%32 = call i8* @CopyStr(i8* %29, i8* %31)
store i8* %32, i8** %3
%33 = load i32, i32* %23
store i32 %33, i32* %4
br label %34
34: ; preds = %17, %28, %5
%35 = phi i32 [ 0, %5 ], [ 1, %28 ], [ 1, %17 ]
ret i32 %35
}