#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 */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printSocketAddress(struct sockaddr *);
int /*<<< orphan*/ stdout;
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 address");
    return;
  }
  fprintf(stdout, "Local socket created\n");
  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   1060 <getsockname@plt>
cmp    $0x0,%rax
jge    119b <getSockName+0x4b>
lea    0xe6f(%rip),%rdi
call   1040 <sysWarn@plt>
jmp    11b9 <getSockName+0x69>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x8(%rbp),%rdi
call   1070 <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   1060 <getsockname@plt>
test   %rax,%rax
js     11a3 <getSockName+0x53>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rsp,%rdi
call   1070 <printSocketAddress@plt>
jmp    11af <getSockName+0x5f>
lea    0xe56(%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 = [29 x i8] c"failed to get socket address\00"
@stdout = global i32 0
@.str.1 = [22 x i8] c"Local socket created\0A\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 ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %18
13: ; preds = %1
%14 = load i32, i32* @stdout
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%16 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%17 = call i32 @printSocketAddress(%struct.sockaddr* %16)
br label %18
18: ; preds = %13, %11
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"failed to get socket address\00"
@stdout = global i32 0
@.str.1 = [22 x i8] c"Local socket created\0A\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 ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %16
12: ; preds = %1
%13 = load i32, i32* @stdout
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @printSocketAddress(%struct.sockaddr* %7)
br label %16
16: ; preds = %12, %10
ret void
}