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

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ stdout;
void logSockInfo(int sock, int inOut) {
  socklen_t size;
  struct sockaddr addr;
  struct sockaddr_in *addrin;
  char addrPrint[40];
  size = sizeof(addr);
  if (inOut == 0)
    getsockname(sock, &addr, &size);
  else if (inOut == 1)
    getpeername(sock, &addr, &size);
  addrin = (struct sockaddr_in *)&addr;
  inet_ntop(addrin->sin_family, &(addrin->sin_addr), addrPrint, 40);
  if (inOut == 0)
    printf("Listening on %s:%d\n", addrPrint, ntohs(addrin->sin_port));
  else if (inOut == 1)
    printf("Accepted from %s:%d\n", addrPrint, ntohs(addrin->sin_port));
  fflush(stdout);
}


x86 assembly (clang, -O0):
<logSockInfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x4,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1194 <logSockInfo+0x34>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1080 <getsockname@plt>
jmp    11b3 <logSockInfo+0x53>
cmpl   $0x1,-0x8(%rbp)
jne    11ae <logSockInfo+0x4e>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1040 <getpeername@plt>
jmp    11b3 <logSockInfo+0x53>
lea    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
lea    -0x40(%rbp),%rdx
mov    $0x28,%ecx
call   1030 <inet_ntop@plt>
cmpl   $0x0,-0x8(%rbp)
jne    120c <logSockInfo+0xac>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
jmp    1240 <logSockInfo+0xe0>
cmpl   $0x1,-0x8(%rbp)
jne    123b <logSockInfo+0xdb>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    0xdde(%rip),%rdi
call   1050 <printf@plt>
jmp    1240 <logSockInfo+0xe0>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logSockInfo>:
push   %rbx
sub    $0x40,%rsp
movl   $0x4,0xc(%rsp)
cmp    $0x1,%esi
je     11b6 <logSockInfo+0x56>
test   %esi,%esi
jne    11fc <logSockInfo+0x9c>
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1080 <getsockname@plt>
mov    0x10(%rsp),%edi
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdx
mov    $0x28,%ecx
call   1030 <inet_ntop@plt>
mov    0x8(%rsp),%edi
call   1070 <ntohs@plt>
lea    0xe4c(%rip),%rdi
jmp    11f0 <logSockInfo+0x90>
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1040 <getpeername@plt>
mov    0x10(%rsp),%edi
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdx
mov    $0x28,%ecx
call   1030 <inet_ntop@plt>
mov    0x8(%rsp),%edi
call   1070 <ntohs@plt>
lea    0xe24(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
jmp    1210 <logSockInfo+0xb0>
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    $0x28,%ecx
call   1030 <inet_ntop@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
add    $0x40,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
@.str = [20 x i8] c"Listening on %s:%d\0A\00"
@.str.1 = [21 x i8] c"Accepted from %s:%d\0A\00"
@stdout = global i32 0
define void @logSockInfo(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr
%7 = alloca %struct.sockaddr_in*
%8 = alloca [40 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 4, i32* %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* %3
%13 = call i32 @getsockname(i32 %12, %struct.sockaddr* %6, i32* %5)
br label %21
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %3
%19 = call i32 @getpeername(i32 %18, %struct.sockaddr* %6, i32* %5)
br label %20
20: ; preds = %17, %14
br label %21
21: ; preds = %20, %11
%22 = bitcast %struct.sockaddr* %6 to %struct.sockaddr_in*
store %struct.sockaddr_in* %22, %struct.sockaddr_in** %7
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 2
%25 = load i32, i32* %24
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 1
%28 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%29 = call i32 @inet_ntop(i32 %25, i32* %27, i8* %28, i32 40)
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %21
%33 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @ntohs(i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %33, i32 %37)
br label %50
39: ; preds = %21
%40 = load i32, i32* %4
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %49
42: ; preds = %39
%43 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%44 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %43, i32 %47)
br label %49
49: ; preds = %42, %39
br label %50
50: ; preds = %49, %32
%51 = load i32, i32* @stdout
%52 = call i32 @fflush(i32 %51)
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Listening on %s:%d\0A\00"
@.str.1 = [21 x i8] c"Accepted from %s:%d\0A\00"
@stdout = global i32 0
define void @logSockInfo(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr
%5 = alloca [40 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr* %4 to i8*
%8 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
store i32 4, i32* %3
switch i32 %1, label %19 [
i32 0, label %9
i32 1, label %22
]
9: ; preds = %2
%10 = call i32 @getsockname(i32 %0, %struct.sockaddr* %4, i32* %3)
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i64 2, i32 0
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i64 1, i32 0
%14 = call i32 @inet_ntop(i32 %12, i32* %13, i8* %8, i32 40)
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @ntohs(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %17)
br label %32
19: ; preds = %2
%20 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i64 1, i32 0
%21 = call i32 @inet_ntop(i32 undef, i32* %20, i8* %8, i32 40)
br label %32
22: ; preds = %2
%23 = call i32 @getpeername(i32 %0, %struct.sockaddr* %4, i32* %3)
%24 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i64 2, i32 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i64 1, i32 0
%27 = call i32 @inet_ntop(i32 %25, i32* %26, i8* %8, i32 40)
%28 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
%31 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %8, i32 %30)
br label %32
32: ; preds = %19, %22, %9
%33 = load i32, i32* @stdout
%34 = call i32 @fflush(i32 %33)
ret void
}