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

/* Variables and functions */
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ logging(char *, char *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int Getsockname(int sockfd, struct sockaddr_in sockaddr, int sin_size) {
  int n, port;
  if ((n = getsockname(sockfd, (struct sockaddr *)&sockaddr, &sin_size)) < 0) {
    perror("getsockname");
    return -1;
  }
  port = ntohs(sockaddr.sin_port);
  char logmsg[128];
  snprintf(logmsg, sizeof(logmsg), "socket has port %d\n", port);
  logging("lsrp-router.log", logmsg);
  return port;
}


x86 assembly (clang, -O0):
<Getsockname>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %esi,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1070 <getsockname@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1198 <Getsockname+0x48>
lea    0xe79(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <Getsockname+0x87>
mov    -0x8(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x18(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    $0x80,%esi
lea    0xe53(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0xa0(%rbp),%rsi
lea    0xe54(%rip),%rdi
call   1040 <logging@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Getsockname>:
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %esi,0x8(%rsp)
mov    %edx,0xc(%rsp)
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1070 <getsockname@plt>
test   %eax,%eax
js     11b7 <Getsockname+0x67>
mov    0x8(%rsp),%edi
call   1050 <ntohs@plt>
mov    %eax,%ebx
lea    0xe85(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x80,%esi
mov    %eax,%ecx
call   1030 <snprintf@plt>
lea    0xe7e(%rip),%rdi
mov    %r14,%rsi
call   1040 <logging@plt>
mov    %ebx,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11aa <Getsockname+0x5a>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"getsockname\00"
@.str.1 = [20 x i8] c"socket has port %d\0A\00"
@.str.2 = [16 x i8] c"lsrp-router.log\00"
define i32 @Getsockname(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [128 x i8]
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %1, i32* %11
store i32 %0, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* %6
%13 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%14 = call i32 @getsockname(i32 %12, %struct.sockaddr* %13, i32* %7)
store i32 %14, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %28
18: ; preds = %3
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @ntohs(i32 %20)
store i32 %21, i32* %9
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%23 = load i32, i32* %9
%24 = call i32 @snprintf(i8* %22, i32 128, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%26 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %9
store i32 %27, i32* %4
br label %28
28: ; preds = %18, %16
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"getsockname\00"
@.str.1 = [20 x i8] c"socket has port %d\0A\00"
@.str.2 = [16 x i8] c"lsrp-router.log\00"
define i32 @Getsockname(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %1, i32* %7
store i32 %2, i32* %5
%8 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%9 = call i32 @getsockname(i32 %0, %struct.sockaddr* %8, i32* %5)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %19
13: ; preds = %3
%14 = load i32, i32* %7
%15 = call i32 @ntohs(i32 %14)
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%17 = call i32 @snprintf(i8* %16, i32 128, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%18 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %19
19: ; preds = %13, %11
%20 = phi i32 [ -1, %11 ], [ %15, %13 ]
ret i32 %20
}