#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*/ memcpy(unsigned char *, unsigned char const *,
                          unsigned char);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
unsigned char *mask_prefix(unsigned char *restrict ret,
                           const unsigned char *restrict prefix,
                           unsigned char plen) {
  if (plen >= 128) {
    memcpy(ret, prefix, 16);
    return ret;
  }
  memset(ret, 0, 16);
  memcpy(ret, prefix, plen / 8);
  if (plen % 8 != 0)
    ret[plen / 8] = (prefix[plen / 8] & ((0xFF << (8 - (plen % 8))) & 0xFF));
  return ret;
}


x86 assembly (clang, -O0):
<mask_prefix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
cmp    $0x80,%eax
jl     1163 <mask_prefix+0x43>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121d <mask_prefix+0xfd>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movzbl -0x19(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
movzbl %al,%edx
call   1040 <memcpy@plt>
movzbl -0x19(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1215 <mask_prefix+0xf5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movzbl -0x19(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x30(%rbp)
movzbl -0x19(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x30(%rbp),%eax
mov    $0x8,%ecx
sub    %edx,%ecx
mov    $0xff,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    $0xff,%ecx
and    %ecx,%eax
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movzbl -0x19(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x29(%rbp),%dl
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mask_prefix>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
test   %bpl,%bpl
js     116e <mask_prefix+0x4e>
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    %ebp,%ebx
shr    $0x3,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
and    $0x7,%ebp
je     117b <mask_prefix+0x5b>
mov    $0x8,%cl
sub    %bpl,%cl
mov    $0xff,%eax
shl    %cl,%eax
and    (%r14,%rbx,1),%al
mov    %al,(%r15,%rbx,1)
jmp    117b <mask_prefix+0x5b>
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @mask_prefix(i8* noalias %0, i8* noalias %1, i8 zeroext %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8 %2, i8* %7
%8 = load i8, i8* %7
%9 = zext i8 %8 to i32
%10 = icmp sge i32 %9, 128
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = load i8*, i8** %6
%14 = call i32 @memcpy(i8* %12, i8* %13, i8 zeroext 16)
%15 = load i8*, i8** %5
store i8* %15, i8** %4
br label %55
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 @memset(i8* %17, i32 0, i32 16)
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = load i8, i8* %7
%22 = zext i8 %21 to i32
%23 = sdiv i32 %22, 8
%24 = trunc i32 %23 to i8
%25 = call i32 @memcpy(i8* %19, i8* %20, i8 zeroext %24)
%26 = load i8, i8* %7
%27 = zext i8 %26 to i32
%28 = srem i32 %27, 8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %53
30: ; preds = %16
%31 = load i8*, i8** %6
%32 = load i8, i8* %7
%33 = zext i8 %32 to i32
%34 = sdiv i32 %33, 8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %31, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = load i8, i8* %7
%40 = zext i8 %39 to i32
%41 = srem i32 %40, 8
%42 = sub nsw i32 8, %41
%43 = shl i32 255, %42
%44 = and i32 %43, 255
%45 = and i32 %38, %44
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %5
%48 = load i8, i8* %7
%49 = zext i8 %48 to i32
%50 = sdiv i32 %49, 8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %47, i64 %51
store i8 %46, i8* %52
br label %53
53: ; preds = %30, %16
%54 = load i8*, i8** %5
store i8* %54, i8** %4
br label %55
55: ; preds = %53, %11
%56 = load i8*, i8** %4
ret i8* %56
}

LLVM IR (-O2):
define i8* @mask_prefix(i8* noalias returned %0, i8* noalias %1, i8 zeroext %2) {
%4 = zext i8 %2 to i32
%5 = icmp slt i8 %2, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @memcpy(i8* %0, i8* %1, i8 zeroext 16)
br label %24
8: ; preds = %3
%9 = tail call i32 @memset(i8* %0, i32 0, i32 16)
%10 = lshr i32 %4, 3
%11 = trunc i32 %10 to i8
%12 = tail call i32 @memcpy(i8* %0, i8* %1, i8 zeroext %11)
%13 = and i32 %4, 7
%14 = icmp eq i32 %13, 0
br i1 %14, label %24, label %15
15: ; preds = %8
%16 = zext i32 %10 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = sub nuw nsw i32 8, %13
%20 = shl nuw nsw i32 255, %19
%21 = trunc i32 %20 to i8
%22 = and i8 %18, %21
%23 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 %22, i8* %23
br label %24
24: ; preds = %8, %15, %6
ret i8* %0
}