#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_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EPROTO;
int /*<<< orphan*/ byte_copy(int /*<<< orphan*/ *, int, unsigned char const *);
int /*<<< orphan*/ byte_zero(struct sockaddr_in *, int);
int /*<<< orphan*/ errno;
int sendto(int, unsigned char const *, long long, int /*<<< orphan*/,
           struct sockaddr *, int);
int socket_send(int fd, const unsigned char *x, long long xlen,
                const unsigned char *ip, const unsigned char *port) {
  struct sockaddr_in sa;
  if (xlen < 0 || xlen > 1048576) {
    errno = EPROTO;
    return -1;
  }
  byte_zero(&sa, sizeof sa);
  sa.sin_family = AF_INET;
  byte_copy(&sa.sin_addr, 4, ip);
  byte_copy(&sa.sin_port, 2, port);
  return sendto(fd, x, xlen, 0, (struct sockaddr *)&sa, sizeof sa);
}


x86 assembly (clang, -O0):
<socket_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jl     1164 <socket_send+0x34>
cmpq   $0x100000,-0x18(%rbp)
jle    1182 <socket_send+0x52>
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <socket_send+0xb3>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1030 <byte_zero@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x4,%rdi
mov    -0x20(%rbp),%rdx
mov    $0x4,%esi
call   1050 <byte_copy@plt>
lea    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x2,%esi
call   1050 <byte_copy@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x100001,%rdx
jb     1160 <socket_send+0x30>
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    0x2e79(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    11c9 <socket_send+0x99>
mov    %r8,%r12
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x8(%rsp),%r13
mov    %r13,%rdi
mov    $0xc,%esi
call   1030 <byte_zero@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    %rbp,%rdx
call   1050 <byte_copy@plt>
mov    %r13,%rdi
mov    $0x2,%esi
mov    %r12,%rdx
call   1050 <byte_copy@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %r13,%r8
mov    $0xc,%r9d
call   1040 <sendto@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@EPROTO = global i32 0
@errno = global i32 0
@AF_INET = global i32 0
define i32 @socket_send(i32 %0, i8* %1, i64 %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca %struct.sockaddr_in
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%13 = load i64, i64* %9
%14 = icmp slt i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %5
%16 = load i64, i64* %9
%17 = icmp sgt i64 %16, 1048576
br i1 %17, label %18, label %20
18: ; preds = %15, %5
%19 = load i32, i32* @EPROTO
store i32 %19, i32* @errno
store i32 -1, i32* %6
br label %35
20: ; preds = %15
%21 = call i32 @byte_zero(%struct.sockaddr_in* %12, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%25 = load i8*, i8** %10
%26 = call i32 @byte_copy(i32* %24, i32 4, i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%28 = load i8*, i8** %11
%29 = call i32 @byte_copy(i32* %27, i32 2, i8* %28)
%30 = load i32, i32* %7
%31 = load i8*, i8** %8
%32 = load i64, i64* %9
%33 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%34 = call i32 @sendto(i32 %30, i8* %31, i64 %32, i32 0, %struct.sockaddr* %33, i32 12)
store i32 %34, i32* %6
br label %35
35: ; preds = %20, %18
%36 = load i32, i32* %6
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@EPROTO = global i32 0
@errno = global i32 0
@AF_INET = global i32 0
define i32 @socket_send(i32 %0, i8* %1, i64 %2, i8* %3, i8* %4) {
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = icmp ugt i64 %2, 1048576
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = load i32, i32* @EPROTO
store i32 %10, i32* @errno
br label %21
11: ; preds = %5
%12 = call i32 @byte_zero(%struct.sockaddr_in* %6, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%16 = call i32 @byte_copy(i32* %15, i32 4, i8* %3)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%18 = call i32 @byte_copy(i32* %17, i32 2, i8* %4)
%19 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%20 = call i32 @sendto(i32 %0, i8* %1, i64 %2, i32 0, %struct.sockaddr* %19, i32 12)
br label %21
21: ; preds = %11, %9
%22 = phi i32 [ -1, %9 ], [ %20, %11 ]
ret i32 %22
}