#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__ EINTR;
int /*<<< orphan*/ LOG_ERR;
scalar_t__ errno;
int /*<<< orphan*/ fsync(int);
size_t send(int, unsigned char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, int,
                          int /*<<< orphan*/);
int writeall(int socket, unsigned char *buffer, size_t bytes) {
  int rtn = (2 == 1);
  size_t alreadyWriteBytes = 0;
  while (alreadyWriteBytes < bytes) {
    ssize_t writeBytes =
        send(socket, buffer + alreadyWriteBytes, bytes - alreadyWriteBytes, 0);
    if (writeBytes > 0) {
      alreadyWriteBytes += writeBytes;
      if (alreadyWriteBytes == bytes)
        break;
    } else {
      if (writeBytes == -1) {
        if (errno == EINTR)
          continue;
        syslog(LOG_ERR, "%s:%d: %s",
               "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
               "outputs/2022-01-22/02-19-57/repos/powo01/csslh/refs/heads/"
               "master/readWrite.c",
               59, strerror(errno));
      }
      break;
    }
  }
  if (alreadyWriteBytes == bytes) {
    fsync(socket);
    rtn = (1 == 1);
  }
  return (rtn);
}


x86 assembly (clang, -O0):
<writeall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    122a <writeall+0xea>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11bd <writeall+0x7d>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11b8 <writeall+0x78>
jmp    122a <writeall+0xea>
jmp    1225 <writeall+0xe5>
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1220 <writeall+0xe0>
mov    0x2e11(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    11e7 <writeall+0xa7>
jmp    1162 <writeall+0x22>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%r8d
lea    0xdf1(%rip),%rsi
lea    0xdf4(%rip),%rdx
mov    $0x3b,%ecx
call   1040 <syslog@plt>
jmp    122a <writeall+0xea>
jmp    1162 <writeall+0x22>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1247 <writeall+0x107>
mov    -0x4(%rbp),%edi
call   1050 <fsync@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeall>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    %rbx,%rbp
jae    1166 <writeall+0x46>
lea    (%r15,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r14d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
add    %rax,%rbp
test   %rax,%rax
je     1166 <writeall+0x46>
cmp    %rbx,%rbp
jne    1140 <writeall+0x20>
xor    %eax,%eax
cmp    %rbx,%rbp
jne    117a <writeall+0x5a>
mov    %r14d,%edi
call   1040 <fsync@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@LOG_ERR = global i32 0
@.str = [10 x i8] c"%s:%d: %s\00"
@.str.1 = [133 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/powo01/csslh/refs/heads/master/readWrite.c\00"
define i32 @writeall(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %7
store i64 0, i64* %8
br label %10
10: ; preds = %48, %41, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %49
14: ; preds = %10
%15 = load i32, i32* %4
%16 = load i8*, i8** %5
%17 = load i64, i64* %8
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %6
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = call i64 @send(i32 %15, i8* %18, i64 %21, i32 0)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp ugt i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %14
%26 = load i64, i64* %9
%27 = load i64, i64* %8
%28 = add i64 %27, %26
store i64 %28, i64* %8
%29 = load i64, i64* %8
%30 = load i64, i64* %6
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %25
br label %49
33: ; preds = %25
br label %48
34: ; preds = %14
%35 = load i64, i64* %9
%36 = icmp eq i64 %35, -1
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
br label %10
42: ; preds = %37
%43 = load i32, i32* @LOG_ERR
%44 = load i64, i64* @errno
%45 = call i32 @strerror(i64 %44)
%46 = call i32 @syslog(i32 %43, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 59, i32 %45)
br label %47
47: ; preds = %42, %34
br label %49
48: ; preds = %33
br label %10
49: ; preds = %47, %32, %10
%50 = load i64, i64* %8
%51 = load i64, i64* %6
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* %4
%55 = call i32 @fsync(i32 %54)
store i32 1, i32* %7
br label %56
56: ; preds = %53, %49
%57 = load i32, i32* %7
ret i32 %57
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@LOG_ERR = global i32 0
define i32 @writeall(i32 %0, i8* %1, i64 %2) {
br label %4
4: ; preds = %7, %3
%5 = phi i64 [ 0, %3 ], [ %12, %7 ]
%6 = icmp ult i64 %5, %2
br i1 %6, label %7, label %15
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %1, i64 %5
%9 = sub i64 %2, %5
%10 = tail call i64 @send(i32 %0, i8* %8, i64 %9, i32 0)
%11 = icmp eq i64 %10, 0
%12 = add i64 %10, %5
%13 = icmp eq i64 %12, %2
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %15, label %4
15: ; preds = %7, %4
%16 = phi i64 [ %12, %7 ], [ %5, %4 ]
%17 = icmp eq i64 %16, %2
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @fsync(i32 %0)
br label %20
20: ; preds = %18, %15
%21 = phi i32 [ 1, %18 ], [ 0, %15 ]
ret i32 %21
}