#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, char const *, size_t, int /*<<< orphan*/,
           struct sockaddr *, int);
int lin_rsk_lo_sendto(const 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
movsbl (%rax),%eax
and    $0xf0,%eax
sar    $0x4,%eax
cmp    $0x4,%eax
je     119c <lin_rsk_lo_sendto+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <lin_rsk_lo_sendto+0x14d>
cmpq   $0x14,-0x18(%rbp)
jae    11b3 <lin_rsk_lo_sendto+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <lin_rsk_lo_sendto+0x14d>
mov    -0x10(%rbp),%rcx
movsbl 0x10(%rcx),%eax
shl    $0x18,%eax
movsbl 0x11(%rcx),%edx
shl    $0x10,%edx
or     %edx,%eax
movsbl 0x12(%rcx),%edx
movzwl %dx,%edx
shl    $0x8,%edx
or     %edx,%eax
movsbl 0x13(%rcx),%ecx
movzwl %cx,%ecx
or     %ecx,%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
movsbl 0x9(%rax),%eax
mov    %eax,-0x44(%rbp)
sub    $0x6,%eax
je     121a <lin_rsk_lo_sendto+0xda>
jmp    1209 <lin_rsk_lo_sendto+0xc9>
mov    -0x44(%rbp),%eax
sub    $0x11,%eax
jne    1245 <lin_rsk_lo_sendto+0x105>
jmp    121a <lin_rsk_lo_sendto+0xda>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsbl 0x2(%rax,%rcx,1),%eax
movzwl %ax,%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
mov    -0x40(%rbp),%rdx
movsbl 0x3(%rcx,%rdx,1),%ecx
or     %ecx,%eax
mov    %ax,-0x36(%rbp)
jmp    124b <lin_rsk_lo_sendto+0x10b>
movw   $0x0,-0x36(%rbp)
mov    0x2d96(%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     1202 <lin_rsk_lo_sendto+0xd2>
mov    %edx,%r14d
mov    %rdi,%rbp
movsbl (%rdi),%ecx
mov    %ecx,%edx
and    $0xf0,%edx
cmp    $0x40,%edx
jne    1202 <lin_rsk_lo_sendto+0xd2>
mov    %rsi,%r15
movzbl 0x10(%rbp),%eax
shl    $0x18,%eax
movsbl 0x11(%rbp),%edx
shl    $0x10,%edx
or     %eax,%edx
movsbl 0x12(%rbp),%eax
movzwl %ax,%eax
shl    $0x8,%eax
or     %edx,%eax
movsbl 0x13(%rbp),%edx
movzwl %dx,%edi
or     %eax,%edi
movsbl 0x9(%rbp),%eax
cmp    $0x11,%eax
je     11a1 <lin_rsk_lo_sendto+0x71>
cmp    $0x6,%eax
jne    11c3 <lin_rsk_lo_sendto+0x93>
shl    $0x2,%ecx
and    $0x3c,%ecx
mov    %rcx,%rax
or     $0x2,%rax
movzbl 0x0(%rbp,%rax,1),%eax
shl    $0x8,%eax
or     $0x3,%rcx
movsbl 0x0(%rbp,%rcx,1),%ebx
or     %eax,%ebx
jmp    11c5 <lin_rsk_lo_sendto+0x95>
xor    %ebx,%ebx
mov    0x2e1c(%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 = sext 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 = sext 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 = sext 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 = sext 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 = sext 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 = sext 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 = sext 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 = sext 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 = sext 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 = sext 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 %61, 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 = sext i8 %19 to i32
%21 = shl nsw i32 %20, 16
%22 = or i32 %21, %17
%23 = getelementptr inbounds i8, i8* %0, i64 18
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = shl nsw i32 %25, 8
%27 = and i32 %26, 16776960
%28 = or i32 %22, %27
%29 = getelementptr inbounds i8, i8* %0, i64 19
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = and i32 %31, 65535
%33 = or i32 %28, %32
%34 = shl nuw nsw i32 %8, 2
%35 = and i32 %34, 60
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %0, i64 9
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
switch i32 %39, label %51 [
i32 6, label %40
i32 17, label %40
]
40: ; preds = %13, %13
%41 = or i64 %36, 2
%42 = getelementptr inbounds i8, i8* %0, i64 %41
%43 = load i8, i8* %42
%44 = zext i8 %43 to i16
%45 = shl nuw i16 %44, 8
%46 = or i64 %36, 3
%47 = getelementptr inbounds i8, i8* %0, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i16
%50 = or i16 %45, %49
br label %51
51: ; preds = %13, %40
%52 = phi i16 [ %50, %40 ], [ 0, %13 ]
%53 = load i32, i32* @AF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %53, i32* %54
%55 = tail call i32 @htonl(i32 %33)
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %55, i32* %56
%57 = tail call i32 @htons(i16 zeroext %52)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %57, i32* %58
%59 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%60 = call i32 @sendto(i32 %2, i8* %0, i64 %1, i32 0, %struct.sockaddr* %59, i32 16)
br label %61
61: ; preds = %3, %51
%62 = phi i32 [ %60, %51 ], [ -1, %3 ]
ret i32 %62
}