#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 */
struct con_ring_buffer {
  int dummy;
};

/* Variables and functions */
unsigned int con_ring_free_sz(struct con_ring_buffer *);
int /*<<< orphan*/ ring_data_cpy(struct con_ring_buffer *,
                                 unsigned char const *, unsigned int);
unsigned int con_ring_append(struct con_ring_buffer *buf,
                             const unsigned char *inbuf, unsigned long size) {
  unsigned int free_sz = 0;
  free_sz = con_ring_free_sz(buf);
  free_sz = free_sz <= size ? free_sz : (unsigned int)size;
  ring_data_cpy(buf, inbuf, free_sz);
  return free_sz;
}


x86 assembly (clang, -O0):
<con_ring_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <con_ring_free_sz@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%rax
ja     115f <con_ring_append+0x3f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1166 <con_ring_append+0x46>
mov    -0x18(%rbp),%rax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <ring_data_cpy@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<con_ring_append>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <con_ring_free_sz@plt>
mov    %eax,%ebp
mov    %eax,%eax
cmp    %rbx,%rax
cmova  %ebx,%ebp
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <ring_data_cpy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.con_ring_buffer = type { i32 }
define i32 @con_ring_append(%struct.con_ring_buffer* %0, i8* %1, i64 %2) {
%4 = alloca %struct.con_ring_buffer*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store %struct.con_ring_buffer* %0, %struct.con_ring_buffer** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %7
%8 = load %struct.con_ring_buffer*, %struct.con_ring_buffer** %4
%9 = call i32 @con_ring_free_sz(%struct.con_ring_buffer* %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = zext i32 %10 to i64
%12 = load i64, i64* %6
%13 = icmp ule i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* %7
br label %19
16: ; preds = %3
%17 = load i64, i64* %6
%18 = trunc i64 %17 to i32
br label %19
19: ; preds = %16, %14
%20 = phi i32 [ %15, %14 ], [ %18, %16 ]
store i32 %20, i32* %7
%21 = load %struct.con_ring_buffer*, %struct.con_ring_buffer** %4
%22 = load i8*, i8** %5
%23 = load i32, i32* %7
%24 = call i32 @ring_data_cpy(%struct.con_ring_buffer* %21, i8* %22, i32 %23)
%25 = load i32, i32* %7
ret i32 %25
}

LLVM IR (-O2):
%struct.con_ring_buffer = type { i32 }
define i32 @con_ring_append(%struct.con_ring_buffer* %0, i8* %1, i64 %2) {
%4 = tail call i32 @con_ring_free_sz(%struct.con_ring_buffer* %0)
%5 = zext i32 %4 to i64
%6 = icmp ugt i64 %5, %2
%7 = trunc i64 %2 to i32
%8 = select i1 %6, i32 %7, i32 %4
%9 = tail call i32 @ring_data_cpy(%struct.con_ring_buffer* %0, i8* %1, i32 %8)
ret i32 %8
}