#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 EINTR;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int sock_bsendto(int s, void *buf, int len, char *hostname, int port) {
  int flag = 0;
  int oldfl, ret, comp = len;
  int olderrno;
  struct sockaddr_in sa;
  oldfl = fcntl(s, F_GETFL, 0);
  if (oldfl & O_NONBLOCK) {
    fcntl(s, F_SETFL, oldfl & (~O_NONBLOCK));
    flag = 1;
  }
  memset((char *)&sa, 0, sizeof(struct sockaddr_in));
  sa.sin_family = AF_INET;
  inet_aton(hostname, &sa.sin_addr);
  sa.sin_port = htons(port);
  while (comp) {
  bsendto_restart:
    if ((ret = sendto(s, (char *)buf, comp, 0, (struct sockaddr *)&sa,
                      sizeof(struct sockaddr_in))) < 0) {
      if (errno == EINTR)
        goto bsendto_restart;
      olderrno = errno;
      if (flag)
        fcntl(s, F_SETFL, oldfl | O_NONBLOCK);
      errno = olderrno;
      return -1;
    }
    comp -= ret;
    buf += ret;
  }
  if (flag)
    fcntl(s, F_SETFL, oldfl | O_NONBLOCK);
  return (len - comp);
}


x86 assembly (clang, -O0):
<sock_bsendto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e49(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11cb <sock_bsendto+0x7b>
mov    -0x8(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
movl   $0x1,-0x28(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x48(%rbp),%rsi
add    $0x4,%rsi
call   1070 <inet_aton@plt>
mov    -0x24(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12c2 <sock_bsendto+0x172>
jmp    1212 <sock_bsendto+0xc2>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%edx
lea    -0x48(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    12a1 <sock_bsendto+0x151>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%eax
jne    1256 <sock_bsendto+0x106>
jmp    1212 <sock_bsendto+0xc2>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1289 <sock_bsendto+0x139>
mov    -0x8(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    0x2d5e(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x38(%rbp),%ecx
mov    0x2d35(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <sock_bsendto+0x1a2>
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1203 <sock_bsendto+0xb3>
cmpl   $0x0,-0x28(%rbp)
je     12e9 <sock_bsendto+0x199>
mov    -0x8(%rbp),%edi
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    0x2cfe(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_bsendto>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,0x14(%rsp)
mov    %rcx,%r13
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r12d
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebp
mov    0x2e58(%rip),%rax
mov    (%rax),%r15d
test   %ebp,%r15d
je     11a8 <sock_bsendto+0x58>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edx
not    %edx
and    %ebp,%edx
mov    %r12d,%edi
call   1030 <fcntl@plt>
mov    %r15d,0x8(%rsp)
mov    %ebp,0xc(%rsp)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x1c(%rsp),%rsi
mov    %r13,%rdi
call   1070 <inet_aton@plt>
mov    0x14(%rsp),%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
test   %r14d,%r14d
je     124b <sock_bsendto+0xfb>
lea    0x18(%rsp),%r13
mov    0x2dbe(%rip),%r15
mov    %r14d,%ebp
mov    %r14d,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
test   %eax,%eax
jns    123d <sock_bsendto+0xed>
mov    0x2d95(%rip),%rax
mov    (%rax),%r14d
cmp    (%r15),%r14d
je     1210 <sock_bsendto+0xc0>
jmp    1273 <sock_bsendto+0x123>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
mov    0x10(%rsp),%r14d
jne    1210 <sock_bsendto+0xc0>
mov    0xc(%rsp),%ecx
test   %ecx,0x8(%rsp)
je     12a9 <sock_bsendto+0x159>
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
or     %ecx,%edx
mov    %r12d,%edi
call   1030 <fcntl@plt>
jmp    12a9 <sock_bsendto+0x159>
mov    0xc(%rsp),%ecx
test   %ecx,0x8(%rsp)
je     1299 <sock_bsendto+0x149>
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
or     %ecx,%edx
mov    %r12d,%edi
call   1030 <fcntl@plt>
mov    0x2d28(%rip),%rax
mov    %r14d,(%rax)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x28,%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 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@AF_INET = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @sock_bsendto(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca %struct.sockaddr_in
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 0, i32* %12
%18 = load i32, i32* %9
store i32 %18, i32* %15
%19 = load i32, i32* %7
%20 = load i32, i32* @F_GETFL
%21 = call i32 @fcntl(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %13
%22 = load i32, i32* %13
%23 = load i32, i32* @O_NONBLOCK
%24 = and i32 %22, %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %5
%27 = load i32, i32* %7
%28 = load i32, i32* @F_SETFL
%29 = load i32, i32* %13
%30 = load i32, i32* @O_NONBLOCK
%31 = xor i32 %30, -1
%32 = and i32 %29, %31
%33 = call i32 @fcntl(i32 %27, i32 %28, i32 %32)
store i32 1, i32* %12
br label %34
34: ; preds = %26, %5
%35 = bitcast %struct.sockaddr_in* %17 to i8*
%36 = call i32 @memset(i8* %35, i32 0, i32 12)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 2
store i32 %37, i32* %38
%39 = load i8*, i8** %10
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 1
%41 = call i32 @inet_aton(i8* %39, i32* %40)
%42 = load i32, i32* %11
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
store i32 %43, i32* %44
br label %45
45: ; preds = %74, %34
%46 = load i32, i32* %15
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %82
48: ; preds = %45
br label %49
49: ; preds = %60, %48
%50 = load i32, i32* %7
%51 = load i8*, i8** %8
%52 = load i32, i32* %15
%53 = bitcast %struct.sockaddr_in* %17 to %struct.sockaddr*
%54 = call i32 @sendto(i32 %50, i8* %51, i32 %52, i32 0, %struct.sockaddr* %53, i32 12)
store i32 %54, i32* %14
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %74
56: ; preds = %49
%57 = load i32, i32* @errno
%58 = load i32, i32* @EINTR
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %61
60: ; preds = %56
br label %49
61: ; preds = %56
%62 = load i32, i32* @errno
store i32 %62, i32* %16
%63 = load i32, i32* %12
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %61
%66 = load i32, i32* %7
%67 = load i32, i32* @F_SETFL
%68 = load i32, i32* %13
%69 = load i32, i32* @O_NONBLOCK
%70 = or i32 %68, %69
%71 = call i32 @fcntl(i32 %66, i32 %67, i32 %70)
br label %72
72: ; preds = %65, %61
%73 = load i32, i32* %16
store i32 %73, i32* @errno
store i32 -1, i32* %6
br label %96
74: ; preds = %49
%75 = load i32, i32* %14
%76 = load i32, i32* %15
%77 = sub nsw i32 %76, %75
store i32 %77, i32* %15
%78 = load i32, i32* %14
%79 = load i8*, i8** %8
%80 = sext i32 %78 to i64
%81 = getelementptr i8, i8* %79, i64 %80
store i8* %81, i8** %8
br label %45
82: ; preds = %45
%83 = load i32, i32* %12
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %92
85: ; preds = %82
%86 = load i32, i32* %7
%87 = load i32, i32* @F_SETFL
%88 = load i32, i32* %13
%89 = load i32, i32* @O_NONBLOCK
%90 = or i32 %88, %89
%91 = call i32 @fcntl(i32 %86, i32 %87, i32 %90)
br label %92
92: ; preds = %85, %82
%93 = load i32, i32* %9
%94 = load i32, i32* %15
%95 = sub nsw i32 %93, %94
store i32 %95, i32* %6
br label %96
96: ; preds = %92, %72
%97 = load i32, i32* %6
ret i32 %97
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@AF_INET = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @sock_bsendto(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = load i32, i32* @F_GETFL
%9 = tail call i32 @fcntl(i32 %0, i32 %8, i32 0)
%10 = load i32, i32* @O_NONBLOCK
%11 = and i32 %10, %9
%12 = icmp eq i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %5
%14 = load i32, i32* @F_SETFL
%15 = xor i32 %10, -1
%16 = and i32 %9, %15
%17 = tail call i32 @fcntl(i32 %0, i32 %14, i32 %16)
br label %18
18: ; preds = %13, %5
%19 = call i32 @memset(i8* %7, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%23 = call i32 @inet_aton(i8* %3, i32* %22)
%24 = call i32 @htons(i32 %4)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %24, i32* %25
%26 = icmp eq i32 %2, 0
br i1 %26, label %51, label %27
27: ; preds = %18
%28 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %29
29: ; preds = %27, %46
%30 = phi i32 [ %2, %27 ], [ %47, %46 ]
%31 = phi i8* [ %1, %27 ], [ %49, %46 ]
br label %32
32: ; preds = %29, %35
%33 = call i32 @sendto(i32 %0, i8* %31, i32 %30, i32 0, %struct.sockaddr* %28, i32 12)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %46
35: ; preds = %32
%36 = load i32, i32* @errno
%37 = load i32, i32* @EINTR
%38 = icmp eq i32 %36, %37
br i1 %38, label %32, label %39
39: ; preds = %35
br i1 %12, label %45, label %40
40: ; preds = %39
%41 = load i32, i32* @F_SETFL
%42 = load i32, i32* @O_NONBLOCK
%43 = or i32 %42, %9
%44 = call i32 @fcntl(i32 %0, i32 %41, i32 %43)
br label %45
45: ; preds = %40, %39
store i32 %36, i32* @errno
br label %57
46: ; preds = %32
%47 = sub nsw i32 %30, %33
%48 = zext i32 %33 to i64
%49 = getelementptr i8, i8* %31, i64 %48
%50 = icmp eq i32 %47, 0
br i1 %50, label %51, label %29
51: ; preds = %46, %18
br i1 %12, label %57, label %52
52: ; preds = %51
%53 = load i32, i32* @F_SETFL
%54 = load i32, i32* @O_NONBLOCK
%55 = or i32 %54, %9
%56 = call i32 @fcntl(i32 %0, i32 %53, i32 %55)
br label %57
57: ; preds = %51, %52, %45
%58 = phi i32 [ -1, %45 ], [ %2, %52 ], [ %2, %51 ]
ret i32 %58
}