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

/* Variables and functions */
int /*<<< orphan*/ hexsummary(int /*<<< orphan*/ *, void const *, size_t);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ orig_sendto(int, void const *, size_t, int,
                               struct sockaddr const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
char *straddr(struct sockaddr const *);
ssize_t sendto(int socket, const void *buffer, size_t length, int flags,
               const struct sockaddr *dest_addr, socklen_t dest_len) {
  if (!dest_addr)
    return sendto(socket, buffer, length, flags, dest_addr, dest_len);
  printf("sendto %s:%d :\n", straddr(dest_addr),
         ntohs(((struct sockaddr_in *)dest_addr)->sin_port));
  hexsummary(NULL, buffer, length);
  return orig_sendto(socket, buffer, length, flags, dest_addr, dest_len);
}


x86 assembly (clang, -O0):
<sendto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ac <sendto+0x4c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
call   1040 <sendto@plt>
mov    %eax,-0x4(%rbp)
jmp    1205 <sendto+0xa5>
mov    -0x28(%rbp),%rdi
call   1080 <straddr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe2f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
call   1050 <hexsummary@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
call   1070 <orig_sendto@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendto>:
test   %r8,%r8
je     11d0 <sendto+0x80>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %edi,%r13d
mov    %r8,%rdi
mov    %r9d,0x4(%rsp)
call   1070 <straddr@plt>
mov    %rax,%r14
mov    (%rbx),%edi
call   1050 <ntohs@plt>
lea    0xe73(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <printf@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1040 <hexsummary@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
mov    %rbx,%r8
mov    0x4(%rsp),%r9d
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <orig_sendto@plt>
nopl   0x0(%rax,%rax,1)
jmp    11d0 <sendto+0x80>

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"sendto %s:%d :\0A\00"
define i32 @sendto(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32 %5) {
%7 = icmp eq %struct.sockaddr* %4, null
br i1 %7, label %8, label %9
8: ; preds = %6, %8
br label %8
9: ; preds = %6
%10 = tail call i8* @straddr(%struct.sockaddr* %4)
%11 = getelementptr %struct.sockaddr, %struct.sockaddr* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @ntohs(i32 %12)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %13)
%15 = tail call i32 @hexsummary(i32* null, i8* %1, i64 %2)
%16 = tail call i32 @orig_sendto(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32 %5)
ret i32 %16
}