#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*/ buffer_copy(unsigned char *, unsigned char *, unsigned int);
unsigned int buffer_write(void *out, unsigned int ocount, void *in,
                          unsigned int icount, unsigned int offset) {
  unsigned char *op = out;
  unsigned char *ip = in;
  if (offset >= ocount)
    return 0;
  if (icount > ocount - offset)
    icount = ocount - offset;
  buffer_copy(op + offset, ip, icount);
  return icount;
}


x86 assembly (clang, -O0):
<buffer_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1152 <buffer_write+0x42>
movl   $0x0,-0x4(%rbp)
jmp    1188 <buffer_write+0x78>
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x28(%rbp),%ecx
cmp    %ecx,%eax
jbe    116c <buffer_write+0x5c>
mov    -0x14(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%eax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <buffer_copy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_write>:
xor    %eax,%eax
sub    %r8d,%esi
jbe    1132 <buffer_write+0x22>
push   %rbx
mov    %ecx,%ebx
cmp    %ecx,%esi
cmovb  %esi,%ebx
mov    %r8d,%eax
add    %rax,%rdi
mov    %rdx,%rsi
mov    %ebx,%edx
call   1030 <buffer_copy@plt>
mov    %ebx,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @buffer_write(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = icmp ugt i32 %1, %4
br i1 %6, label %7, label %14
7: ; preds = %5
%8 = sub i32 %1, %4
%9 = icmp ult i32 %8, %3
%10 = select i1 %9, i32 %8, i32 %3
%11 = zext i32 %4 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 @buffer_copy(i8* %12, i8* %2, i32 %10)
br label %14
14: ; preds = %5, %7
%15 = phi i32 [ %10, %7 ], [ 0, %5 ]
ret i32 %15
}