#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*/ memcpy(char *, char *, unsigned int);
char *unwind_write(char *pos, char *src, unsigned int size, char *dest_head,
                   unsigned int dest_length) {
  unsigned int remaining_bytes = dest_head + dest_length - pos;
  if (remaining_bytes > size) {
    memcpy(pos, src, size);
    return pos + size;
  } else if (remaining_bytes == size) {
    memcpy(pos, src, size);
    return dest_head;
  } else {
    unsigned int copied_bytes = size - remaining_bytes;
    memcpy(pos, src, remaining_bytes);
    memcpy(dest_head, src + remaining_bytes, copied_bytes);
    return dest_head + copied_bytes;
  }
}


x86 assembly (clang, -O0):
<unwind_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    %rcx,%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jbe    116e <unwind_write+0x5e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11d4 <unwind_write+0xc4>
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1197 <unwind_write+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d4 <unwind_write+0xc4>
mov    -0x1c(%rbp),%eax
sub    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%eax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unwind_write>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
mov    %r8d,%ebx
add    %rcx,%rbx
sub    %rdi,%rbx
cmp    %edx,%ebx
jbe    1145 <unwind_write+0x35>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <memcpy@plt>
mov    %r14d,%r13d
add    %r12,%r13
jmp    117d <unwind_write+0x6d>
mov    %rcx,%r13
jne    115a <unwind_write+0x4a>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <memcpy@plt>
jmp    117d <unwind_write+0x6d>
sub    %ebx,%r14d
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    %ebx,%esi
add    %r15,%rsi
mov    %r13,%rdi
mov    %r14d,%edx
call   1030 <memcpy@plt>
add    %r14,%r13
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @unwind_write(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%14 = load i8*, i8** %10
%15 = load i32, i32* %11
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8*, i8** %7
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = load i32, i32* %9
%25 = icmp ugt i32 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %5
%27 = load i8*, i8** %7
%28 = load i8*, i8** %8
%29 = load i32, i32* %9
%30 = call i32 @memcpy(i8* %27, i8* %28, i32 %29)
%31 = load i8*, i8** %7
%32 = load i32, i32* %9
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8* %34, i8** %6
br label %64
35: ; preds = %5
%36 = load i32, i32* %12
%37 = load i32, i32* %9
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i8*, i8** %7
%41 = load i8*, i8** %8
%42 = load i32, i32* %9
%43 = call i32 @memcpy(i8* %40, i8* %41, i32 %42)
%44 = load i8*, i8** %10
store i8* %44, i8** %6
br label %64
45: ; preds = %35
%46 = load i32, i32* %9
%47 = load i32, i32* %12
%48 = sub i32 %46, %47
store i32 %48, i32* %13
%49 = load i8*, i8** %7
%50 = load i8*, i8** %8
%51 = load i32, i32* %12
%52 = call i32 @memcpy(i8* %49, i8* %50, i32 %51)
%53 = load i8*, i8** %10
%54 = load i8*, i8** %8
%55 = load i32, i32* %12
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i32, i32* %13
%59 = call i32 @memcpy(i8* %53, i8* %57, i32 %58)
%60 = load i8*, i8** %10
%61 = load i32, i32* %13
%62 = zext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8* %63, i8** %6
br label %64
64: ; preds = %45, %39, %26
%65 = load i8*, i8** %6
ret i8* %65
}

LLVM IR (-O2):
define i8* @unwind_write(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = zext i32 %4 to i64
%7 = getelementptr inbounds i8, i8* %3, i64 %6
%8 = ptrtoint i8* %7 to i64
%9 = ptrtoint i8* %0 to i64
%10 = sub i64 %8, %9
%11 = trunc i64 %10 to i32
%12 = icmp ugt i32 %11, %2
br i1 %12, label %13, label %17
13: ; preds = %5
%14 = tail call i32 @memcpy(i8* %0, i8* %1, i32 %2)
%15 = zext i32 %2 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
br label %29
17: ; preds = %5
%18 = icmp eq i32 %11, %2
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 @memcpy(i8* %0, i8* %1, i32 %2)
br label %29
21: ; preds = %17
%22 = sub i32 %2, %11
%23 = tail call i32 @memcpy(i8* %0, i8* %1, i32 %11)
%24 = and i64 %10, 4294967295
%25 = getelementptr inbounds i8, i8* %1, i64 %24
%26 = tail call i32 @memcpy(i8* %3, i8* %25, i32 %22)
%27 = zext i32 %22 to i64
%28 = getelementptr inbounds i8, i8* %3, i64 %27
br label %29
29: ; preds = %21, %19, %13
%30 = phi i8* [ %16, %13 ], [ %3, %19 ], [ %28, %21 ]
ret i8* %30
}