#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*/ utf8;

/* Variables and functions */
unsigned char *malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, char const *, int const);
unsigned char *load_test_buf(int len) {
  const char utf8[] = "\xF0\x90\xBF\x80";
  const int utf8_len = sizeof(utf8) / sizeof(utf8[0]) - 1;
  unsigned char *data = malloc(len);
  unsigned char *p = data;
  while (len >= utf8_len) {
    memcpy(p, utf8, utf8_len);
    p += utf8_len;
    len -= utf8_len;
  }
  while (len--)
    *p++ = 0x7F;
  return data;
}


x86 assembly (clang, -O0):
<load_test_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0xecf(%rip),%eax
mov    %eax,-0x9(%rbp)
mov    0xeca(%rip),%al
mov    %al,-0x5(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x4,-0x4(%rbp)
jl     118e <load_test_buf+0x6e>
mov    -0x20(%rbp),%rdi
lea    -0x9(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x4(%rbp)
jmp    1158 <load_test_buf+0x38>
jmp    1193 <load_test_buf+0x73>
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
je     11be <load_test_buf+0x9e>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x7f,(%rax)
jmp    1193 <load_test_buf+0x73>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_test_buf>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
movb   $0x0,0x4(%rsp)
movl   $0x80bf90f0,(%rsp)
call   1050 <malloc@plt>
mov    %rax,%r14
cmp    $0x4,%r15d
jl     11a3 <load_test_buf+0x73>
mov    %rsp,%r12
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
add    $0x4,%rbx
lea    -0x4(%r15),%eax
cmp    $0x7,%r15d
mov    %eax,%r15d
ja     1160 <load_test_buf+0x30>
test   %eax,%eax
je     1194 <load_test_buf+0x64>
mov    %eax,%edx
mov    %rbx,%rdi
mov    $0x7f,%esi
call   1030 <memset@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r15d,%eax
mov    %r14,%rbx
test   %eax,%eax
jne    1185 <load_test_buf+0x55>
jmp    1194 <load_test_buf+0x64>

LLVM IR (-O0):
@__const.load_test_buf.utf8 = [5 x i8] c"\F0\90\BF\80\00"
define i8* @load_test_buf(i32 %0) {
%2 = alloca i32
%3 = alloca [5 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 %0, i32* %2
%7 = bitcast [5 x i8]* %3 to i8*
store i32 4, i32* %4
%8 = load i32, i32* %2
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
store i8* %10, i8** %6
br label %11
11: ; preds = %14, %1
%12 = load i32, i32* %2
%13 = icmp sge i32 %12, 4
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i8*, i8** %6
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%17 = call i32 @memcpy(i8* %15, i8* %16, i32 4)
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 4
store i8* %19, i8** %6
%20 = load i32, i32* %2
%21 = sub nsw i32 %20, 4
store i32 %21, i32* %2
br label %11
22: ; preds = %11
br label %23
23: ; preds = %27, %22
%24 = load i32, i32* %2
%25 = add nsw i32 %24, -1
store i32 %25, i32* %2
%26 = icmp ne i32 %24, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %6
store i8 127, i8* %28
br label %23
30: ; preds = %23
%31 = load i8*, i8** %5
ret i8* %31
}

LLVM IR (-O2):
@__const.load_test_buf.utf8 = [5 x i8] c"\F0\90\BF\80\00"
define i8* @load_test_buf(i32 %0) {
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @malloc(i32 %0)
%5 = icmp sgt i32 %0, 3
br i1 %5, label %12, label %6
6: ; preds = %12, %1
%7 = phi i32 [ %0, %1 ], [ %17, %12 ]
%8 = phi i8* [ %4, %1 ], [ %16, %12 ]
%9 = icmp eq i32 %7, 0
br i1 %9, label %19, label %10
10: ; preds = %6
%11 = zext i32 %7 to i64
br label %19
12: ; preds = %1, %12
%13 = phi i8* [ %16, %12 ], [ %4, %1 ]
%14 = phi i32 [ %17, %12 ], [ %0, %1 ]
%15 = call i32 @memcpy(i8* %13, i8* %3, i32 4)
%16 = getelementptr inbounds i8, i8* %13, i64 4
%17 = add nsw i32 %14, -4
%18 = icmp ugt i32 %14, 7
br i1 %18, label %12, label %6
19: ; preds = %10, %6
ret i8* %4
}