#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_in6 {
  int /*<<< orphan*/ sin6_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int sa_family;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
#define PF_INET 129
#define PF_INET6 128
int /*<<< orphan*/ dbgprintf(char *, ...);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int ntohs(int /*<<< orphan*/);
void debugListenInfo(int fd, char *type) {
  char *szFamily;
  int port;
  struct sockaddr sa;
  struct sockaddr_in *ipv4;
  struct sockaddr_in6 *ipv6;
  socklen_t saLen = sizeof(sa);
  if (getsockname(fd, &sa, &saLen) == 0) {
    switch (sa.sa_family) {
    case PF_INET:
      szFamily = "IPv4";
      ipv4 = (struct sockaddr_in *)&sa;
      port = ntohs(ipv4->sin_port);
      break;
    case PF_INET6:
      szFamily = "IPv6";
      ipv6 = (struct sockaddr_in6 *)&sa;
      port = ntohs(ipv6->sin6_port);
      break;
    default:
      szFamily = "other";
      port = -1;
      break;
    }
    dbgprintf("Listening on %s syslogd socket %d (%s/port %d).\n", type, fd,
              szFamily, port);
    return;
  }
  dbgprintf("Listening on syslogd socket %d - could not obtain peer info.\n",
            fd);
}


x86 assembly (clang, -O0):
<debugListenInfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x34(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1050 <getsockname@plt>
cmp    $0x0,%rax
jne    1209 <debugListenInfo+0xd9>
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0x80,%eax
je     11af <debugListenInfo+0x7f>
jmp    1176 <debugListenInfo+0x46>
mov    -0x38(%rbp),%eax
sub    $0x81,%eax
jne    11d5 <debugListenInfo+0xa5>
jmp    1189 <debugListenInfo+0x59>
lea    0xe70(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x1c(%rbp)
jmp    11e7 <debugListenInfo+0xb7>
lea    0xe4f(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x1c(%rbp)
jmp    11e7 <debugListenInfo+0xb7>
lea    0xe2e(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <dbgprintf@plt>
jmp    121a <debugListenInfo+0xea>
mov    -0x4(%rbp),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <dbgprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debugListenInfo>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1050 <getsockname@plt>
test   %rax,%rax
je     116c <debugListenInfo+0x3c>
lea    0xee0(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <dbgprintf@plt>
jmp    11c2 <debugListenInfo+0x92>
mov    0x8(%rsp),%edi
cmp    $0x81,%edi
je     118e <debugListenInfo+0x5e>
cmp    $0x80,%edi
jne    119f <debugListenInfo+0x6f>
lea    0xe7e(%rip),%r15
mov    $0x80,%edi
jmp    1195 <debugListenInfo+0x65>
lea    0xe6b(%rip),%r15
call   1040 <ntohs@plt>
mov    %eax,%r8d
jmp    11ac <debugListenInfo+0x7c>
lea    0xe64(%rip),%r15
mov    $0xffffffff,%r8d
lea    0xe5d(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1030 <dbgprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [5 x i8] c"IPv4\00"
@.str.1 = [5 x i8] c"IPv6\00"
@.str.2 = [6 x i8] c"other\00"
@.str.3 = [49 x i8] c"Listening on %s syslogd socket %d (%s/port %d).\0A\00"
@.str.4 = [62 x i8] c"Listening on syslogd socket %d - could not obtain peer info.\0A\00"
define void @debugListenInfo(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr
%8 = alloca %struct.sockaddr_in*
%9 = alloca %struct.sockaddr_in6*
%10 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 4, i32* %10
%11 = load i32, i32* %3
%12 = call i64 @getsockname(i32 %11, %struct.sockaddr* %7, i32* %10)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %2
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i32 0, i32 0
%16 = load i32, i32* %15
switch i32 %16, label %29 [
i32 129, label %17
i32 128, label %23
]
17: ; preds = %14
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %5
%18 = bitcast %struct.sockaddr* %7 to %struct.sockaddr_in*
store %struct.sockaddr_in* %18, %struct.sockaddr_in** %8
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @ntohs(i32 %21)
store i32 %22, i32* %6
br label %30
23: ; preds = %14
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %5
%24 = bitcast %struct.sockaddr* %7 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %24, %struct.sockaddr_in6** %9
%25 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %9
%26 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @ntohs(i32 %27)
store i32 %28, i32* %6
br label %30
29: ; preds = %14
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %5
store i32 -1, i32* %6
br label %30
30: ; preds = %29, %23, %17
%31 = load i8*, i8** %4
%32 = load i32, i32* %3
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %31, i32 %32, i8* %33, i32 %34)
br label %39
36: ; preds = %2
%37 = load i32, i32* %3
%38 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36, %30
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"IPv4\00"
@.str.1 = [5 x i8] c"IPv6\00"
@.str.2 = [6 x i8] c"other\00"
@.str.3 = [49 x i8] c"Listening on %s syslogd socket %d (%s/port %d).\0A\00"
@.str.4 = [62 x i8] c"Listening on syslogd socket %d - could not obtain peer info.\0A\00"
define void @debugListenInfo(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr
%4 = alloca i32
%5 = bitcast %struct.sockaddr* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%7 = call i64 @getsockname(i32 %0, %struct.sockaddr* %3, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %2
%10 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %3, i64 0, i32 0
%11 = load i32, i32* %10
switch i32 %11, label %17 [
i32 129, label %13
i32 128, label %12
]
12: ; preds = %9
br label %13
13: ; preds = %9, %12
%14 = phi i32 [ 128, %12 ], [ %11, %9 ]
%15 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %12 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %9 ]
%16 = call i32 @ntohs(i32 %14)
br label %17
17: ; preds = %13, %9
%18 = phi i32 [ -1, %9 ], [ %16, %13 ]
%19 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %9 ], [ %15, %13 ]
%20 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 %0, i8* %19, i32 %18)
br label %23
21: ; preds = %2
%22 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i32 %0)
br label %23
23: ; preds = %21, %17
ret void
}