#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*/ exit(int);
int intrand(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *);
char *patron_text(int potenciade2, char *texto, int txtlen) {
  int tamano;
  int rnd;
  char *result;
  tamano = 2 << (potenciade2 - 1);
  result = (char *)malloc((tamano + 1) * sizeof(char));
  if (result == NULL) {
    printf("no hay memoria..\n");
    exit(-1);
  }
  rnd = intrand(txtlen - tamano);
  memcpy(result, texto + rnd, tamano);
  result[tamano] = 0;
  return result;
}


x86 assembly (clang, -O0):
<patron_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x2,%eax
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11aa <patron_text+0x5a>
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
sub    -0x18(%rbp),%edi
call   1030 <intrand@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<patron_text>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
lea    -0x1(%rdi),%ecx
mov    $0x2,%ebx
shl    %cl,%ebx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11aa <patron_text+0x5a>
mov    %rax,%rbp
sub    %ebx,%r15d
mov    %r15d,%edi
call   1030 <intrand@plt>
cltq
add    %rax,%r14
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4f(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"no hay memoria..\0A\00"
define i8* @patron_text(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
%12 = shl i32 2, %11
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 1
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %3
%23 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %3
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = sub nsw i32 %26, %27
%29 = call i32 @intrand(i32 %28)
store i32 %29, i32* %8
%30 = load i8*, i8** %9
%31 = load i8*, i8** %5
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32, i32* %7
%36 = call i32 @memcpy(i8* %30, i8* %34, i32 %35)
%37 = load i8*, i8** %9
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %9
ret i8* %41
}

LLVM IR (-O2):
@str = [17 x i8] c"no hay memoria..\00"
define i8* @patron_text(i32 %0, i8* %1, i32 %2) {
%4 = add nsw i32 %0, -1
%5 = shl i32 2, %4
%6 = or i32 %5, 1
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %3
%14 = sub nsw i32 %2, %5
%15 = tail call i32 @intrand(i32 %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = tail call i32 @memcpy(i8* %8, i8* %17, i32 %5)
%19 = sext i32 %5 to i64
%20 = getelementptr inbounds i8, i8* %8, i64 %19
store i8 0, i8* %20
ret i8* %8
}