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

/* Variables and functions */
int /*<<< orphan*/ SOL_IP;
int /*<<< orphan*/ SO_ORIGINAL_DST;
int get_addr_name(struct sockaddr *, char *, int);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct sockaddr *,
               int *);
int get_original_dst(int fd, char *dst, socklen_t dst_size) {
  struct sockaddr_storage addr;
  socklen_t addr_len = sizeof(addr);
  if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, (struct sockaddr *)&addr,
                 &addr_len) == -1)
    return -1;
  int port = get_addr_name((struct sockaddr *)&addr, dst, dst_size);
  if (port == -1)
    return -1;
  return port;
}


x86 assembly (clang, -O0):
<get_original_dst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eb5(%rip),%rax
mov    (%rax),%esi
mov    0x2e8c(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1040 <getsockopt@plt>
cmp    $0xffffffff,%eax
jne    1170 <get_original_dst+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <get_original_dst+0x7f>
lea    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <get_addr_name@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1199 <get_original_dst+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <get_original_dst+0x7f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_original_dst>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
movl   $0x4,0x4(%rsp)
mov    0x2ec1(%rip),%rax
mov    (%rax),%esi
mov    0x2e98(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
lea    0x4(%rsp),%r8
call   1040 <getsockopt@plt>
cmp    $0xffffffff,%eax
je     1163 <get_original_dst+0x43>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <get_addr_name@plt>
jmp    1168 <get_original_dst+0x48>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@SOL_IP = global i32 0
@SO_ORIGINAL_DST = global i32 0
define i32 @get_original_dst(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 4, i32* %9
%11 = load i32, i32* %5
%12 = load i32, i32* @SOL_IP
%13 = load i32, i32* @SO_ORIGINAL_DST
%14 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%15 = call i32 @getsockopt(i32 %11, i32 %12, i32 %13, %struct.sockaddr* %14, i32* %9)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %28
18: ; preds = %3
%19 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = call i32 @get_addr_name(%struct.sockaddr* %19, i8* %20, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %18
store i32 -1, i32* %4
br label %28
26: ; preds = %18
%27 = load i32, i32* %10
store i32 %27, i32* %4
br label %28
28: ; preds = %26, %25, %17
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@SOL_IP = global i32 0
@SO_ORIGINAL_DST = global i32 0
define i32 @get_original_dst(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast %struct.sockaddr_storage* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%8 = load i32, i32* @SOL_IP
%9 = load i32, i32* @SO_ORIGINAL_DST
%10 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%11 = call i32 @getsockopt(i32 %0, i32 %8, i32 %9, %struct.sockaddr* %10, i32* %5)
%12 = icmp eq i32 %11, -1
br i1 %12, label %15, label %13
13: ; preds = %3
%14 = call i32 @get_addr_name(%struct.sockaddr* %10, i8* %1, i32 %2)
br label %15
15: ; preds = %3, %13
%16 = phi i32 [ %14, %13 ], [ -1, %3 ]
ret i32 %16
}