#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sk_in;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ htonl(unsigned int);
int /*<<< orphan*/ htons(unsigned short);
int sendto(int const, unsigned char const *, size_t, int /*<<< orphan*/,
           struct sockaddr *, int);
int lin_rsk_lo_sendto(const unsigned char *buffer, size_t buffer_size,
                      const int sockfd) {
  struct sockaddr_in sk_in = {0};
  unsigned int ipv4_addr = 0;
  unsigned short dst_port = 0;
  size_t offset = 0;
  if (((buffer[0] & 0xf0) >> 4) != 4) {
    return -1;
  }
  if (buffer_size < 20) {
    return -1;
  }
  ipv4_addr = (((unsigned short)buffer[16]) << 24) |
              (((unsigned short)buffer[17]) << 16) |
              (((unsigned short)buffer[18]) << 8) |
              ((unsigned short)buffer[19]);
  offset = 4 * (buffer[0] & 0x0f);
  switch (buffer[9]) {
  case 6:
  case 17:
    dst_port = (((unsigned short)buffer[offset + 2]) << 8) | buffer[offset + 3];
    break;
  default:
    dst_port = 0;
    break;
  }
  sk_in.sin_family = AF_INET;
  sk_in.sin_addr.s_addr = htonl(ipv4_addr);
  sk_in.sin_port = htons(dst_port);
  return sendto(sockfd, buffer, buffer_size, 0, (struct sockaddr *)&sk_in,
                sizeof(sk_in));
}


