#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__ const,
                           size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, void const *, size_t);
void checked_write(int fd, const void *buf, size_t count) {
  const ssize_t written = write(fd, buf, count);
  if (written < 0) {
    perror("write");
    exit(1);
  } else {
    const size_t uwritten = (size_t)written;
    if (uwritten == count) {
      return;
    } else {
      fprintf(stderr, "Incomplete write(2): %d of %d bytes\n", written, count);
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<checked_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
jge    1188 <checked_write+0x48>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11a4 <checked_write+0x64>
add    $0x30,%rsp
pop    %rbp
ret
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe4a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<checked_write>:
push   %rbx
mov    %rdx,%rbx
call   1030 <write@plt>
test   %rax,%rax
js     1155 <checked_write+0x15>
cmp    %rbx,%rax
jne    116b <checked_write+0x2b>
pop    %rbx
ret
lea    0xea4(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%edi
lea    0xe8b(%rip),%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Incomplete write(2): %d of %d bytes\0A\00"
define void @checked_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i64 @write(i32 %9, i8* %10, i64 %11)
store i64 %12, i64* %7
%13 = load i64, i64* %7
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i64, i64* %7
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = load i64, i64* %6
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %18
ret void
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i64, i64* %7
%27 = load i64, i64* %6
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %26, i64 %27)
%29 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Incomplete write(2): %d of %d bytes\0A\00"
define void @checked_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @write(i32 %0, i8* %1, i64 %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3
%10 = icmp eq i64 %4, %2
br i1 %10, label %11, label %12
11: ; preds = %9
ret void
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %4, i64 %2)
%15 = tail call i32 @exit(i32 1)
unreachable
}