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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           scalar_t__);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int write(int, char *, size_t);
ssize_t rio_writen(int fd, void *usrbuf, size_t n) {
  size_t nleft = n;
  ssize_t nwritten;
  char *bufp = (char *)usrbuf;
  while (nleft > 0) {
    if ((nwritten = write(fd, bufp, nleft)) <= 0) {
      if (errno == EINTR)
        nwritten = 0;
      else {
        fprintf(stderr,
                "[ERROR] (%s:%d: errno: %s) "
                "errno == %d\n"
                "\n",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/hervewenjie/webserver/refs/"
                "heads/master/src/rio.c",
                47, (errno == 0 ? "None" : strerror(errno)), errno);
        return -1;
      }
    }
    nleft -= nwritten;
    bufp += nwritten;
  }
  return n;
}


x86 assembly (clang, -O0):
<rio_writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    123f <rio_writen+0x10f>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     121a <rio_writen+0xea>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <rio_writen+0x70>
movl   $0x0,-0x24(%rbp)
jmp    1215 <rio_writen+0xe5>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e25(%rip),%rax
cmpq   $0x0,(%rax)
jne    11cd <rio_writen+0x9d>
lea    0xef1(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    11e0 <rio_writen+0xb0>
mov    0x2e04(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x40(%rbp),%r8
mov    0x2dea(%rip),%rax
mov    (%rax),%r9
lea    0xe08(%rip),%rsi
lea    0xe2a(%rip),%rdx
mov    $0x2f,%ecx
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1246 <rio_writen+0x116>
jmp    121a <rio_writen+0xea>
movslq -0x24(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1153 <rio_writen+0x23>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rio_writen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     11ce <rio_writen+0x9e>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e84(%rip),%r12
mov    0x2e75(%rip),%r13
mov    %r14,%rbp
jmp    116a <rio_writen+0x3a>
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%rbp
je     11ce <rio_writen+0x9e>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %eax,%eax
jg     1160 <rio_writen+0x30>
mov    (%r12),%rdi
xor    %eax,%eax
cmp    0x0(%r13),%rdi
je     1160 <rio_writen+0x30>
mov    0x2e51(%rip),%rax
mov    (%rax),%ebx
test   %rdi,%rdi
je     11a4 <rio_writen+0x74>
call   1050 <strerror@plt>
mov    %rax,%r8
mov    (%r12),%r9
jmp    11ae <rio_writen+0x7e>
lea    0xf0a(%rip),%r8
xor    %r9d,%r9d
lea    0xe4b(%rip),%rsi
lea    0xe6d(%rip),%rdx
mov    %ebx,%edi
mov    $0x2f,%ecx
call   1040 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%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
@stderr = global i32 0
@.str = [41 x i8] c"[ERROR] (%s:%d: errno: %s) errno == %d\0A\0A\00"
@.str.1 = [140 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hervewenjie/webserver/refs/heads/master/src/rio.c\00"
@.str.2 = [5 x i8] c"None\00"
define i32 @rio_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
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i64, i64* %7
store i64 %11, i64* %8
%12 = load i8*, i8** %6
store i8* %12, i8** %10
br label %13
13: ; preds = %40, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %49
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %8
%20 = call i32 @write(i32 %17, i8* %18, i64 %19)
store i32 %20, i32* %9
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 0, i32* %9
br label %39
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i64, i64* @errno
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
br label %35
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = call i8* @strerror(i64 %33)
br label %35
35: ; preds = %32, %31
%36 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %31 ], [ %34, %32 ]
%37 = load i64, i64* @errno
%38 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([140 x i8], [140 x i8]* @.str.1, i64 0, i64 0), i32 47, i8* %36, i64 %37)
store i32 -1, i32* %4
br label %52
39: ; preds = %26
br label %40
40: ; preds = %39, %16
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = load i64, i64* %8
%44 = sub i64 %43, %42
store i64 %44, i64* %8
%45 = load i32, i32* %9
%46 = load i8*, i8** %10
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %10
br label %13
49: ; preds = %13
%50 = load i64, i64* %7
%51 = trunc i64 %50 to i32
store i32 %51, i32* %4
br label %52
52: ; preds = %49, %35
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [41 x i8] c"[ERROR] (%s:%d: errno: %s) errno == %d\0A\0A\00"
@.str.1 = [140 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hervewenjie/webserver/refs/heads/master/src/rio.c\00"
@.str.2 = [5 x i8] c"None\00"
define i32 @rio_writen(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %30, label %5
5: ; preds = %3, %24
%6 = phi i8* [ %28, %24 ], [ %1, %3 ]
%7 = phi i64 [ %27, %24 ], [ %2, %3 ]
%8 = tail call i32 @write(i32 %0, i8* %6, i64 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %24
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %24, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = icmp eq i64 %11, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i8* @strerror(i64 %11)
%19 = load i64, i64* @errno
br label %20
20: ; preds = %14, %17
%21 = phi i64 [ %19, %17 ], [ 0, %14 ]
%22 = phi i8* [ %18, %17 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %14 ]
%23 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([140 x i8], [140 x i8]* @.str.1, i64 0, i64 0), i32 47, i8* %22, i64 %21)
br label %32
24: ; preds = %10, %5
%25 = phi i32 [ %8, %5 ], [ 0, %10 ]
%26 = zext i32 %25 to i64
%27 = sub i64 %7, %26
%28 = getelementptr inbounds i8, i8* %6, i64 %26
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %5
30: ; preds = %24, %3
%31 = trunc i64 %2 to i32
br label %32
32: ; preds = %30, %20
%33 = phi i32 [ -1, %20 ], [ %31, %30 ]
ret i32 %33
}