#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 */
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ len;

/* Variables and functions */
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ pthread_sigmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
size_t write_all(int, ...);
int send_packet(int fd, const char *buf, size_t len) {
  int ret = 0;
  sigset_t set, old;
  sigemptyset(&set);
  sigaddset(&set, SIGPIPE);
  pthread_sigmask(SIG_BLOCK, &set, &old);
  if (write_all(fd, &len, sizeof(len)) != sizeof(len))
    ret = -1;
  if (!ret && write_all(fd, buf, len) != len)
    ret = -1;
  pthread_sigmask(SIG_SETMASK, &old, NULL);
  return ret;
}


x86 assembly (clang, -O0):
<send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <sigemptyset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1060 <sigaddset@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <pthread_sigmask@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1040 <write_all@plt>
cmp    $0x8,%rax
je     11af <send_packet+0x6f>
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11dc <send_packet+0x9c>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1040 <write_all@plt>
cmp    -0x18(%rbp),%rax
je     11dc <send_packet+0x9c>
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <pthread_sigmask@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_packet>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rdx,(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <sigemptyset@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <sigaddset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <pthread_sigmask@plt>
mov    %rsp,%rsi
mov    $0x8,%edx
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <write_all@plt>
mov    $0xffffffff,%ebx
cmp    $0x8,%rax
jne    11bc <send_packet+0x7c>
mov    (%rsp),%rdx
mov    %ebp,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <write_all@plt>
xor    %ebx,%ebx
cmp    (%rsp),%rax
setne  %bl
neg    %ebx
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <pthread_sigmask@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SIGPIPE = global i32 0
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
define i32 @send_packet(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %7
%10 = call i32 @sigemptyset(i32* %8)
%11 = load i32, i32* @SIGPIPE
%12 = call i32 @sigaddset(i32* %8, i32 %11)
%13 = load i32, i32* @SIG_BLOCK
%14 = call i32 @pthread_sigmask(i32 %13, i32* %8, i32* %9)
%15 = load i32, i32* %4
%16 = call i64 (i32, ...) @write_all(i32 %15, i64* %6, i64 8)
%17 = icmp ne i64 %16, 8
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %7
br label %19
19: ; preds = %18, %3
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %19
%23 = load i32, i32* %4
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = call i64 (i32, ...) @write_all(i32 %23, i8* %24, i64 %25)
%27 = load i64, i64* %6
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 -1, i32* %7
br label %30
30: ; preds = %29, %22, %19
%31 = load i32, i32* @SIG_SETMASK
%32 = call i32 @pthread_sigmask(i32 %31, i32* %9, i32* null)
%33 = load i32, i32* %7
ret i32 %33
}

LLVM IR (-O2):
@SIGPIPE = global i32 0
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
define i32 @send_packet(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i64 %2, i64* %4
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = call i32 @sigemptyset(i32* %5)
%10 = load i32, i32* @SIGPIPE
%11 = call i32 @sigaddset(i32* %5, i32 %10)
%12 = load i32, i32* @SIG_BLOCK
%13 = call i32 @pthread_sigmask(i32 %12, i32* %5, i32* %6)
%14 = call i64 (i32, ...) @write_all(i32 %0, i64* %4, i64 8)
%15 = icmp eq i64 %14, 8
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = load i64, i64* %4
%18 = call i64 (i32, ...) @write_all(i32 %0, i8* %1, i64 %17)
%19 = load i64, i64* %4
%20 = icmp ne i64 %18, %19
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %16, %3
%23 = phi i32 [ -1, %3 ], [ %21, %16 ]
%24 = load i32, i32* @SIG_SETMASK
%25 = call i32 @pthread_sigmask(i32 %24, i32* %6, i32* null)
ret i32 %23
}