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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char *,
                           int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int write(int, scalar_t__, size_t);
ssize_t writen(int fd, const void *buf, size_t n) {
  size_t done = 0;
  ssize_t ret;
  while (done < n) {
    ret = write(fd, (caddr_t)buf + done, n - done);
    if (ret == -1) {
      if (errno == EINTR) {
        continue;
      }
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        return (ssize_t)done;
      }
      fprintf(
          stderr,
          "%s: "
          "write: %s"
          " (%s:%d)\n",
          "warning", strerror(errno),
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c",
          403);
      ;
      ;
      return -1;
    } else {
      done += ret;
    }
  }
  return (ssize_t)done;
}


x86 assembly (clang, -O0):
<writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    123b <writen+0x10b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    122a <writen+0xfa>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
jne    119d <writen+0x6d>
jmp    114b <writen+0x1b>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
je     11d1 <writen+0xa1>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <writen+0xad>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1242 <writen+0x112>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rcx
lea    0xdfb(%rip),%rsi
lea    0xe0b(%rip),%rdx
lea    0xe0c(%rip),%r8
mov    $0x193,%r9d
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <writen+0x112>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    114b <writen+0x1b>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     11fa <writen+0xca>
mov    %edi,%r13d
xor    %r12d,%r12d
mov    0x2e84(%rip),%r15
mov    0x2e6d(%rip),%r14
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    118d <writen+0x5d>
mov    (%r15),%rdi
cmp    (%r14),%rdi
je     1170 <writen+0x40>
jmp    11a7 <writen+0x77>
cltq
add    %rax,%r12
mov    0x8(%rsp),%rsi
lea    (%r12,%rsi,1),%rbp
mov    0x10(%rsp),%rbx
sub    %r12,%rbx
ja     1170 <writen+0x40>
jmp    11fd <writen+0xcd>
mov    0x2e0a(%rip),%rax
cmp    (%rax),%rdi
je     11fd <writen+0xcd>
mov    0x2e16(%rip),%rax
cmp    (%rax),%rdi
je     11fd <writen+0xcd>
mov    0x2e1a(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe2c(%rip),%rsi
lea    0xe3c(%rip),%rdx
lea    0xe3d(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x193,%r9d
call   1040 <fprintf@plt>
mov    $0xffffffff,%r12d
jmp    11fd <writen+0xcd>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"%s: write: %s (%s:%d)\0A\00"
@.str.1 = [8 x i8] c"warning\00"
@.str.2 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c\00"
define i32 @writen(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
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 = %52, %30, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %53
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = ptrtoint i8* %16 to i64
%18 = load i64, i64* %8
%19 = add i64 %17, %18
%20 = load i64, i64* %7
%21 = load i64, i64* %8
%22 = sub i64 %20, %21
%23 = call i32 @write(i32 %15, i64 %19, i64 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %47
26: ; preds = %14
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %10
31: ; preds = %26
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EWOULDBLOCK
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35, %31
%40 = load i64, i64* %8
%41 = trunc i64 %40 to i32
store i32 %41, i32* %4
br label %56
42: ; preds = %35
%43 = load i32, i32* @stderr
%44 = load i64, i64* @errno
%45 = call i8* @strerror(i64 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 403)
store i32 -1, i32* %4
br label %56
47: ; preds = %14
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = load i64, i64* %8
%51 = add i64 %50, %49
store i64 %51, i64* %8
br label %52
52: ; preds = %47
br label %10
53: ; preds = %10
%54 = load i64, i64* %8
%55 = trunc i64 %54 to i32
store i32 %55, i32* %4
br label %56
56: ; preds = %53, %42, %39
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"%s: write: %s (%s:%d)\0A\00"
@.str.1 = [8 x i8] c"warning\00"
@.str.2 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c\00"
define i32 @writen(i32 %0, i8* %1, i64 %2) {
%4 = ptrtoint i8* %1 to i64
%5 = icmp eq i64 %2, 0
br i1 %5, label %37, label %6
6: ; preds = %3, %29
%7 = phi i64 [ %34, %29 ], [ %2, %3 ]
%8 = phi i64 [ %33, %29 ], [ %4, %3 ]
%9 = phi i64 [ %31, %29 ], [ 0, %3 ]
br label %10
10: ; preds = %6, %13
%11 = tail call i32 @write(i32 %0, i64 %8, i64 %7)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %10, label %17
17: ; preds = %13
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %14, %18
%20 = load i64, i64* @EWOULDBLOCK
%21 = icmp eq i64 %14, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = trunc i64 %9 to i32
br label %37
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = tail call i8* @strerror(i64 %14)
%28 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 403)
br label %37
29: ; preds = %10
%30 = sext i32 %11 to i64
%31 = add i64 %9, %30
%32 = icmp ult i64 %31, %2
%33 = add i64 %31, %4
%34 = sub i64 %2, %31
br i1 %32, label %6, label %35
35: ; preds = %29
%36 = trunc i64 %31 to i32
br label %37
37: ; preds = %3, %35, %25, %23
%38 = phi i32 [ %24, %23 ], [ -1, %25 ], [ 0, %3 ], [ %36, %35 ]
ret i32 %38
}