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

/* Variables and functions */
char *malloc(scalar_t__);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strdup(char const *string) {
  char *pstr;
  puts("So atm the location says:");
  puts(pstr);
  printf("And it has %lu bytes\n", sizeof(pstr));
  pstr = malloc(strlen(string) + 1);
  if (pstr != NULL)
    strcpy(pstr, string);
  printf("Now it has %lu bytes\n", sizeof(pstr));
  return pstr;
}


x86 assembly (clang, -O0):
<strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1040 <puts@plt>
lea    0xea2(%rip),%rdi
mov    $0x8,%esi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b3 <strdup+0x63>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    0xe76(%rip),%rdi
mov    $0x8,%esi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xea2(%rip),%rdi
call   1040 <puts@plt>
call   1040 <puts@plt>
lea    0xeab(%rip),%rdi
mov    $0x8,%esi
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     119d <strdup+0x4d>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    0xe8c(%rip),%rdi
mov    $0x8,%esi
call   1060 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [26 x i8] c"So atm the location says:\00"
@.str.1 = [22 x i8] c"And it has %lu bytes\0A\00"
@.str.2 = [22 x i8] c"Now it has %lu bytes\0A\00"
define i8* @strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%5 = load i8*, i8** %3
%6 = call i32 @puts(i8* %5)
%7 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 8)
%8 = load i8*, i8** %2
%9 = call i64 @strlen(i8* %8)
%10 = add nsw i64 %9, 1
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i8*, i8** %2
%17 = call i32 @strcpy(i8* %15, i8* %16)
br label %18
18: ; preds = %14, %1
%19 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 8)
%20 = load i8*, i8** %3
ret i8* %20
}

LLVM IR (-O2):
@.str = [26 x i8] c"So atm the location says:\00"
@.str.1 = [22 x i8] c"And it has %lu bytes\0A\00"
@.str.2 = [22 x i8] c"Now it has %lu bytes\0A\00"
define i8* @strdup(i8* %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) undef)
%4 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 8)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add nsw i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = tail call i32 @strcpy(i8* %7, i8* %0)
br label %11
11: ; preds = %9, %1
%12 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 8)
ret i8* %7
}