#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 iw_event {
  int len;
};

/* Variables and functions */
int IW_EV_LCP_LEN;
int /*<<< orphan*/ memcpy(char *, char *, int);
char *iwe_stream_add_value(char *event, char *value, char *ends,
                           struct iw_event *iwe, int event_len) {
  event_len -= IW_EV_LCP_LEN;
  if ((value + event_len) < ends) {
    memcpy(value, (char *)iwe + IW_EV_LCP_LEN, event_len);
    value += event_len;
    iwe->len = value - event;
    memcpy(event, (char *)iwe, IW_EV_LCP_LEN);
  }
  return value;
}


x86 assembly (clang, -O0):
<iwe_stream_add_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    0x2ec5(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jae    11a9 <iwe_stream_add_value+0x99>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e97(%rip),%rax
movslq (%rax),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
call   1030 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iwe_stream_add_value>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rcx
mov    0x2ed3(%rip),%r12
movslq (%r12),%rax
movslq %r8d,%rdx
sub    %rax,%rdx
lea    (%rsi,%rdx,1),%rbx
cmp    %rcx,%rbx
jae    1162 <iwe_stream_add_value+0x52>
mov    %rdi,%r15
add    %r14,%rax
mov    %rsi,%rdi
mov    %rax,%rsi
call   1030 <memcpy@plt>
mov    %ebx,%eax
sub    %r15d,%eax
mov    %eax,(%r14)
mov    (%r12),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <memcpy@plt>
jmp    1165 <iwe_stream_add_value+0x55>
mov    %rsi,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.iw_event = type { i32 }
@IW_EV_LCP_LEN = global i32 0
define i8* @iwe_stream_add_value(i8* %0, i8* %1, i8* %2, %struct.iw_event* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.iw_event*
%10 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store %struct.iw_event* %3, %struct.iw_event** %9
store i32 %4, i32* %10
%11 = load i32, i32* @IW_EV_LCP_LEN
%12 = load i32, i32* %10
%13 = sub nsw i32 %12, %11
store i32 %13, i32* %10
%14 = load i8*, i8** %7
%15 = load i32, i32* %10
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8*, i8** %8
%19 = icmp ult i8* %17, %18
br i1 %19, label %20, label %46
20: ; preds = %5
%21 = load i8*, i8** %7
%22 = load %struct.iw_event*, %struct.iw_event** %9
%23 = bitcast %struct.iw_event* %22 to i8*
%24 = load i32, i32* @IW_EV_LCP_LEN
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i32, i32* %10
%28 = call i32 @memcpy(i8* %21, i8* %26, i32 %27)
%29 = load i32, i32* %10
%30 = load i8*, i8** %7
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = load i8*, i8** %6
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = trunc i64 %37 to i32
%39 = load %struct.iw_event*, %struct.iw_event** %9
%40 = getelementptr inbounds %struct.iw_event, %struct.iw_event* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i8*, i8** %6
%42 = load %struct.iw_event*, %struct.iw_event** %9
%43 = bitcast %struct.iw_event* %42 to i8*
%44 = load i32, i32* @IW_EV_LCP_LEN
%45 = call i32 @memcpy(i8* %41, i8* %43, i32 %44)
br label %46
46: ; preds = %20, %5
%47 = load i8*, i8** %7
ret i8* %47
}

LLVM IR (-O2):
%struct.iw_event = type { i32 }
@IW_EV_LCP_LEN = global i32 0
define i8* @iwe_stream_add_value(i8* %0, i8* %1, i8* readnone %2, %struct.iw_event* %3, i32 %4) {
%6 = load i32, i32* @IW_EV_LCP_LEN
%7 = sub nsw i32 %4, %6
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = icmp ult i8* %9, %2
br i1 %10, label %11, label %23
11: ; preds = %5
%12 = bitcast %struct.iw_event* %3 to i8*
%13 = sext i32 %6 to i64
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = tail call i32 @memcpy(i8* %1, i8* %14, i32 %7)
%16 = ptrtoint i8* %9 to i64
%17 = ptrtoint i8* %0 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = getelementptr inbounds %struct.iw_event, %struct.iw_event* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @IW_EV_LCP_LEN
%22 = tail call i32 @memcpy(i8* %0, i8* %12, i32 %21)
br label %23
23: ; preds = %11, %5
%24 = phi i8* [ %9, %11 ], [ %1, %5 ]
ret i8* %24
}