#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*/ memmove(char *, char *, int);
int write(int, char *, int);
int mywrite(int fd, char *buf, int *len) {
  int num_of_written_bytes = write(fd, buf, *len);
  if (num_of_written_bytes <= 0)
    return num_of_written_bytes;
  if (num_of_written_bytes != *len)
    memmove(buf, buf + num_of_written_bytes, (*len) - num_of_written_bytes);
  *len -= num_of_written_bytes;
  return num_of_written_bytes;
}


x86 assembly (clang, -O0):
<mywrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     115d <mywrite+0x3d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119c <mywrite+0x7c>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     1189 <mywrite+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
sub    -0x1c(%rbp),%edx
call   1040 <memmove@plt>
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mywrite>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    (%rdx),%edx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1152 <mywrite+0x32>
mov    (%rbx),%edx
mov    %ebp,%eax
sub    %ebp,%edx
je     114e <mywrite+0x2e>
mov    %ebp,%esi
add    %r14,%rsi
mov    %r14,%rdi
call   1040 <memmove@plt>
mov    (%rbx),%eax
sub    %ebp,%eax
mov    %eax,(%rbx)
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mywrite(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%9 = load i32, i32* %5
%10 = load i8*, i8** %6
%11 = load i32*, i32** %7
%12 = load i32, i32* %11
%13 = call i32 @write(i32 %9, i8* %10, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* %8
store i32 %17, i32* %4
br label %40
18: ; preds = %3
%19 = load i32, i32* %8
%20 = load i32*, i32** %7
%21 = load i32, i32* %20
%22 = icmp ne i32 %19, %21
br i1 %22, label %23, label %34
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = load i8*, i8** %6
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32*, i32** %7
%30 = load i32, i32* %29
%31 = load i32, i32* %8
%32 = sub nsw i32 %30, %31
%33 = call i32 @memmove(i8* %24, i8* %28, i32 %32)
br label %34
34: ; preds = %23, %18
%35 = load i32, i32* %8
%36 = load i32*, i32** %7
%37 = load i32, i32* %36
%38 = sub nsw i32 %37, %35
store i32 %38, i32* %36
%39 = load i32, i32* %8
store i32 %39, i32* %4
br label %40
40: ; preds = %34, %16
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
define i32 @mywrite(i32 %0, i8* %1, i32* nocapture %2) {
%4 = load i32, i32* %2
%5 = tail call i32 @write(i32 %0, i8* %1, i32 %4)
%6 = icmp slt i32 %5, 1
br i1 %6, label %19, label %7
7: ; preds = %3
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, %5
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = zext i32 %5 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = sub nsw i32 %8, %5
%14 = tail call i32 @memmove(i8* %1, i8* %12, i32 %13)
%15 = load i32, i32* %2
br label %16
16: ; preds = %10, %7
%17 = phi i32 [ %15, %10 ], [ %5, %7 ]
%18 = sub nsw i32 %17, %5
store i32 %18, i32* %2
br label %19
19: ; preds = %3, %16
ret i32 %5
}