#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(int);
int /*<<< orphan*/ problem(char *);
char *strncpy(char *, char *, int);
char *dupstrn(char *str, int len) {
  char *p;
  if (str == NULL)
    return NULL;
  if ((p = (char *)myalloc(len + 1)) == NULL)
    problem("dynamic memory exhausted");
  *(p + len) = '\0';
  return strncpy(p, str, len);
}


x86 assembly (clang, -O0):
<dupstrn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1157 <dupstrn+0x27>
movq   $0x0,-0x8(%rbp)
jmp    119c <dupstrn+0x6c>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1040 <myalloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    117c <dupstrn+0x4c>
lea    0xe89(%rip),%rdi
call   1050 <problem@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dupstrn>:
test   %rdi,%rdi
je     1177 <dupstrn+0x47>
push   %r15
push   %r14
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
lea    0x1(%r15),%edi
call   1040 <myalloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    115d <dupstrn+0x2d>
lea    0xea8(%rip),%rdi
call   1050 <problem@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <strncpy@plt>
xor    %eax,%eax
ret

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

LLVM IR (-O2):
@.str = [25 x i8] c"dynamic memory exhausted\00"
define i8* @dupstrn(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %15, label %4
4: ; preds = %2
%5 = add nsw i32 %1, 1
%6 = tail call i64 @myalloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%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, %4
%12 = sext i32 %1 to i64
%13 = getelementptr inbounds i8, i8* %7, i64 %12
store i8 0, i8* %13
%14 = tail call i8* @strncpy(i8* %7, i8* %0, i32 %1)
br label %15
15: ; preds = %2, %11
%16 = phi i8* [ %14, %11 ], [ null, %2 ]
ret i8* %16
}