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

/* Variables and functions */
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
size_t sendto(int, void *, size_t, int /*<<< orphan*/, struct sockaddr *, int);
int ez_sendto(int sock, void *data, size_t length, int addr_family, char *host,
              int port) {
  struct sockaddr_in dest_addr;
  memset(&dest_addr, 0, sizeof(struct sockaddr_in));
  dest_addr.sin_family = addr_family;
  dest_addr.sin_port = htons(port);
  if (inet_pton(addr_family, host, &(dest_addr.sin_addr)) == 0) {
    printf("Error: invalid IP address:%s\n", host);
    return 0;
  }
  if (sendto(sock, data, length, 0, (struct sockaddr *)&dest_addr,
             sizeof(struct sockaddr_in)) != length) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<ez_sendto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
add    $0x4,%rdx
call   1070 <inet_pton@plt>
cmp    $0x0,%rax
jne    11c9 <ez_sendto+0x79>
mov    -0x28(%rbp),%rsi
lea    0xe48(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1202 <ez_sendto+0xb2>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
cmp    -0x18(%rbp),%rax
je     11fb <ez_sendto+0xab>
movl   $0x0,-0x4(%rbp)
jmp    1202 <ez_sendto+0xb2>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ez_sendto>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%ebx
mov    %r8,%r13
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,0x10(%rsp)
mov    %edi,%r12d
mov    %rsp,%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    %r15d,(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rdx
mov    %r15d,%edi
mov    %r13,%rsi
call   1070 <inet_pton@plt>
test   %rax,%rax
je     11cd <ez_sendto+0x7d>
mov    %rsp,%r8
mov    %r12d,%edi
mov    0x10(%rsp),%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
xor    %ebp,%ebp
cmp    %r14,%rax
sete   %bpl
jmp    11dc <ez_sendto+0x8c>
lea    0xe2c(%rip),%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
mov    %ebp,%eax
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 }
@.str = [30 x i8] c"Error: invalid IP address:%s\0A\00"
define i32 @ez_sendto(i32 %0, i8* %1, i64 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca %struct.sockaddr_in
store i32 %0, i32* %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%15 = call i32 @memset(%struct.sockaddr_in* %14, i32 0, i32 12)
%16 = load i32, i32* %11
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %13
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* %11
%22 = load i8*, i8** %12
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%24 = call i64 @inet_pton(i32 %21, i8* %22, i32* %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %6
%27 = load i8*, i8** %12
%28 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 0, i32* %7
br label %39
29: ; preds = %6
%30 = load i32, i32* %8
%31 = load i8*, i8** %9
%32 = load i64, i64* %10
%33 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%34 = call i64 @sendto(i32 %30, i8* %31, i64 %32, i32 0, %struct.sockaddr* %33, i32 12)
%35 = load i64, i64* %10
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %29
store i32 0, i32* %7
br label %39
38: ; preds = %29
store i32 1, i32* %7
br label %39
39: ; preds = %38, %37, %26
%40 = load i32, i32* %7
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [30 x i8] c"Error: invalid IP address:%s\0A\00"
define i32 @ez_sendto(i32 %0, i8* %1, i64 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca %struct.sockaddr_in
%8 = bitcast %struct.sockaddr_in* %7 to i8*
%9 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
store i32 %3, i32* %10
%11 = call i32 @htons(i32 %5)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1
%14 = call i64 @inet_pton(i32 %3, i8* %4, i32* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %23
18: ; preds = %6
%19 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%20 = call i64 @sendto(i32 %0, i8* %1, i64 %2, i32 0, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i64 %20, %2
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %18, %16
%24 = phi i32 [ 0, %16 ], [ %22, %18 ]
ret i32 %24
}