#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*/ socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int ntohs(int /*<<< orphan*/);
int orig_connect(int, struct sockaddr const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
char *straddr(struct sockaddr const *);
int connect(int socket, const struct sockaddr *address, socklen_t address_len) {
  if (address->sa_family == AF_INET6 || address->sa_family == AF_INET)
    printf("[*] Connect to %s:%d\n", straddr(address),
           ntohs(((struct sockaddr_in *)address)->sin_port));
  return orig_connect(socket, address, address_len);
}


x86 assembly (clang, -O0):
<connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <connect+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
jne    11aa <connect+0x6a>
mov    -0x10(%rbp),%rdi
call   1060 <straddr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <orig_connect@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%rax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%rax
je     116a <connect+0x2a>
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%rax
jne    118d <connect+0x4d>
mov    %rbx,%rdi
call   1060 <straddr@plt>
mov    %rax,%r15
mov    (%rbx),%edi
call   1050 <ntohs@plt>
lea    0xe7d(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <orig_connect@plt>

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

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