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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int sendto(int, char const *, int, int /*<<< orphan*/, struct sockaddr *, int);
int sendto_fd(int s, const char *buf, int bufLen) {
  struct sockaddr_nl nladdr;
  int r;
  memset(&nladdr, 0, sizeof(nladdr));
  nladdr.nl_family = AF_NETLINK;
  while ((r = sendto(s, buf, bufLen, 0, (struct sockaddr *)&nladdr,
                     sizeof(nladdr))) < bufLen) {
    if (r > 0) {
      buf += r;
      bufLen -= r;
    } else if (errno != EAGAIN)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sendto_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x18(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x1c(%rbp)
cmp    -0x14(%rbp),%eax
jge    11d0 <sendto_fd+0xb0>
cmpl   $0x0,-0x1c(%rbp)
jle    11a0 <sendto_fd+0x80>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11cb <sendto_fd+0xab>
mov    0x2e31(%rip),%rax
mov    (%rax),%rax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
je     11c6 <sendto_fd+0xa6>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <sendto_fd+0xb7>
jmp    11cb <sendto_fd+0xab>
jmp    114e <sendto_fd+0x2e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendto_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r12d
mov    %rsp,%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0x4,%r9d
call   1040 <sendto@plt>
cmp    %ebx,%eax
jge    11bc <sendto_fd+0x9c>
mov    %rsp,%r15
mov    0x2e4f(%rip),%r13
xor    %ebp,%ebp
jmp    11a3 <sendto_fd+0x83>
nopl   (%rax)
mov    %eax,%ecx
add    %rcx,%r14
sub    %eax,%ebx
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0x4,%r9d
call   1040 <sendto@plt>
cmp    %eax,%ebx
jle    11bc <sendto_fd+0x9c>
test   %eax,%eax
jg     1180 <sendto_fd+0x60>
mov    0x2e2a(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1187 <sendto_fd+0x67>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @sendto_fd(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_nl
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = call i32 @memset(%struct.sockaddr_nl* %8, i32 0, i32 4)
%11 = load i32, i32* @AF_NETLINK
%12 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %8, i32 0, i32 0
store i32 %11, i32* %12
br label %13
13: ; preds = %38, %3
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = bitcast %struct.sockaddr_nl* %8 to %struct.sockaddr*
%18 = call i32 @sendto(i32 %14, i8* %15, i32 %16, i32 0, %struct.sockaddr* %17, i32 4)
store i32 %18, i32* %9
%19 = load i32, i32* %7
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %39
21: ; preds = %13
%22 = load i32, i32* %9
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i32, i32* %9
%26 = load i8*, i8** %6
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %6
%29 = load i32, i32* %9
%30 = load i32, i32* %7
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %7
br label %38
32: ; preds = %21
%33 = load i64, i64* @errno
%34 = load i64, i64* @EAGAIN
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 -1, i32* %4
br label %40
37: ; preds = %32
br label %38
38: ; preds = %37, %24
br label %13
39: ; preds = %13
store i32 0, i32* %4
br label %40
40: ; preds = %39, %36
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @sendto_fd(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_nl
%5 = bitcast %struct.sockaddr_nl* %4 to i8*
%6 = call i32 @memset(%struct.sockaddr_nl* %4, i32 0, i32 4)
%7 = load i32, i32* @AF_NETLINK
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i64 0, i32 0
store i32 %7, i32* %8
%9 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%10 = call i32 @sendto(i32 %0, i8* %1, i32 %2, i32 0, %struct.sockaddr* %9, i32 4)
%11 = icmp slt i32 %10, %2
br i1 %11, label %12, label %30
12: ; preds = %3, %25
%13 = phi i32 [ %28, %25 ], [ %10, %3 ]
%14 = phi i32 [ %27, %25 ], [ %2, %3 ]
%15 = phi i8* [ %26, %25 ], [ %1, %3 ]
%16 = icmp sgt i32 %13, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = zext i32 %13 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = sub nsw i32 %14, %13
br label %25
21: ; preds = %12
%22 = load i64, i64* @errno
%23 = load i64, i64* @EAGAIN
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21, %17
%26 = phi i8* [ %19, %17 ], [ %15, %21 ]
%27 = phi i32 [ %20, %17 ], [ %14, %21 ]
%28 = call i32 @sendto(i32 %0, i8* %26, i32 %27, i32 0, %struct.sockaddr* %9, i32 4)
%29 = icmp sgt i32 %27, %28
br i1 %29, label %12, label %30
30: ; preds = %21, %25, %3
%31 = phi i32 [ 0, %3 ], [ 0, %25 ], [ -1, %21 ]
ret i32 %31
}