#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(char *, char const *, size_t);
char *safe_malloc(size_t);
size_t strlen(char const *);
char *mutt_substrdup(const char *begin, const char *end) {
  size_t len;
  char *p;
  if (end)
    len = end - begin;
  else
    len = strlen(begin);
  p = safe_malloc(len + 1);
  memcpy(p, begin, len);
  p[len] = 0;
  return p;
}


x86 assembly (clang, -O0):
<mutt_substrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     115f <mutt_substrdup+0x2f>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    116c <mutt_substrdup+0x3c>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <safe_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutt_substrdup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     1145 <mutt_substrdup+0x15>
mov    %rsi,%rbx
sub    %r14,%rbx
jmp    1150 <mutt_substrdup+0x20>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
lea    0x1(%rbx),%rdi
call   1030 <safe_malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @mutt_substrdup(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = load i8*, i8** %3
%12 = ptrtoint i8* %10 to i64
%13 = ptrtoint i8* %11 to i64
%14 = sub i64 %12, %13
store i64 %14, i64* %5
br label %18
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %5
br label %18
18: ; preds = %15, %9
%19 = load i64, i64* %5
%20 = add i64 %19, 1
%21 = call i8* @safe_malloc(i64 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = load i64, i64* %5
%25 = call i32 @memcpy(i8* %22, i8* %23, i64 %24)
%26 = load i8*, i8** %6
%27 = load i64, i64* %5
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %6
ret i8* %29
}

LLVM IR (-O2):
define i8* @mutt_substrdup(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = ptrtoint i8* %1 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
br label %10
8: ; preds = %2
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
br label %10
10: ; preds = %8, %4
%11 = phi i64 [ %7, %4 ], [ %9, %8 ]
%12 = add i64 %11, 1
%13 = tail call i8* @safe_malloc(i64 %12)
%14 = tail call i32 @memcpy(i8* %13, i8* %0, i64 %11)
%15 = getelementptr inbounds i8, i8* %13, i64 %11
store i8 0, i8* %15
ret i8* %13
}