#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 s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
scalar_t__ getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ memset(struct sockaddr_in *, char, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void ant_debug_sock(int sock) {
  struct sockaddr_in addr;
  int olderrno;
  socklen_t len = sizeof(addr);
  olderrno = errno;
  memset(&addr, '\0', sizeof(addr));
  if (getpeername(sock, (struct sockaddr *)&addr, &len) < 0) {
    errno = olderrno;
  }
  fprintf(stderr, "%u.%u.%u.%u\n", (addr.sin_addr.s_addr) & 0xff,
          ((addr.sin_addr.s_addr) >> 8) & 0xff,
          ((addr.sin_addr.s_addr) >> 16) & 0xff,
          ((addr.sin_addr.s_addr) >> 24) & 0xff);
  fprintf(stderr, "port = %hd\n", ntohs(addr.sin_port));
}


x86 assembly (clang, -O0):
<ant_debug_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x8,-0x18(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1030 <getpeername@plt>
cmp    $0x0,%rax
jge    1194 <ant_debug_sock+0x54>
mov    -0x14(%rbp),%ecx
mov    0x2e46(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
and    $0xff,%edx
mov    -0xc(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0xc(%rbp),%r8d
sar    $0x10,%r8d
and    $0xff,%r8d
mov    -0xc(%rbp),%r9d
sar    $0x18,%r9d
and    $0xff,%r9d
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <ntohs@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ant_debug_sock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x8,0xc(%rsp)
mov    0x2e7d(%rip),%r15
mov    (%r15),%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <getpeername@plt>
test   %rax,%rax
jns    1189 <ant_debug_sock+0x49>
mov    %ebp,(%r15)
mov    0x2e50(%rip),%rbx
mov    (%rbx),%edi
mov    0x14(%rsp),%eax
movzbl %al,%edx
movzbl %ah,%ecx
mov    %eax,%esi
shr    $0x10,%esi
movzbl %sil,%r8d
shr    $0x18,%eax
lea    0xe51(%rip),%rsi
mov    %eax,%r9d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%ebx
mov    0x10(%rsp),%edi
call   1060 <ntohs@plt>
lea    0xe42(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x18,%rsp
pop    %rbx
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 }
@errno = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"%u.%u.%u.%u\0A\00"
@.str.1 = [12 x i8] c"port = %hd\0A\00"
define void @ant_debug_sock(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 8, i32* %5
%6 = load i32, i32* @errno
store i32 %6, i32* %4
%7 = call i32 @memset(%struct.sockaddr_in* %3, i8 signext 0, i32 8)
%8 = load i32, i32* %2
%9 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%10 = call i64 @getpeername(i32 %8, %struct.sockaddr* %9, i32* %5)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i32, i32* %4
store i32 %13, i32* @errno
br label %14
14: ; preds = %12, %1
%15 = load i32, i32* @stderr
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %18, 255
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = ashr i32 %22, 8
%24 = and i32 %23, 255
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = ashr i32 %27, 16
%29 = and i32 %28, 255
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = ashr i32 %32, 24
%34 = and i32 %33, 255
%35 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %24, i32 %29, i32 %34)
%36 = load i32, i32* @stderr
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @ntohs(i32 %38)
%40 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %39)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"%u.%u.%u.%u\0A\00"
@.str.1 = [12 x i8] c"port = %hd\0A\00"
define void @ant_debug_sock(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 8, i32* %3
%6 = load i32, i32* @errno
%7 = call i32 @memset(%struct.sockaddr_in* %2, i8 signext 0, i32 8)
%8 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%9 = call i64 @getpeername(i32 %0, %struct.sockaddr* %8, i32* %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 %6, i32* @errno
br label %12
12: ; preds = %11, %1
%13 = load i32, i32* @stderr
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, 255
%17 = lshr i32 %15, 8
%18 = and i32 %17, 255
%19 = lshr i32 %15, 16
%20 = and i32 %19, 255
%21 = lshr i32 %15, 24
%22 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %18, i32 %20, i32 %21)
%23 = load i32, i32* @stderr
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @ntohs(i32 %25)
%27 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %26)
ret void
}