#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, char *, unsigned int const);
void writing(int sockfd, void *buffer, const unsigned len) {
  unsigned delivered = 0;
  int n;
  char *ptr = (char *)buffer;
  while (delivered < len) {
    n = write(sockfd, ptr, len - delivered);
    if (n < 0) {
      perror("ERROR writing to socket");
      exit(-1);
    } else {
      delivered += n;
      ptr += n;
    }
  }
}


x86 assembly (clang, -O0):
<writing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11b1 <writing+0x81>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1192 <writing+0x62>
lea    0xe7d(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1151 <writing+0x21>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writing>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     1170 <writing+0x40>
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edx
sub    %ebp,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
test   %eax,%eax
js     117b <writing+0x4b>
add    %eax,%ebp
mov    %eax,%eax
add    %rax,%rbx
cmp    %r15d,%ebp
jb     1150 <writing+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe7e(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"ERROR writing to socket\00"
define void @writing(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%10 = load i8*, i8** %5
store i8* %10, i8** %9
br label %11
11: ; preds = %35, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %36
15: ; preds = %11
%16 = load i32, i32* %4
%17 = load i8*, i8** %9
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = sub i32 %18, %19
%21 = call i32 @write(i32 %16, i8* %17, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %15
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = add i32 %29, %28
store i32 %30, i32* %7
%31 = load i32, i32* %8
%32 = load i8*, i8** %9
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %9
br label %35
35: ; preds = %27
br label %11
36: ; preds = %11
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"ERROR writing to socket\00"
define void @writing(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %19, label %5
5: ; preds = %3, %14
%6 = phi i8* [ %17, %14 ], [ %1, %3 ]
%7 = phi i32 [ %15, %14 ], [ 0, %3 ]
%8 = sub i32 %2, %7
%9 = tail call i32 @write(i32 %0, i8* %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %5
%15 = add i32 %9, %7
%16 = zext i32 %9 to i64
%17 = getelementptr inbounds i8, i8* %6, i64 %16
%18 = icmp ult i32 %15, %2
br i1 %18, label %5, label %19
19: ; preds = %14, %3
ret void
}