#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 */
#define EINTR 128
int EIO;
int errno;
int /*<<< orphan*/ log_failed_assert(char *, int, char *);
scalar_t__ write(int, void const *, size_t);
ssize_t write_all(int fd, const void *buf, size_t count) {
  ssize_t written = 0;
  while (count > 0) {
    ssize_t r = write(fd, buf, count);
    if (r < 0) {
      switch (errno) {
      case EINTR:
        break;
      default:
        return -1;
      }
    } else if (0 == r) {
      errno = EIO;
      return -1;
    } else {
      do {
        if (!(r <= (ssize_t)count))
          log_failed_assert(
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/PatchyFog/lighttpd1.4-mbedtls/refs/"
              "heads/master/src/log.c",
              72,
              "assertion failed: "
              "r <= (ssize_t) count");
      } while (0);
      written += r;
      buf = r + (char const *)buf;
      count -= r;
    }
  }
  return written;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1221 <write_all+0x101>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1195 <write_all+0x75>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1183 <write_all+0x63>
jmp    117e <write_all+0x5e>
jmp    1190 <write_all+0x70>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1229 <write_all+0x109>
jmp    121c <write_all+0xfc>
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    11c0 <write_all+0xa0>
mov    0x2e38(%rip),%rax
mov    (%rax),%ecx
mov    0x2e27(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1229 <write_all+0x109>
jmp    11c5 <write_all+0xa5>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jle    11eb <write_all+0xcb>
lea    0xe26(%rip),%rdi
mov    $0x48,%esi
lea    0xeae(%rip),%rdx
call   1040 <log_failed_assert@plt>
jmp    11f0 <write_all+0xd0>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    121c <write_all+0xfc>
jmp    113b <write_all+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11ac <write_all+0x8c>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
xor    %r14d,%r14d
mov    0x2e96(%rip),%r13
jmp    115b <write_all+0x3b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %r12,%r14
add    %r12,%r15
test   %rbx,%rbx
je     11c5 <write_all+0xa5>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     11a0 <write_all+0x80>
je     11b1 <write_all+0x91>
mov    %rax,%r12
sub    %rax,%rbx
jge    1150 <write_all+0x30>
lea    0xe82(%rip),%rdi
mov    $0x48,%esi
lea    0xf0a(%rip),%rdx
call   1040 <log_failed_assert@plt>
jmp    1150 <write_all+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpl   $0x80,0x0(%r13)
je     1156 <write_all+0x36>
jmp    11be <write_all+0x9e>
xor    %r14d,%r14d
jmp    11c5 <write_all+0xa5>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x0(%r13)
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EIO = global i32 0
@.str = [148 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/PatchyFog/lighttpd1.4-mbedtls/refs/heads/master/src/log.c\00"
@.str.1 = [39 x i8] c"assertion failed: r <= (ssize_t) count\00"
define i64 @write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %49, %3
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %50
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i64 @write(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %9
%18 = load i64, i64* %9
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* @errno
switch i32 %21, label %23 [
i32 128, label %22
]
22: ; preds = %20
br label %24
23: ; preds = %20
store i64 -1, i64* %4
br label %52
24: ; preds = %22
br label %49
25: ; preds = %13
%26 = load i64, i64* %9
%27 = icmp eq i64 0, %26
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i32, i32* @EIO
store i32 %29, i32* @errno
store i64 -1, i64* %4
br label %52
30: ; preds = %25
br label %31
31: ; preds = %30
%32 = load i64, i64* %9
%33 = load i64, i64* %7
%34 = icmp sle i64 %32, %33
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = call i32 @log_failed_assert(i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str, i64 0, i64 0), i32 72, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
br label %38
38: ; preds = %37
%39 = load i64, i64* %9
%40 = load i64, i64* %8
%41 = add nsw i64 %40, %39
store i64 %41, i64* %8
%42 = load i64, i64* %9
%43 = load i8*, i8** %6
%44 = getelementptr inbounds i8, i8* %43, i64 %42
store i8* %44, i8** %6
%45 = load i64, i64* %9
%46 = load i64, i64* %7
%47 = sub i64 %46, %45
store i64 %47, i64* %7
br label %48
48: ; preds = %38
br label %49
49: ; preds = %48, %24
br label %10
50: ; preds = %10
%51 = load i64, i64* %8
store i64 %51, i64* %4
br label %52
52: ; preds = %50, %28, %23
%53 = load i64, i64* %4
ret i64 %53
}

LLVM IR (-O2):
@errno = global i32 0
@EIO = global i32 0
@.str = [148 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/PatchyFog/lighttpd1.4-mbedtls/refs/heads/master/src/log.c\00"
@.str.1 = [39 x i8] c"assertion failed: r <= (ssize_t) count\00"
define i64 @write_all(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %31, label %5
5: ; preds = %3, %26
%6 = phi i64 [ %29, %26 ], [ 0, %3 ]
%7 = phi i64 [ %28, %26 ], [ %2, %3 ]
%8 = phi i8* [ %27, %26 ], [ %1, %3 ]
%9 = tail call i64 @write(i32 %0, i8* %8, i64 %7)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = load i32, i32* @errno
%13 = icmp eq i32 %12, 128
br i1 %13, label %26, label %31
14: ; preds = %5
%15 = icmp eq i64 %9, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = load i32, i32* @EIO
store i32 %17, i32* @errno
br label %31
18: ; preds = %14
%19 = icmp slt i64 %7, %9
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = tail call i32 @log_failed_assert(i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str, i64 0, i64 0), i32 72, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
%23 = add nsw i64 %9, %6
%24 = getelementptr inbounds i8, i8* %8, i64 %9
%25 = sub i64 %7, %9
br label %26
26: ; preds = %22, %11
%27 = phi i8* [ %8, %11 ], [ %24, %22 ]
%28 = phi i64 [ %7, %11 ], [ %25, %22 ]
%29 = phi i64 [ %6, %11 ], [ %23, %22 ]
%30 = icmp eq i64 %28, 0
br i1 %30, label %31, label %5
31: ; preds = %26, %11, %3, %16
%32 = phi i64 [ -1, %16 ], [ 0, %3 ], [ %29, %26 ], [ -1, %11 ]
ret i64 %32
}