#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 */

/* Variables and functions */
unsigned char safewrite(int, void const *, unsigned char);
int write_sf_packet(int fd, const void *packet, int len) {
  unsigned char l = len;
  if (safewrite(fd, &l, 1) != 1 || safewrite(fd, packet, l) != l)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<write_sf_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,-0x15(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <safewrite@plt>
movzbl %al,%eax
cmp    $0x1,%eax
jne    1164 <write_sf_packet+0x54>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movzbl -0x15(%rbp),%edx
call   1030 <safewrite@plt>
movzbl %al,%eax
movzbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
je     1170 <write_sf_packet+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    1177 <write_sf_packet+0x67>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sf_packet>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %dl,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <safewrite@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1,%cl
jne    1153 <write_sf_packet+0x43>
movzbl 0x7(%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <safewrite@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    0x7(%rsp),%cl
setne  %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_sf_packet(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = trunc i32 %9 to i8
store i8 %10, i8* %8
%11 = load i32, i32* %5
%12 = call zeroext i8 @safewrite(i32 %11, i8* %8, i8 zeroext 1)
%13 = zext i8 %12 to i32
%14 = icmp ne i32 %13, 1
br i1 %14, label %24, label %15
15: ; preds = %3
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i8, i8* %8
%19 = call zeroext i8 @safewrite(i32 %16, i8* %17, i8 zeroext %18)
%20 = zext i8 %19 to i32
%21 = load i8, i8* %8
%22 = zext i8 %21 to i32
%23 = icmp ne i32 %20, %22
br i1 %23, label %24, label %25
24: ; preds = %15, %3
store i32 -1, i32* %4
br label %26
25: ; preds = %15
store i32 0, i32* %4
br label %26
26: ; preds = %25, %24
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
define i32 @write_sf_packet(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = trunc i32 %2 to i8
store i8 %5, i8* %4
%6 = call zeroext i8 @safewrite(i32 %0, i8* %4, i8 zeroext 1)
%7 = icmp eq i8 %6, 1
br i1 %7, label %8, label %14
8: ; preds = %3
%9 = load i8, i8* %4
%10 = call zeroext i8 @safewrite(i32 %0, i8* %1, i8 zeroext %9)
%11 = load i8, i8* %4
%12 = icmp ne i8 %10, %11
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %8, %3
%15 = phi i32 [ -1, %3 ], [ %13, %8 ]
ret i32 %15
}