#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*/ panic(char *);
scalar_t__ write(int, char *, size_t);
void must_write(int fd, char *buf, size_t amt) {
  size_t did;
  did = 0;
  while (did < amt) {
    ssize_t here;
    here = write(fd, buf, amt);
    if (here < 0)
      panic("i/o error on output");
    did += here;
    buf += here;
  }
}


x86 assembly (clang, -O0):
<must_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1191 <must_write+0x71>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1174 <must_write+0x54>
lea    0xe91(%rip),%rdi
call   1040 <panic@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    113b <must_write+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<must_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     117b <must_write+0x5b>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r12d
xor    %ebp,%ebp
lea    0xebe(%rip),%r15
jmp    115b <must_write+0x3b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %r13,%rbp
add    %r13,%rbx
cmp    %r14,%rbp
jae    117b <must_write+0x5b>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %rax,%r13
test   %rax,%rax
jns    1150 <must_write+0x30>
mov    %r15,%rdi
call   1040 <panic@plt>
jmp    1150 <must_write+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"i/o error on output\00"
define void @must_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
store i64 0, i64* %7
br label %9
9: ; preds = %22, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %29
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = call i64 @write(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @panic(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %13
%23 = load i64, i64* %8
%24 = load i64, i64* %7
%25 = add i64 %24, %23
store i64 %25, i64* %7
%26 = load i64, i64* %8
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 %26
store i8* %28, i8** %5
br label %9
29: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"i/o error on output\00"
define void @must_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %16, label %5
5: ; preds = %3, %12
%6 = phi i8* [ %14, %12 ], [ %1, %3 ]
%7 = phi i64 [ %13, %12 ], [ 0, %3 ]
%8 = tail call i64 @write(i32 %0, i8* %6, i64 %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @panic(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %5
%13 = add i64 %8, %7
%14 = getelementptr inbounds i8, i8* %6, i64 %8
%15 = icmp ult i64 %13, %2
br i1 %15, label %5, label %16
16: ; preds = %12, %3
ret void
}