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

/* Variables and functions */
scalar_t__ getpeername(int, struct sockaddr *, int *);
char *inet_ntoa(int /*<<< orphan*/);
char *get_sock_addr(int sock) {
  struct sockaddr_in m_addr;
  socklen_t sock_len = sizeof m_addr;
  if (sock == -1) {
    return NULL;
  }
  if (getpeername(sock, (struct sockaddr *)&m_addr, &sock_len) == 0) {
    return inet_ntoa(m_addr.sin_addr);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_sock_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x4,-0x14(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1149 <get_sock_addr+0x29>
movq   $0x0,-0x8(%rbp)
jmp    117c <get_sock_addr+0x5c>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <getpeername@plt>
cmp    $0x0,%rax
jne    1174 <get_sock_addr+0x54>
mov    -0x10(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x8(%rbp)
jmp    117c <get_sock_addr+0x5c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sock_addr>:
push   %rax
movl   $0x4,0x4(%rsp)
cmp    $0xffffffff,%edi
je     1140 <get_sock_addr+0x20>
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1040 <getpeername@plt>
test   %rax,%rax
je     1144 <get_sock_addr+0x24>
xor    %eax,%eax
pop    %rcx
ret
mov    (%rsp),%edi
call   1030 <inet_ntoa@plt>
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i8* @get_sock_addr(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %5
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %19
9: ; preds = %1
%10 = load i32, i32* %3
%11 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%12 = call i64 @getpeername(i32 %10, %struct.sockaddr* %11, i32* %5)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i8* @inet_ntoa(i32 %16)
store i8* %17, i8** %2
br label %19
18: ; preds = %9
store i8* null, i8** %2
br label %19
19: ; preds = %18, %14, %8
%20 = load i8*, i8** %2
ret i8* %20
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i8* @get_sock_addr(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = icmp eq i32 %0, -1
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%9 = call i64 @getpeername(i32 %0, %struct.sockaddr* %8, i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i8* @inet_ntoa(i32 %13)
br label %15
15: ; preds = %7, %1, %11
%16 = phi i8* [ %14, %11 ], [ null, %1 ], [ null, %7 ]
ret i8* %16
}