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

/* Variables and functions */
int /*<<< orphan*/ _mm_loadu_si128(int /*<<< orphan*/ *);
int /*<<< orphan*/ _mm_setzero_si128();
__m128i load_n_bytes(const uint8_t *p, const size_t num_bytes) {
  if (num_bytes == 0) {
    return _mm_setzero_si128();
  } else if (num_bytes % 16 == 0) {
    return _mm_loadu_si128((__m128i *)p);
  } else {
    __m128i tmp;
    unsigned i;
    for (i = 0; i < num_bytes; ++i) {
      ((char *)&tmp)[i] = ((char *)p)[i];
    }
    return tmp;
  }
}


x86 assembly (clang, -O0):
<load_n_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    114a <load_n_bytes+0x2a>
mov    $0x0,%al
call   1030 <_mm_setzero_si128@plt>
mov    %eax,-0x4(%rbp)
jmp    11a6 <load_n_bytes+0x86>
mov    -0x18(%rbp),%rax
and    $0xf,%rax
cmp    $0x0,%rax
jne    116d <load_n_bytes+0x4d>
mov    -0x10(%rbp),%rdi
call   1040 <_mm_loadu_si128@plt>
mov    %eax,-0x4(%rbp)
jmp    11a6 <load_n_bytes+0x86>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    11a0 <load_n_bytes+0x80>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%eax
mov    %cl,-0x1c(%rbp,%rax,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1174 <load_n_bytes+0x54>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_n_bytes>:
test   %rsi,%rsi
je     1145 <load_n_bytes+0x25>
test   $0xf,%sil
je     114c <load_n_bytes+0x2c>
xor    %eax,%eax
nopl   (%rax)
movzbl (%rdi,%rax,1),%ecx
mov    %cl,-0x4(%rsp,%rax,1)
add    $0x1,%eax
cmp    %rsi,%rax
jb     1130 <load_n_bytes+0x10>
mov    -0x4(%rsp),%eax
ret
xor    %eax,%eax
jmp    1030 <_mm_setzero_si128@plt>
jmp    1040 <_mm_loadu_si128@plt>

LLVM IR (-O0):
define i32 @load_n_bytes(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (...) @_mm_setzero_si128()
store i32 %11, i32* %3
br label %41
12: ; preds = %2
%13 = load i64, i64* %5
%14 = urem i64 %13, 16
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32*, i32** %4
%18 = call i32 @_mm_loadu_si128(i32* %17)
store i32 %18, i32* %3
br label %41
19: ; preds = %12
store i32 0, i32* %7
br label %20
20: ; preds = %36, %19
%21 = load i32, i32* %7
%22 = zext i32 %21 to i64
%23 = load i64, i64* %5
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %39
25: ; preds = %20
%26 = load i32*, i32** %4
%27 = bitcast i32* %26 to i8*
%28 = load i32, i32* %7
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = bitcast i32* %6 to i8*
%33 = load i32, i32* %7
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
br label %36
36: ; preds = %25
%37 = load i32, i32* %7
%38 = add i32 %37, 1
store i32 %38, i32* %7
br label %20
39: ; preds = %20
%40 = load i32, i32* %6
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %16, %10
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
define i32 @load_n_bytes(i32* %0, i64 %1) {
%3 = alloca i32
%4 = icmp eq i64 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @_mm_setzero_si128()
br label %25
7: ; preds = %2
%8 = and i64 %1, 15
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @_mm_loadu_si128(i32* %0)
br label %25
12: ; preds = %7
%13 = bitcast i32* %3 to i8*
%14 = bitcast i32* %0 to i8*
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ 0, %12 ], [ %21, %15 ]
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = getelementptr inbounds i8, i8* %13, i64 %16
store i8 %18, i8* %19
%20 = add nuw nsw i64 %16, 1
%21 = and i64 %20, 4294967295
%22 = icmp ult i64 %21, %1
br i1 %22, label %15, label %23
23: ; preds = %15
%24 = load i32, i32* %3
br label %25
25: ; preds = %23, %10, %5
%26 = phi i32 [ %6, %5 ], [ %11, %10 ], [ %24, %23 ]
ret i32 %26
}