#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*/ addr;

/* Variables and functions */
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printSocketAddress(struct sockaddr *);
int /*<<< orphan*/ sysWarn(char *);
void getSockName(int sock) {
  struct sockaddr_storage addr;
  socklen_t addrLen = sizeof(addr);
  memset(&addr, 0, addrLen);
  if (getsockname(sock, (struct sockaddr *)&addr, &addrLen) < 0) {
    sysWarn("failed to get socket name");
    return;
  }
  printSocketAddress((struct sockaddr *)&addr);
}


x86 assembly (clang, -O0):
<getSockName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1050 <getsockname@plt>
cmp    $0x0,%rax
jge    118b <getSockName+0x4b>
lea    0xe7f(%rip),%rdi
call   1040 <sysWarn@plt>
jmp    1194 <getSockName+0x54>
lea    -0x8(%rbp),%rdi
call   1060 <printSocketAddress@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getSockName>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <getsockname@plt>
test   %rax,%rax
js     117e <getSockName+0x3e>
mov    %rsp,%rdi
call   1060 <printSocketAddress@plt>
jmp    118a <getSockName+0x4a>
lea    0xe7b(%rip),%rdi
call   1040 <sysWarn@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"failed to get socket name\00"
define void @getSockName(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
store i32 %0, i32* %2
store i32 4, i32* %4
%5 = load i32, i32* %4
%6 = call i32 @memset(%struct.sockaddr_storage* %3, i32 0, i32 %5)
%7 = load i32, i32* %2
%8 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%9 = call i64 @getsockname(i32 %7, %struct.sockaddr* %8, i32* %4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @sysWarn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %16
13: ; preds = %1
%14 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%15 = call i32 @printSocketAddress(%struct.sockaddr* %14)
br label %16
16: ; preds = %13, %11
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"failed to get socket name\00"
define void @getSockName(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_storage* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = call i32 @memset(%struct.sockaddr_storage* %2, i32 0, i32 4)
%7 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%8 = call i64 @getsockname(i32 %0, %struct.sockaddr* %7, i32* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @sysWarn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %14
12: ; preds = %1
%13 = call i32 @printSocketAddress(%struct.sockaddr* %7)
br label %14
14: ; preds = %12, %10
ret void
}