#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*/ uint8_t;
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
size_t write(int, int /*<<< orphan*/ *, size_t);
void serial_write(int fd, uint8_t *buf, size_t count) {
  ssize_t n = write(fd, buf, count);
  if (n < 0) {
    perror("write");
    exit(1);
  }
  if (n != count) {
    fprintf(stderr, "Short write:  Tired to write %zd, only wrote %zd\n", count,
            n);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<serial_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    1188 <serial_write+0x48>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11bd <serial_write+0x7d>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe58(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_write>:
push   %rbx
mov    %rdx,%rbx
call   1030 <write@plt>
cmp    %rbx,%rax
jne    1140 <serial_write+0x10>
pop    %rbx
ret
mov    0x2e99(%rip),%rcx
mov    (%rcx),%edi
lea    0xeb0(%rip),%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [50 x i8] c"Short write: Tired to write %zd, only wrote %zd\0A\00"
define void @serial_write(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%8 = load i32, i32* %4
%9 = load i32*, i32** %5
%10 = load i64, i64* %6
%11 = call i64 @write(i32 %8, i32* %9, i64 %10)
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = icmp ult i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i64, i64* %7
%19 = load i64, i64* %6
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i64, i64* %6
%24 = load i64, i64* %7
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i64 %23, i64 %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [50 x i8] c"Short write: Tired to write %zd, only wrote %zd\0A\00"
define void @serial_write(i32 %0, i32* nocapture %1, i64 %2) {
%4 = tail call i64 @write(i32 %0, i32* %1, i64 %2)
%5 = icmp eq i64 %4, %2
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i64 %2, i64 %4)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
ret void
}