#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 {
  int dummy;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ sendto(int, void const *, size_t, int,
                          struct sockaddr const *, int /*<<< orphan*/);
ssize_t sendto_loop(int fd, const void *buf, size_t buf_len, int flags,
                    const struct sockaddr *sa, socklen_t sa_len) {
  for (;;) {
    ssize_t l;
    if ((l = sendto(fd, buf, buf_len, flags, sa, sa_len)) >= 0)
      return l;
    if (errno != EINTR)
      return -errno;
  }
}


x86 assembly (clang, -O0):
<sendto_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
call   1030 <sendto@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jl     1160 <sendto_loop+0x50>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <sendto_loop+0x80>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%eax
je     118b <sendto_loop+0x7b>
xor    %eax,%eax
mov    0x2e5f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <sendto_loop+0x80>
jmp    112e <sendto_loop+0x1e>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendto_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8,%r15
mov    %ecx,%r12d
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %edi,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r13,%rdx
mov    %r12d,%ecx
mov    %r15,%r8
mov    %r14d,%r9d
call   1030 <sendto@plt>
test   %eax,%eax
jns    1160 <sendto_loop+0x50>
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%eax
je     1130 <sendto_loop+0x20>
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i32 0
@EINTR = global i32 0
define i32 @sendto_loop(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca %struct.sockaddr*
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i32 %3, i32* %11
store %struct.sockaddr* %4, %struct.sockaddr** %12
store i32 %5, i32* %13
br label %15
15: ; preds = %33, %6
%16 = load i32, i32* %8
%17 = load i8*, i8** %9
%18 = load i64, i64* %10
%19 = load i32, i32* %11
%20 = load %struct.sockaddr*, %struct.sockaddr** %12
%21 = load i32, i32* %13
%22 = call i32 @sendto(i32 %16, i8* %17, i64 %18, i32 %19, %struct.sockaddr* %20, i32 %21)
store i32 %22, i32* %14
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = load i32, i32* %14
store i32 %25, i32* %7
br label %34
26: ; preds = %15
%27 = load i32, i32* @errno
%28 = load i32, i32* @EINTR
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %7
br label %34
33: ; preds = %26
br label %15
34: ; preds = %30, %24
%35 = load i32, i32* %7
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i32 0
@EINTR = global i32 0
define i32 @sendto_loop(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32 %5) {
br label %7
7: ; preds = %10, %6
%8 = tail call i32 @sendto(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32 %5)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = load i32, i32* @errno
%12 = load i32, i32* @EINTR
%13 = icmp eq i32 %11, %12
br i1 %13, label %7, label %14
14: ; preds = %10
%15 = sub nsw i32 0, %11
br label %16
16: ; preds = %7, %14
%17 = phi i32 [ %15, %14 ], [ %8, %7 ]
ret i32 %17
}