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

/* Variables and functions */
scalar_t__ ENOSPC;
scalar_t__ erase_oldest_dump();
scalar_t__ errno;
size_t write(int, void const *, size_t);
ssize_t dump_write(int fd, const void *buf, size_t count) {
  ssize_t written;
  written = 0;
  while (written != count) {
    ssize_t rc;
    rc = write(fd, buf + written, count - written);
    if ((rc == -1) && (errno == ENOSPC)) {
      if (erase_oldest_dump() != 0) {
        written = -1;
        goto out;
      }
    } else if (rc == -1) {
      written = -1;
      goto out;
    } else {
      written += rc;
    }
  }
out:
  return written;
}


x86 assembly (clang, -O0):
<dump_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
je     11db <dump_write+0xbb>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11ad <dump_write+0x8d>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <dump_write+0x8d>
mov    $0x0,%al
call   1040 <erase_oldest_dump@plt>
cmp    $0x0,%rax
je     11a8 <dump_write+0x88>
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
jmp    11e0 <dump_write+0xc0>
jmp    11d6 <dump_write+0xb6>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11c5 <dump_write+0xa5>
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
jmp    11e0 <dump_write+0xc0>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11d6 <dump_write+0xb6>
jmp    113b <dump_write+0x1b>
jmp    11e0 <dump_write+0xc0>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     118e <dump_write+0x6e>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e98(%rip),%r13
xor    %r12d,%r12d
mov    $0xffffffffffffffff,%rbp
jmp    1158 <dump_write+0x38>
nopl   0x0(%rax)
add    %rax,%r12
cmp    %rbx,%r12
je     1192 <dump_write+0x72>
lea    (%r14,%r12,1),%rsi
mov    %rbx,%rdx
sub    %r12,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
jne    1150 <dump_write+0x30>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    1195 <dump_write+0x75>
xor    %eax,%eax
call   1040 <erase_oldest_dump@plt>
test   %rax,%rax
je     1153 <dump_write+0x33>
jmp    1195 <dump_write+0x75>
xor    %ebp,%ebp
jmp    1195 <dump_write+0x75>
mov    %rbx,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOSPC = global i64 0
define i64 @dump_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 = %42, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %43
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = getelementptr i8, i8* %15, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = sub i64 %18, %19
%21 = call i64 @write(i32 %14, i8* %17, i64 %20)
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %33
24: ; preds = %13
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOSPC
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = call i64 (...) @erase_oldest_dump()
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i64 -1, i64* %7
br label %44
32: ; preds = %28
br label %42
33: ; preds = %24, %13
%34 = load i64, i64* %8
%35 = icmp eq i64 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %33
store i64 -1, i64* %7
br label %44
37: ; preds = %33
%38 = load i64, i64* %8
%39 = load i64, i64* %7
%40 = add i64 %39, %38
store i64 %40, i64* %7
br label %41
41: ; preds = %37
br label %42
42: ; preds = %41, %32
br label %9
43: ; preds = %9
br label %44
44: ; preds = %43, %36, %31
%45 = load i64, i64* %7
ret i64 %45
}

LLVM IR (-O2):
@errno = global i64 0
@ENOSPC = global i64 0
define i64 @dump_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %23, label %5
5: ; preds = %3, %20
%6 = phi i64 [ %21, %20 ], [ 0, %3 ]
%7 = getelementptr i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i64 @write(i32 %0, i8* %7, i64 %8)
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %18
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOSPC
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = tail call i64 (...) @erase_oldest_dump()
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %23
18: ; preds = %5
%19 = add i64 %9, %6
br label %20
20: ; preds = %18, %15
%21 = phi i64 [ %6, %15 ], [ %19, %18 ]
%22 = icmp eq i64 %21, %2
br i1 %22, label %23, label %5
23: ; preds = %20, %15, %11, %3
%24 = phi i64 [ 0, %3 ], [ -1, %11 ], [ -1, %15 ], [ %2, %20 ]
ret i64 %24
}