#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 {
  scalar_t__ ss_family;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int INET6_ADDRSTRLEN;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ SOL_IP;
int /*<<< orphan*/ SOL_IPV6;
int SO_ORIGINAL_DST;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getsockname(int, struct sockaddr *, int *);
int getsockopt(int, int /*<<< orphan*/, int, struct sockaddr *, int *);
int htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int get_org_dstaddr(int sockfd, struct sockaddr_storage *orig_dst) {
  char orig_dst_str[INET6_ADDRSTRLEN];
  socklen_t addrlen = sizeof(*orig_dst);
  int r;
  memset(orig_dst, 0, addrlen);
  r = getsockopt(sockfd, SOL_IP, SO_ORIGINAL_DST, (struct sockaddr *)orig_dst,
                 &addrlen);
  if (r < 0)
    r = getsockopt(sockfd, SOL_IPV6, 80, (struct sockaddr *)orig_dst, &addrlen);
  if (r < 0) {
    fprintf(stderr, "both SO_ORIGINAL_DST and IP6T_SO_ORIGINAL_DST failed !\n");
    r = getsockname(sockfd, (struct sockaddr *)orig_dst, &addrlen);
    if (r < 0) {
      perror("getsockname: ");
      return -1;
    }
  }
  if (orig_dst->ss_family == AF_INET) {
    inet_ntop(AF_INET, &(((struct sockaddr_in *)orig_dst)->sin_addr),
              orig_dst_str, INET_ADDRSTRLEN);
    fprintf(stderr, "Original destination for socket %d : %s:%d\n", sockfd,
            orig_dst_str, htons(((struct sockaddr_in *)orig_dst)->sin_port));
  } else if (orig_dst->ss_family == AF_INET6) {
    inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)orig_dst)->sin6_addr),
              orig_dst_str, INET6_ADDRSTRLEN);
    fprintf(stderr, "Original destination for socket %d : [%s]:%d\n", sockfd,
            orig_dst_str, htons(((struct sockaddr_in6 *)orig_dst)->sin6_port));
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_org_dstaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x8,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rcx
lea    -0x24(%rbp),%r8
call   1060 <getsockopt@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1210 <get_org_dstaddr+0xa0>
mov    -0x8(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0x50,%edx
lea    -0x24(%rbp),%r8
call   1060 <getsockopt@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1272 <get_org_dstaddr+0x102>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1090 <getsockname@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    126d <get_org_dstaddr+0xfd>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1370 <get_org_dstaddr+0x200>
jmp    1272 <get_org_dstaddr+0x102>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%rax
jne    12ea <get_org_dstaddr+0x17a>
mov    -0x38(%rbp),%rdx
mov    0x2d3c(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    0x2d02(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    %eax,%r8d
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1362 <get_org_dstaddr+0x1f2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2cd0(%rip),%rcx
cmp    (%rcx),%rax
jne    135d <get_org_dstaddr+0x1ed>
mov    -0x38(%rbp),%rdx
mov    0x2cbc(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    0x2cba(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    %eax,%r8d
lea    0xd1c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1362 <get_org_dstaddr+0x1f2>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_org_dstaddr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e4d(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
movl   $0x8,-0x2c(%rbp)
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%r8
mov    %r14d,%edi
mov    %rbx,%rcx
call   1060 <getsockopt@plt>
test   %eax,%eax
jns    122a <get_org_dstaddr+0xba>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    -0x2c(%rbp),%r8
mov    %r14d,%edi
mov    $0x50,%edx
mov    %rbx,%rcx
call   1060 <getsockopt@plt>
test   %eax,%eax
jns    122a <get_org_dstaddr+0xba>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    -0x2c(%rbp),%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1090 <getsockname@plt>
test   %eax,%eax
js     12c7 <get_org_dstaddr+0x157>
mov    (%rbx),%rdi
mov    0x2d9c(%rip),%rax
cmp    (%rax),%rdi
jne    1268 <get_org_dstaddr+0xf8>
lea    0x4(%rbx),%rsi
mov    0x2d64(%rip),%rax
mov    (%rax),%ecx
mov    %r15,%rdx
call   1030 <inet_ntop@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%r13d
mov    (%rbx),%edi
call   1040 <htons@plt>
lea    0xde0(%rip),%rsi
jmp    129f <get_org_dstaddr+0x12f>
mov    0x2d59(%rip),%rax
xor    %r12d,%r12d
cmp    (%rax),%rdi
jne    12b5 <get_org_dstaddr+0x145>
lea    0x4(%rbx),%rsi
mov    0x0(%r13),%ecx
mov    %r15,%rdx
call   1030 <inet_ntop@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%r13d
mov    (%rbx),%edi
call   1040 <htons@plt>
lea    0xdd3(%rip),%rsi
xor    %r12d,%r12d
mov    %r13d,%edi
mov    %r14d,%edx
mov    %r15,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r12d
jmp    12b5 <get_org_dstaddr+0x145>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@INET6_ADDRSTRLEN = global i32 0
@SOL_IP = global i32 0
@SO_ORIGINAL_DST = global i32 0
@SOL_IPV6 = global i32 0
@stderr = global i32 0
@.str = [56 x i8] c"both SO_ORIGINAL_DST and IP6T_SO_ORIGINAL_DST failed !\0A\00"
@.str.1 = [14 x i8] c"getsockname: \00"
@AF_INET = global i64 0
@INET_ADDRSTRLEN = global i32 0
@.str.2 = [44 x i8] c"Original destination for socket %d : %s:%d\0A\00"
@AF_INET6 = global i64 0
@.str.3 = [46 x i8] c"Original destination for socket %d : [%s]:%d\0A\00"
define i32 @get_org_dstaddr(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_storage* %1, %struct.sockaddr_storage** %5
%11 = load i32, i32* @INET6_ADDRSTRLEN
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
store i32 8, i32* %8
%15 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%16 = load i32, i32* %8
%17 = call i32 @memset(%struct.sockaddr_storage* %15, i32 0, i32 %16)
%18 = load i32, i32* %4
%19 = load i32, i32* @SOL_IP
%20 = load i32, i32* @SO_ORIGINAL_DST
%21 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%22 = bitcast %struct.sockaddr_storage* %21 to %struct.sockaddr*
%23 = call i32 @getsockopt(i32 %18, i32 %19, i32 %20, %struct.sockaddr* %22, i32* %8)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %2
%27 = load i32, i32* %4
%28 = load i32, i32* @SOL_IPV6
%29 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%30 = bitcast %struct.sockaddr_storage* %29 to %struct.sockaddr*
%31 = call i32 @getsockopt(i32 %27, i32 %28, i32 80, %struct.sockaddr* %30, i32* %8)
store i32 %31, i32* %9
br label %32
32: ; preds = %26, %2
%33 = load i32, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%38 = load i32, i32* %4
%39 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%40 = bitcast %struct.sockaddr_storage* %39 to %struct.sockaddr*
%41 = call i32 @getsockname(i32 %38, %struct.sockaddr* %40, i32* %8)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %35
%45 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %10
br label %91
46: ; preds = %35
br label %47
47: ; preds = %46, %32
%48 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%49 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %48, i32 0, i32 0
%50 = load i64, i64* %49
%51 = load i64, i64* @AF_INET
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %68
53: ; preds = %47
%54 = load i64, i64* @AF_INET
%55 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%56 = bitcast %struct.sockaddr_storage* %55 to %struct.sockaddr_in*
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %56, i32 0, i32 1
%58 = load i32, i32* @INET_ADDRSTRLEN
%59 = call i32 @inet_ntop(i64 %54, i32* %57, i8* %14, i32 %58)
%60 = load i32, i32* @stderr
%61 = load i32, i32* %4
%62 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%63 = bitcast %struct.sockaddr_storage* %62 to %struct.sockaddr_in*
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i32 @htons(i32 %65)
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %61, i8* %14, i32 %66)
br label %90
68: ; preds = %47
%69 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%70 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %69, i32 0, i32 0
%71 = load i64, i64* %70
%72 = load i64, i64* @AF_INET6
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %89
74: ; preds = %68
%75 = load i64, i64* @AF_INET6
%76 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%77 = bitcast %struct.sockaddr_storage* %76 to %struct.sockaddr_in6*
%78 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %77, i32 0, i32 1
%79 = load i32, i32* @INET6_ADDRSTRLEN
%80 = call i32 @inet_ntop(i64 %75, i32* %78, i8* %14, i32 %79)
%81 = load i32, i32* @stderr
%82 = load i32, i32* %4
%83 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%84 = bitcast %struct.sockaddr_storage* %83 to %struct.sockaddr_in6*
%85 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %84, i32 0, i32 0
%86 = load i32, i32* %85
%87 = call i32 @htons(i32 %86)
%88 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %82, i8* %14, i32 %87)
br label %89
89: ; preds = %74, %68
br label %90
90: ; preds = %89, %53
store i32 0, i32* %3
store i32 1, i32* %10
br label %91
91: ; preds = %90, %44
%92 = load i8*, i8** %6
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@INET6_ADDRSTRLEN = global i32 0
@SOL_IP = global i32 0
@SO_ORIGINAL_DST = global i32 0
@SOL_IPV6 = global i32 0
@stderr = global i32 0
@.str = [56 x i8] c"both SO_ORIGINAL_DST and IP6T_SO_ORIGINAL_DST failed !\0A\00"
@.str.1 = [14 x i8] c"getsockname: \00"
@AF_INET = global i64 0
@INET_ADDRSTRLEN = global i32 0
@.str.2 = [44 x i8] c"Original destination for socket %d : %s:%d\0A\00"
@AF_INET6 = global i64 0
@.str.3 = [46 x i8] c"Original destination for socket %d : [%s]:%d\0A\00"
define i32 @get_org_dstaddr(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = load i32, i32* @INET6_ADDRSTRLEN
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32* %3 to i8*
store i32 8, i32* %3
%8 = tail call i32 @memset(%struct.sockaddr_storage* %1, i32 0, i32 8)
%9 = load i32, i32* @SOL_IP
%10 = load i32, i32* @SO_ORIGINAL_DST
%11 = bitcast %struct.sockaddr_storage* %1 to %struct.sockaddr*
%12 = call i32 @getsockopt(i32 %0, i32 %9, i32 %10, %struct.sockaddr* %11, i32* %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %2
%15 = load i32, i32* @SOL_IPV6
%16 = call i32 @getsockopt(i32 %0, i32 %15, i32 80, %struct.sockaddr* %11, i32* %3)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @getsockname(i32 %0, %struct.sockaddr* %11, i32* %3)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %53
25: ; preds = %2, %18, %14
%26 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @AF_INET
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %40
30: ; preds = %25
%31 = bitcast %struct.sockaddr_storage* %1 to %struct.sockaddr_in*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i64 0, i32 1
%33 = load i32, i32* @INET_ADDRSTRLEN
%34 = call i32 @inet_ntop(i64 %27, i32* %32, i8* %6, i32 %33)
%35 = load i32, i32* @stderr
%36 = bitcast %struct.sockaddr_storage* %1 to i32*
%37 = load i32, i32* %36
%38 = call i32 @htons(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %0, i8* %6, i32 %38)
br label %53
40: ; preds = %25
%41 = load i64, i64* @AF_INET6
%42 = icmp eq i64 %27, %41
br i1 %42, label %43, label %53
43: ; preds = %40
%44 = bitcast %struct.sockaddr_storage* %1 to %struct.sockaddr_in6*
%45 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %44, i64 0, i32 1
%46 = load i32, i32* @INET6_ADDRSTRLEN
%47 = call i32 @inet_ntop(i64 %27, i32* %45, i8* %6, i32 %46)
%48 = load i32, i32* @stderr
%49 = bitcast %struct.sockaddr_storage* %1 to i32*
%50 = load i32, i32* %49
%51 = call i32 @htons(i32 %50)
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %0, i8* %6, i32 %51)
br label %53
53: ; preds = %30, %43, %40, %23
%54 = phi i32 [ -1, %23 ], [ 0, %40 ], [ 0, %43 ], [ 0, %30 ]
ret i32 %54
}