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

/* Variables and functions */
int /*<<< orphan*/ SOL_IP;
int /*<<< orphan*/ SOL_IPV6;
int SO_ORIGINAL_DST;
scalar_t__ getsockopt(int, int /*<<< orphan*/, int, struct sockaddr *,
                      int /*<<< orphan*/ *);
int getdestaddr(int fd, struct sockaddr *addr, socklen_t *addrlen) {
  if (getsockopt(fd, SOL_IPV6, 80, addr, addrlen) == 0) {
    return 0;
  }
  if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, addr, addrlen) == 0) {
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<getdestaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ebb(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x50,%edx
call   1030 <getsockopt@plt>
cmp    $0x0,%rax
jne    1157 <getdestaddr+0x47>
movl   $0x0,-0x4(%rbp)
jmp    1196 <getdestaddr+0x86>
mov    -0x8(%rbp),%edi
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
call   1030 <getsockopt@plt>
cmp    $0x0,%rax
jne    118f <getdestaddr+0x7f>
movl   $0x0,-0x4(%rbp)
jmp    1196 <getdestaddr+0x86>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdestaddr>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ec5(%rip),%rax
mov    (%rax),%esi
mov    $0x50,%edx
mov    %rbx,%rcx
mov    %r14,%r8
call   1030 <getsockopt@plt>
test   %rax,%rax
je     1160 <getdestaddr+0x50>
mov    0x2eb7(%rip),%rax
mov    (%rax),%esi
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rcx
mov    %r14,%r8
call   1030 <getsockopt@plt>
neg    %rax
sbb    %eax,%eax
jmp    1162 <getdestaddr+0x52>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOL_IPV6 = global i32 0
@SOL_IP = global i32 0
@SO_ORIGINAL_DST = global i32 0
define i32 @getdestaddr(i32 %0, %struct.sockaddr* %1, i32* %2) {
%4 = load i32, i32* @SOL_IPV6
%5 = tail call i64 @getsockopt(i32 %0, i32 %4, i32 80, %struct.sockaddr* %1, i32* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = load i32, i32* @SOL_IP
%9 = load i32, i32* @SO_ORIGINAL_DST
%10 = tail call i64 @getsockopt(i32 %0, i32 %8, i32 %9, %struct.sockaddr* %1, i32* %2)
%11 = icmp ne i64 %10, 0
%12 = sext i1 %11 to i32
br label %13
13: ; preds = %7, %3
%14 = phi i32 [ 0, %3 ], [ %12, %7 ]
ret i32 %14
}