#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 */
typedef int /*<<< orphan*/ __compar_fn_t;

/* Variables and functions */
void *lfind(void const *, void *, size_t *, size_t, int /*<<< orphan*/);
void *memcpy(void *, void const *, size_t);
void *lsearch(const void *key, void *base, size_t *nmemb, size_t size,
              __compar_fn_t compar) {
  void *result;
  result = lfind(key, base, nmemb, size, compar);
  if (result == NULL) {
    result = memcpy(base + (*nmemb) * size, key, size);
    ++(*nmemb);
  }
  return result;
}


x86 assembly (clang, -O0):
<lsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
call   1030 <lfind@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119e <lsearch+0x7e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
imul   -0x20(%rbp),%rax
add    %rax,%rdi
mov    %rdi,-0x38(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsearch>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
call   1030 <lfind@plt>
test   %rax,%rax
jne    115d <lsearch+0x3d>
mov    (%r14),%rax
imul   %r15,%rax
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
addq   $0x1,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @lsearch(i8* %0, i8* %1, i64* %2, i64 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64* %2, i64** %8
store i64 %3, i64* %9
store i32 %4, i32* %10
%12 = load i8*, i8** %6
%13 = load i8*, i8** %7
%14 = load i64*, i64** %8
%15 = load i64, i64* %9
%16 = load i32, i32* %10
%17 = call i8* @lfind(i8* %12, i8* %13, i64* %14, i64 %15, i32 %16)
store i8* %17, i8** %11
%18 = load i8*, i8** %11
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %32
20: ; preds = %5
%21 = load i8*, i8** %7
%22 = load i64*, i64** %8
%23 = load i64, i64* %22
%24 = load i64, i64* %9
%25 = mul i64 %23, %24
%26 = getelementptr i8, i8* %21, i64 %25
%27 = load i8*, i8** %6
%28 = load i64, i64* %9
store i8* %26, i8** %11
%29 = load i64*, i64** %8
%30 = load i64, i64* %29
%31 = add i64 %30, 1
store i64 %31, i64* %29
br label %32
32: ; preds = %20, %5
%33 = load i8*, i8** %11
ret i8* %33
}

LLVM IR (-O2):
define i8* @lsearch(i8* %0, i8* %1, i64* %2, i64 %3, i32 %4) {
%6 = tail call i8* @lfind(i8* %0, i8* %1, i64* %2, i64 %3, i32 %4)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = load i64, i64* %2
%10 = mul i64 %9, %3
%11 = getelementptr i8, i8* %1, i64 %10
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %11, i8* align 1 %0, i64 %3, i1 false)
%12 = load i64, i64* %2
%13 = add i64 %12, 1
store i64 %13, i64* %2
br label %14
14: ; preds = %8, %5
%15 = phi i8* [ %11, %8 ], [ %6, %5 ]
ret i8* %15
}