#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 */
scalar_t__ memcpy(void *, char const *, size_t);
int strlen(char const *);
void *trackmalloc(size_t, char const *, char const *, int);
extern char *trackstrdup(const char *src, const char *expr, const char *file,
                         int line) {
  size_t len = strlen(src) + 1;
  void *copy = trackmalloc(len, expr, file, line);
  if (copy == NULL)
    return NULL;
  return (char *)memcpy(copy, src, len);
}


x86 assembly (clang, -O0):
<trackstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1050 <trackmalloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1189 <trackstrdup+0x59>
movq   $0x0,-0x8(%rbp)
jmp    119e <trackstrdup+0x6e>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trackstrdup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%r14
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1050 <trackmalloc@plt>
test   %rax,%rax
je     1175 <trackstrdup+0x45>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    1177 <trackstrdup+0x47>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @trackstrdup(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = tail call i8* @trackmalloc(i64 %7, i8* %1, i8* %2, i32 %3)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = tail call i64 @memcpy(i8* %8, i8* %0, i64 %7)
%12 = inttoptr i64 %11 to i8*
br label %13
13: ; preds = %4, %10
%14 = phi i8* [ %12, %10 ], [ null, %4 ]
ret i8* %14
}