#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;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int sendto(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
           int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_send_udp(char *destIp, int destPort, unsigned char *buf, int len) {
  int result;
  struct sockaddr_in address;
  int s_socket;
  int count = 5;
  s_socket = socket(AF_INET, SOCK_DGRAM, 0);
  bzero(&address, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = inet_addr(destIp);
  address.sin_port = htons(destPort);
  while (count--) {
    result = sendto(s_socket, buf, len, 0, (struct sockaddr *)&address,
                    sizeof(address));
    if (result == len)
      break;
  }
  close(s_socket);
  if ((count == 0) && (result != len))
    return -1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<socket_send_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x5,-0x40(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x3c(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x40(%rbp)
cmp    $0x0,%eax
je     1212 <socket_send_udp+0xb2>
mov    -0x3c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    120d <socket_send_udp+0xad>
jmp    1212 <socket_send_udp+0xb2>
jmp    11ca <socket_send_udp+0x6a>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x40(%rbp)
jne    123c <socket_send_udp+0xdc>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     123c <socket_send_udp+0xdc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <socket_send_udp+0xe3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_send_udp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbp
mov    0x2e68(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%r15d
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %rbp,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %r12d,%edi
xor    %r12d,%r12d
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %ebx,%ebp
cmp    %ebx,%eax
je     1278 <socket_send_udp+0x118>
xor    %r12d,%r12d
lea    0x8(%rsp),%r8
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %ebx,%ebp
cmp    %ebx,%eax
je     1278 <socket_send_udp+0x118>
xor    %r12d,%r12d
lea    0x8(%rsp),%r8
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %ebx,%ebp
cmp    %ebx,%eax
je     1278 <socket_send_udp+0x118>
xor    %r12d,%r12d
lea    0x8(%rsp),%r8
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %ebx,%ebp
cmp    %ebx,%eax
je     1278 <socket_send_udp+0x118>
lea    0x8(%rsp),%r8
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,%ebp
cmp    %ebx,%eax
sete   %r12b
mov    %r15d,%edi
call   1050 <close@plt>
cmp    %ebx,%ebp
setne  %al
and    %r12b,%al
movzbl %al,%eax
neg    %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, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @socket_send_udp(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 5, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %12
%17 = call i32 @bzero(%struct.sockaddr_in* %11, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i8*, i8** %6
%21 = call i32 @inet_addr(i8* %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %7
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %25, i32* %26
br label %27
27: ; preds = %41, %4
%28 = load i32, i32* %13
%29 = add nsw i32 %28, -1
store i32 %29, i32* %13
%30 = icmp ne i32 %28, 0
br i1 %30, label %31, label %42
31: ; preds = %27
%32 = load i32, i32* %12
%33 = load i8*, i8** %8
%34 = load i32, i32* %9
%35 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%36 = call i32 @sendto(i32 %32, i8* %33, i32 %34, i32 0, %struct.sockaddr* %35, i32 12)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = load i32, i32* %9
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %31
br label %42
41: ; preds = %31
br label %27
42: ; preds = %40, %27
%43 = load i32, i32* %12
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %13
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i32, i32* %10
%49 = load i32, i32* %9
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 -1, i32* %5
br label %53
52: ; preds = %47, %42
store i32 0, i32* %5
br label %53
53: ; preds = %52, %51
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @socket_send_udp(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @inet_addr(i8* %0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%18 = call i32 @sendto(i32 %9, i8* %2, i32 %3, i32 0, %struct.sockaddr* %17, i32 12)
%19 = icmp eq i32 %18, %3
br i1 %19, label %32, label %20
20: ; preds = %4
%21 = call i32 @sendto(i32 %9, i8* %2, i32 %3, i32 0, %struct.sockaddr* %17, i32 12)
%22 = icmp eq i32 %21, %3
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = call i32 @sendto(i32 %9, i8* %2, i32 %3, i32 0, %struct.sockaddr* %17, i32 12)
%25 = icmp eq i32 %24, %3
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = call i32 @sendto(i32 %9, i8* %2, i32 %3, i32 0, %struct.sockaddr* %17, i32 12)
%28 = icmp eq i32 %27, %3
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i32 @sendto(i32 %9, i8* %2, i32 %3, i32 0, %struct.sockaddr* %17, i32 12)
%31 = icmp eq i32 %30, %3
br label %32
32: ; preds = %29, %26, %23, %20, %4
%33 = phi i32 [ %3, %4 ], [ %3, %20 ], [ %3, %23 ], [ %3, %26 ], [ %30, %29 ]
%34 = phi i1 [ false, %4 ], [ false, %20 ], [ false, %23 ], [ false, %26 ], [ %31, %29 ]
%35 = call i32 @close(i32 %9)
%36 = icmp ne i32 %33, %3
%37 = select i1 %34, i1 %36, i1 false
%38 = sext i1 %37 to i32
ret i32 %38
}