#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*/ assert(int);
char *char_buffer_malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
long strtoul(char const *, char **, int);
scalar_t__ u8_memchr(char *, char, size_t, int *);
char *expand_url(size_t base_len, const char *base_url, size_t stub_len,
                 const char *stub_url) {
  int charn;
  if (u8_memchr((char *)stub_url, '|',
                (((stub_len) < (20)) ? (stub_len) : (20)), &charn)) {
    char *_end;
    long int parsed_number = strtoul(stub_url, &_end, 10);
    assert(*_end == '|');
    if (parsed_number <= base_len) {
      size_t expanded_len = parsed_number + stub_len - (charn + 1);
      char *expanded_url = char_buffer_malloc(expanded_len);
      memcpy(expanded_url, base_url, parsed_number);
      memcpy(expanded_url + parsed_number, stub_url + (charn + 1),
             stub_len - (charn + 1));
      return expanded_url;
    }
  }
  char *expanded_url = char_buffer_malloc(stub_len);
  memcpy(expanded_url, stub_url, stub_len);
  return expanded_url;
}


x86 assembly (clang, -O0):
<expand_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x14,-0x20(%rbp)
jae    1188 <expand_url+0x38>
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    1196 <expand_url+0x46>
mov    $0x14,%eax
mov    %rax,-0x68(%rbp)
jmp    1196 <expand_url+0x46>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rdx
mov    $0x7c,%esi
lea    -0x2c(%rbp),%rcx
call   1030 <u8_memchr@plt>
cmp    $0x0,%rax
je     1264 <expand_url+0x114>
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7c,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x40(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     125f <expand_url+0x10f>
mov    -0x40(%rbp),%rax
add    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1070 <char_buffer_malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rdi
add    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cltq
sub    %rax,%rdx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    128a <expand_url+0x13a>
jmp    1264 <expand_url+0x114>
mov    -0x20(%rbp),%rdi
call   1070 <char_buffer_malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_url>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r12
cmp    $0x14,%rdx
mov    $0x14,%edx
cmovb  %rbp,%rdx
lea    0xc(%rsp),%rcx
mov    %r14,%rdi
mov    $0x7c,%esi
call   1030 <u8_memchr@plt>
test   %rax,%rax
je     11bb <expand_url+0x6b>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %rax,%r13
mov    0x10(%rsp),%rax
xor    %edi,%edi
cmpb   $0x7c,(%rax)
sete   %dil
call   1040 <assert@plt>
cmp    %r12,%r13
jbe    11ce <expand_url+0x7e>
mov    %rbp,%rdi
call   1070 <char_buffer_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
jmp    120c <expand_url+0xbc>
movslq 0xc(%rsp),%rdi
not    %rdi
add    %r13,%rdi
add    %rbp,%rdi
call   1070 <char_buffer_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
add    %r13,%rdi
movslq 0xc(%rsp),%rax
lea    (%r14,%rax,1),%rsi
add    $0x1,%rsi
add    $0x1,%rax
sub    %rax,%rbp
mov    %rbp,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @expand_url(i64 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i8*
store i64 %0, i64* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%16 = load i8*, i8** %9
%17 = load i64, i64* %8
%18 = icmp ult i64 %17, 20
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = load i64, i64* %8
br label %22
21: ; preds = %4
br label %22
22: ; preds = %21, %19
%23 = phi i64 [ %20, %19 ], [ 20, %21 ]
%24 = call i64 @u8_memchr(i8* %16, i8 signext 124, i64 %23, i32* %10)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %68
26: ; preds = %22
%27 = load i8*, i8** %9
%28 = call i64 @strtoul(i8* %27, i8** %11, i32 10)
store i64 %28, i64* %12
%29 = load i8*, i8** %11
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 124
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i64, i64* %12
%36 = load i64, i64* %6
%37 = icmp ule i64 %35, %36
br i1 %37, label %38, label %67
38: ; preds = %26
%39 = load i64, i64* %12
%40 = load i64, i64* %8
%41 = add i64 %39, %40
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = sub i64 %41, %44
store i64 %45, i64* %13
%46 = load i64, i64* %13
%47 = call i8* @char_buffer_malloc(i64 %46)
store i8* %47, i8** %14
%48 = load i8*, i8** %14
%49 = load i8*, i8** %7
%50 = load i64, i64* %12
%51 = call i32 @memcpy(i8* %48, i8* %49, i64 %50)
%52 = load i8*, i8** %14
%53 = load i64, i64* %12
%54 = getelementptr inbounds i8, i8* %52, i64 %53
%55 = load i8*, i8** %9
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
%60 = load i64, i64* %8
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = sub i64 %60, %63
%65 = call i32 @memcpy(i8* %54, i8* %59, i64 %64)
%66 = load i8*, i8** %14
store i8* %66, i8** %5
br label %76
67: ; preds = %26
br label %68
68: ; preds = %67, %22
%69 = load i64, i64* %8
%70 = call i8* @char_buffer_malloc(i64 %69)
store i8* %70, i8** %15
%71 = load i8*, i8** %15
%72 = load i8*, i8** %9
%73 = load i64, i64* %8
%74 = call i32 @memcpy(i8* %71, i8* %72, i64 %73)
%75 = load i8*, i8** %15
store i8* %75, i8** %5
br label %76
76: ; preds = %68, %38
%77 = load i8*, i8** %5
ret i8* %77
}

LLVM IR (-O2):
define i8* @expand_url(i64 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = bitcast i32* %5 to i8*
%8 = icmp ult i64 %2, 20
%9 = select i1 %8, i64 %2, i64 20
%10 = call i64 @u8_memchr(i8* %3, i8 signext 124, i64 %9, i32* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %37, label %12
12: ; preds = %4
%13 = bitcast i8** %6 to i8*
%14 = call i64 @strtoul(i8* %3, i8** %6, i32 10)
%15 = load i8*, i8** %6
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 124
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = icmp ugt i64 %14, %0
br i1 %20, label %21, label %22
21: ; preds = %12
br label %37
22: ; preds = %12
%23 = add i64 %14, %2
%24 = load i32, i32* %5
%25 = xor i32 %24, -1
%26 = sext i32 %25 to i64
%27 = add i64 %23, %26
%28 = call i8* @char_buffer_malloc(i64 %27)
%29 = call i32 @memcpy(i8* %28, i8* %1, i64 %14)
%30 = getelementptr inbounds i8, i8* %28, i64 %14
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %3, i64 %33
%35 = sub i64 %2, %33
%36 = call i32 @memcpy(i8* %30, i8* %34, i64 %35)
br label %40
37: ; preds = %21, %4
%38 = call i8* @char_buffer_malloc(i64 %2)
%39 = call i32 @memcpy(i8* %38, i8* %3, i64 %2)
br label %40
40: ; preds = %22, %37
%41 = phi i8* [ %38, %37 ], [ %28, %22 ]
ret i8* %41
}