#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_request_info {
  int dummy;
};
struct iw_event {
  int len;
  char u;
};

/* Variables and functions */
int IW_EV_LCP_PK_LEN;
int iwe_stream_event_len_adjust(struct iw_request_info *, int);
int iwe_stream_lcp_len(struct iw_request_info *);
scalar_t__ likely(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *iwe_stream_add_event(struct iw_request_info *info, char *stream,
                           char *ends, struct iw_event *iwe, int event_len) {
  int lcp_len = iwe_stream_lcp_len(info);
  event_len = iwe_stream_event_len_adjust(info, event_len);
  if (likely((stream + event_len) < ends)) {
    iwe->len = event_len;
    memcpy(stream, (char *)iwe, IW_EV_LCP_PK_LEN);
    memcpy(stream + lcp_len, &iwe->u, event_len - lcp_len);
    stream += event_len;
  }
  return stream;
}


x86 assembly (clang, -O0):
<iwe_stream_add_event>:
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    -0x8(%rbp),%rdi
call   1050 <iwe_stream_lcp_len@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <iwe_stream_event_len_adjust@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <likely@plt>
cmp    $0x0,%rax
je     11eb <iwe_stream_add_event+0xab>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x24(%rbp),%edx
sub    -0x28(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iwe_stream_add_event>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebx
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%rbp
call   1050 <iwe_stream_lcp_len@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <iwe_stream_event_len_adjust@plt>
mov    %eax,%ebp
movslq %eax,%rbx
add    %r15,%rbx
xor    %edi,%edi
cmp    %r13,%rbx
setb   %dil
call   1060 <likely@plt>
test   %rax,%rax
je     11b9 <iwe_stream_add_event+0x79>
mov    %ebp,(%r14)
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
movslq %r12d,%rax
add    %rax,%r15
add    $0x4,%r14
sub    %eax,%ebp
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
jmp    11bc <iwe_stream_add_event+0x7c>
mov    %r15,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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