x86 assembly (clang, -O0):
<lin_rsk_lo_sendto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x0,-0x34(%rbp)
movw   $0x0,-0x36(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf0,%eax
sar    $0x4,%eax
cmp    $0x4,%eax
je     119c <lin_rsk_lo_sendto+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <lin_rsk_lo_sendto+0x12d>
cmpq   $0x14,-0x18(%rbp)
jae    11b3 <lin_rsk_lo_sendto+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <lin_rsk_lo_sendto+0x12d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%eax
bswap  %eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
shl    $0x2,%eax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x9(%rax),%eax
mov    %eax,-0x44(%rbp)
sub    $0x6,%eax
je     11fa <lin_rsk_lo_sendto+0xba>
jmp    11e9 <lin_rsk_lo_sendto+0xa9>
mov    -0x44(%rbp),%eax
sub    $0x11,%eax
jne    1225 <lin_rsk_lo_sendto+0xe5>
jmp    11fa <lin_rsk_lo_sendto+0xba>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
movzbl 0x2(%rax,%rcx,1),%eax
movzwl %ax,%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
mov    -0x40(%rbp),%rdx
movzbl 0x3(%rcx,%rdx,1),%ecx
or     %ecx,%eax
mov    %ax,-0x36(%rbp)
jmp    122b <lin_rsk_lo_sendto+0xeb>
movw   $0x0,-0x36(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x2c(%rbp)
movzwl -0x36(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
mov    $0x10,%r9d
call   1060 <sendto@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lin_rsk_lo_sendto>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0x14(%rsp)
mov    $0xffffffff,%eax
cmp    $0x14,%rsi
jb     11d8 <lin_rsk_lo_sendto+0xa8>
mov    %edx,%r14d
mov    %rdi,%rbp
movzbl (%rdi),%ecx
mov    %ecx,%edx
and    $0xfffffff0,%edx
cmp    $0x40,%edx
jne    11d8 <lin_rsk_lo_sendto+0xa8>
mov    %rsi,%r15
mov    0x10(%rbp),%edi
bswap  %edi
mov    0x9(%rbp),%al
cmp    $0x11,%al
je     1177 <lin_rsk_lo_sendto+0x47>
cmp    $0x6,%al
jne    1199 <lin_rsk_lo_sendto+0x69>
shl    $0x2,%ecx
and    $0x3c,%ecx
mov    %rcx,%rax
or     $0x2,%rax
movzbl 0x0(%rbp,%rax,1),%eax
shl    $0x8,%eax
or     $0x3,%rcx
movzbl 0x0(%rbp,%rcx,1),%ebx
or     %eax,%ebx
jmp    119b <lin_rsk_lo_sendto+0x6b>
xor    %ebx,%ebx
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
movzwl %bx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r8
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
mov    $0x10,%r9d
call   1050 <sendto@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define i32 @lin_rsk_lo_sendto(i8* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i16
%11 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%12 = bitcast %struct.sockaddr_in* %8 to i8*
store i32 0, i32* %9
store i16 0, i16* %10
store i64 0, i64* %11
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = and i32 %16, 240
%18 = ashr i32 %17, 4
%19 = icmp ne i32 %18, 4
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %96
21: ; preds = %3
%22 = load i64, i64* %6
%23 = icmp ult i64 %22, 20
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %4
br label %96
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i64 16
%28 = load i8, i8* %27
%29 = zext i8 %28 to i16
%30 = zext i16 %29 to i32
%31 = shl i32 %30, 24
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i64 17
%34 = load i8, i8* %33
%35 = zext i8 %34 to i16
%36 = zext i16 %35 to i32
%37 = shl i32 %36, 16
%38 = or i32 %31, %37
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 18
%41 = load i8, i8* %40
%42 = zext i8 %41 to i16
%43 = zext i16 %42 to i32
%44 = shl i32 %43, 8
%45 = or i32 %38, %44
%46 = load i8*, i8** %5
%47 = getelementptr inbounds i8, i8* %46, i64 19
%48 = load i8, i8* %47
%49 = zext i8 %48 to i16
%50 = zext i16 %49 to i32
%51 = or i32 %45, %50
store i32 %51, i32* %9
%52 = load i8*, i8** %5
%53 = getelementptr inbounds i8, i8* %52, i64 0
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = and i32 %55, 15
%57 = mul nsw i32 4, %56
%58 = sext i32 %57 to i64
store i64 %58, i64* %11
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i64 9
%61 = load i8, i8* %60
%62 = zext i8 %61 to i32
switch i32 %62, label %80 [
i32 6, label %63
i32 17, label %63
]
63: ; preds = %25, %25
%64 = load i8*, i8** %5
%65 = load i64, i64* %11
%66 = add i64 %65, 2
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i8, i8* %67
%69 = zext i8 %68 to i16
%70 = zext i16 %69 to i32
%71 = shl i32 %70, 8
%72 = load i8*, i8** %5
%73 = load i64, i64* %11
%74 = add i64 %73, 3
%75 = getelementptr inbounds i8, i8* %72, i64 %74
%76 = load i8, i8* %75
%77 = zext i8 %76 to i32
%78 = or i32 %71, %77
%79 = trunc i32 %78 to i16
store i16 %79, i16* %10
br label %81
80: ; preds = %25
store i16 0, i16* %10
br label %81
81: ; preds = %80, %63
%82 = load i32, i32* @AF_INET
%83 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %82, i32* %83
%84 = load i32, i32* %9
%85 = call i32 @htonl(i32 %84)
%86 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%87 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %86, i32 0, i32 0
store i32 %85, i32* %87
%88 = load i16, i16* %10
%89 = call i32 @htons(i16 zeroext %88)
%90 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %89, i32* %90
%91 = load i32, i32* %7
%92 = load i8*, i8** %5
%93 = load i64, i64* %6
%94 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%95 = call i32 @sendto(i32 %91, i8* %92, i64 %93, i32 0, %struct.sockaddr* %94, i32 16)
store i32 %95, i32* %4
br label %96
96: ; preds = %81, %24, %20
%97 = load i32, i32* %4
ret i32 %97
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define i32 @lin_rsk_lo_sendto(i8* %0, i64 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 0, i32* %6
%7 = load i8, i8* %0
%8 = zext i8 %7 to i32
%9 = and i32 %8, 240
%10 = icmp ne i32 %9, 64
%11 = icmp ult i64 %1, 20
%12 = or i1 %11, %10
br i1 %12, label %58, label %13
13: ; preds = %3
%14 = getelementptr inbounds i8, i8* %0, i64 16
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = shl nuw i32 %16, 24
%18 = getelementptr inbounds i8, i8* %0, i64 17
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = shl nuw nsw i32 %20, 16
%22 = or i32 %21, %17
%23 = getelementptr inbounds i8, i8* %0, i64 18
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = shl nuw nsw i32 %25, 8
%27 = or i32 %22, %26
%28 = getelementptr inbounds i8, i8* %0, i64 19
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = or i32 %27, %30
%32 = shl nuw nsw i32 %8, 2
%33 = and i32 %32, 60
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %0, i64 9
%36 = load i8, i8* %35
switch i8 %36, label %48 [
i8 6, label %37
i8 17, label %37
]
37: ; preds = %13, %13
%38 = or i64 %34, 2
%39 = getelementptr inbounds i8, i8* %0, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i16
%42 = shl nuw i16 %41, 8
%43 = or i64 %34, 3
%44 = getelementptr inbounds i8, i8* %0, i64 %43
%45 = load i8, i8* %44
%46 = zext i8 %45 to i16
%47 = or i16 %42, %46
br label %48
48: ; preds = %13, %37
%49 = phi i16 [ %47, %37 ], [ 0, %13 ]
%50 = load i32, i32* @AF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %50, i32* %51
%52 = tail call i32 @htonl(i32 %31)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %52, i32* %53
%54 = tail call i32 @htons(i16 zeroext %49)
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %54, i32* %55
%56 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%57 = call i32 @sendto(i32 %2, i8* %0, i64 %1, i32 0, %struct.sockaddr* %56, i32 16)
br label %58
58: ; preds = %3, %48
%59 = phi i32 [ %57, %48 ], [ -1, %3 ]
ret i32 %59
}