#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 {
  scalar_t__ sa_family;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ hexsummary(int /*<<< orphan*/ *, void const *, size_t);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ orig_send(int, void const *, size_t, int);
int /*<<< orphan*/ printf(char *, char *, int);
char *straddr(struct sockaddr *);
ssize_t send(int socket, const void *buffer, size_t length, int flags) {
  socklen_t addr_len = sizeof(struct sockaddr);
  struct sockaddr addr;
  getpeername(socket, &addr, &addr_len);
  if (addr.sa_family != AF_INET && addr.sa_family != AF_INET6)
    return orig_send(socket, buffer, length, flags);
  printf("send %s:%d :\n", straddr(&addr),
         ntohs(((struct sockaddr_in *)&addr)->sin_port));
  hexsummary(NULL, buffer, length);
  return orig_send(socket, buffer, length, flags);
}


x86 assembly (clang, -O0):
<send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x8,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <getpeername@plt>
mov    -0x28(%rbp),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <send+0x70>
mov    -0x28(%rbp),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <send+0x70>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <orig_send@plt>
mov    %eax,-0x4(%rbp)
jmp    121e <send+0xbe>
lea    -0x28(%rbp),%rdi
call   1080 <straddr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <ntohs@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe0e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
call   1060 <hexsummary@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <orig_send@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%ebx
movl   $0x8,0xc(%rsp)
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
call   1040 <getpeername@plt>
mov    (%rsp),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <send+0x48>
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <send+0x79>
mov    %rsp,%rdi
call   1080 <straddr@plt>
mov    %rax,%r12
mov    (%rsp),%edi
call   1070 <ntohs@plt>
lea    0xe3e(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1060 <hexsummary@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <orig_send@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [14 x i8] c"send %s:%d :\0A\00"
define i32 @send(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i32 8, i32* %10
%12 = load i32, i32* %6
%13 = call i32 @getpeername(i32 %12, %struct.sockaddr* %11, i32* %10)
%14 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %11, i32 0, i32 0
%15 = load i64, i64* %14
%16 = load i64, i64* @AF_INET
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %4
%19 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %11, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @AF_INET6
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i32, i32* %6
%25 = load i8*, i8** %7
%26 = load i64, i64* %8
%27 = load i32, i32* %9
%28 = call i32 @orig_send(i32 %24, i8* %25, i64 %26, i32 %27)
store i32 %28, i32* %5
br label %44
29: ; preds = %18, %4
%30 = call i8* @straddr(%struct.sockaddr* %11)
%31 = bitcast %struct.sockaddr* %11 to %struct.sockaddr_in*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @ntohs(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %30, i32 %34)
%36 = load i8*, i8** %7
%37 = load i64, i64* %8
%38 = call i32 @hexsummary(i32* null, i8* %36, i64 %37)
%39 = load i32, i32* %6
%40 = load i8*, i8** %7
%41 = load i64, i64* %8
%42 = load i32, i32* %9
%43 = call i32 @orig_send(i32 %39, i8* %40, i64 %41, i32 %42)
store i32 %43, i32* %5
br label %44
44: ; preds = %29, %23
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [14 x i8] c"send %s:%d :\0A\00"
define i32 @send(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr
%7 = bitcast i32* %5 to i8*
store i32 8, i32* %5
%8 = bitcast %struct.sockaddr* %6 to i8*
%9 = call i32 @getpeername(i32 %0, %struct.sockaddr* %6, i32* %5)
%10 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %6, i64 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* @AF_INET
%13 = icmp eq i64 %11, %12
%14 = load i64, i64* @AF_INET6
%15 = icmp eq i64 %11, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %17, label %24
17: ; preds = %4
%18 = call i8* @straddr(%struct.sockaddr* %6)
%19 = bitcast %struct.sockaddr* %6 to i32*
%20 = load i32, i32* %19
%21 = call i32 @ntohs(i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %21)
%23 = call i32 @hexsummary(i32* null, i8* %1, i64 %2)
br label %24
24: ; preds = %4, %17
%25 = call i32 @orig_send(i32 %0, i8* %1, i64 %2, i32 %3)
ret i32 %25
}