#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EMSGSIZE;
scalar_t__ ENOBUFS;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int send(int, void const *, size_t, int /*<<< orphan*/);
int sendto(int, void const *, size_t, int /*<<< orphan*/, struct sockaddr *,
           int);
int do_send(int sk, const void *data, size_t len,
            const struct sockaddr_storage *addr) {
  int res;
  if (!addr || 1)
    res = send(sk, data, len, 0);
  else
    res = sendto(sk, data, len, 0, (struct sockaddr *)addr, sizeof(*addr));
  if (res < 0) {
    if (errno == ENOBUFS || errno == EAGAIN)
      return res;
    if (errno == EMSGSIZE)
      return 0;
    do {
      perror("send");
      return -1;
    } while (0);
  }
  return res;
}


x86 assembly (clang, -O0):
<do_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1161 <do_send+0x31>
mov    $0x1,%al
test   $0x1,%al
jne    1161 <do_send+0x31>
jmp    117b <do_send+0x4b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x24(%rbp)
jmp    119a <do_send+0x6a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1226 <do_send+0xf6>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
je     11d8 <do_send+0xa8>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <do_send+0xb3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <do_send+0xfc>
mov    0x2de6(%rip),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
jne    1209 <do_send+0xd9>
movl   $0x0,-0x4(%rbp)
jmp    122c <do_send+0xfc>
jmp    120e <do_send+0xde>
lea    0xdeb(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <do_send+0xfc>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_send>:
push   %rax
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
js     112e <do_send+0xe>
pop    %rcx
ret
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2eb9(%rip),%rdx
cmp    (%rdx),%rcx
je     112c <do_send+0xc>
mov    0x2e75(%rip),%rdx
cmp    (%rdx),%rcx
je     112c <do_send+0xc>
mov    0x2e89(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     112c <do_send+0xc>
lea    0xe9b(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@ENOBUFS = global i64 0
@EAGAIN = global i64 0
@EMSGSIZE = global i64 0
@.str = [5 x i8] c"send\00"
define i32 @do_send(i32 %0, i8* %1, i64 %2, %struct.sockaddr_storage* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.sockaddr_storage*
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store %struct.sockaddr_storage* %3, %struct.sockaddr_storage** %9
%11 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %9
%12 = icmp ne %struct.sockaddr_storage* %11, null
br i1 %12, label %13, label %14
13: ; preds = %4
br i1 true, label %14, label %19
14: ; preds = %13, %4
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = load i64, i64* %8
%18 = call i32 @send(i32 %15, i8* %16, i64 %17, i32 0)
store i32 %18, i32* %10
br label %26
19: ; preds = %13
%20 = load i32, i32* %6
%21 = load i8*, i8** %7
%22 = load i64, i64* %8
%23 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %9
%24 = bitcast %struct.sockaddr_storage* %23 to %struct.sockaddr*
%25 = call i32 @sendto(i32 %20, i8* %21, i64 %22, i32 0, %struct.sockaddr* %24, i32 4)
store i32 %25, i32* %10
br label %26
26: ; preds = %19, %14
%27 = load i32, i32* %10
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @ENOBUFS
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i64, i64* @errno
%35 = load i64, i64* @EAGAIN
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33, %29
%38 = load i32, i32* %10
store i32 %38, i32* %5
br label %50
39: ; preds = %33
%40 = load i64, i64* @errno
%41 = load i64, i64* @EMSGSIZE
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 0, i32* %5
br label %50
44: ; preds = %39
br label %45
45: ; preds = %44
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %50
47: ; No predecessors!
br label %48
48: ; preds = %47, %26
%49 = load i32, i32* %10
store i32 %49, i32* %5
br label %50
50: ; preds = %48, %45, %43, %37
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
@errno = global i64 0
@ENOBUFS = global i64 0
@EAGAIN = global i64 0
@EMSGSIZE = global i64 0
@.str = [5 x i8] c"send\00"
define i32 @do_send(i32 %0, i8* %1, i64 %2, %struct.sockaddr_storage* nocapture readnone %3) {
%5 = tail call i32 @send(i32 %0, i8* %1, i64 %2, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOBUFS
%10 = icmp eq i64 %8, %9
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %19, label %14
14: ; preds = %7
%15 = load i64, i64* @EMSGSIZE
%16 = icmp eq i64 %8, %15
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %4, %14, %7, %17
%20 = phi i32 [ -1, %17 ], [ %5, %7 ], [ 0, %14 ], [ %5, %4 ]
ret i32 %20
}