#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 size_t uint8_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ stderr;
size_t write(int, void const *, size_t);
void serial_write_packet(int fd, const void *packet) {
  size_t sent, len = ((uint8_t *)packet)[0];
  sent = write(fd, packet, len);
  if (sent != len) {
    fprintf(stderr, "Warning: only sent %d of %d bytes\n", (int)sent, (int)len);
  }
}


x86 assembly (clang, -O0):
<serial_write_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     117d <serial_write_packet+0x5d>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
lea    0xe88(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_write_packet>:
push   %rbx
mov    (%rsi),%rbx
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    %rbx,%rax
jne    1133 <serial_write_packet+0x13>
pop    %rbx
ret
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%edi
lea    0xebd(%rip),%rsi
mov    %eax,%edx
mov    %ebx,%ecx
pop    %rbx
jmp    1040 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"Warning: only sent %d of %d bytes\0A\00"
define void @serial_write_packet(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = bitcast i8* %7 to i64*
%9 = getelementptr inbounds i64, i64* %8, i64 0
%10 = load i64, i64* %9
store i64 %10, i64* %6
%11 = load i32, i32* %3
%12 = load i8*, i8** %4
%13 = load i64, i64* %6
%14 = call i64 @write(i32 %11, i8* %12, i64 %13)
store i64 %14, i64* %5
%15 = load i64, i64* %5
%16 = load i64, i64* %6
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %25
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i64, i64* %5
%21 = trunc i64 %20 to i32
%22 = load i64, i64* %6
%23 = trunc i64 %22 to i32
%24 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %23)
br label %25
25: ; preds = %18, %2
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"Warning: only sent %d of %d bytes\0A\00"
define void @serial_write_packet(i32 %0, i8* nocapture %1) {
%3 = bitcast i8* %1 to i64*
%4 = load i64, i64* %3
%5 = tail call i64 @write(i32 %0, i8* %1, i64 %4)
%6 = icmp eq i64 %5, %4
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = trunc i64 %5 to i32
%10 = trunc i64 %4 to i32
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
br label %12
12: ; preds = %7, %2
ret void
}