#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__ myalloc(scalar_t__);
int /*<<< orphan*/ problem(char *);
char *strcpy(char *, char *);
scalar_t__ strlen(char *);
char *dupstr(char *str) {
  char *p;
  if (str == NULL)
    return NULL;
  if ((p = (char *)myalloc(strlen(str) + 1)) == NULL)
    problem("dynamic memory exhausted");
  return strcpy(p, str);
}


x86 assembly (clang, -O0):
<dupstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <dupstr+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11a4 <dupstr+0x64>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1040 <myalloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1193 <dupstr+0x53>
lea    0xe72(%rip),%rdi
call   1060 <problem@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dupstr>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     117b <dupstr+0x3b>
mov    %rdi,%r14
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1040 <myalloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116e <dupstr+0x2e>
lea    0xe97(%rip),%rdi
call   1060 <problem@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    117d <dupstr+0x3d>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [25 x i8] c"dynamic memory exhausted\00"
define i8* @dupstr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
store i8* null, i8** %2
br label %21
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i64 @strlen(i8* %9)
%11 = add nsw i64 %10, 1
%12 = call i64 @myalloc(i64 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = call i32 @problem(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %8
%18 = load i8*, i8** %4
%19 = load i8*, i8** %3
%20 = call i8* @strcpy(i8* %18, i8* %19)
store i8* %20, i8** %2
br label %21
21: ; preds = %17, %7
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
@.str = [25 x i8] c"dynamic memory exhausted\00"
define i8* @dupstr(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = add nsw i64 %4, 1
%6 = tail call i64 @myalloc(i64 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @problem(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = tail call i8* @strcpy(i8* dereferenceable(1) %7, i8* %0)
br label %13
13: ; preds = %1, %11
%14 = phi i8* [ %7, %11 ], [ null, %1 ]
ret i8* %14
}