#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 */
char *malloc_or_die(char *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *copy_prefix(const char *s, int n) {
  char *ss;
  if (s == NULL)
    return NULL;
  ss = malloc_or_die("copy_prefix", n + 1);
  memcpy(ss, s, n);
  ss[n] = 0;
  return ss;
}


x86 assembly (clang, -O0):
<copy_prefix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1147 <copy_prefix+0x27>
movq   $0x0,-0x8(%rbp)
jmp    1181 <copy_prefix+0x61>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
lea    0xeac(%rip),%rdi
call   1040 <malloc_or_die@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_prefix>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     115a <copy_prefix+0x3a>
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x1(%r14),%esi
lea    0xec5(%rip),%rdi
call   1040 <malloc_or_die@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <memcpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    115c <copy_prefix+0x3c>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"copy_prefix\00"
define i8* @copy_prefix(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 %23
10: ; preds = %2
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
%13 = call i8* @malloc_or_die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i32 @memcpy(i8* %14, i8* %15, i32 %16)
%18 = load i8*, i8** %6
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
%22 = load i8*, i8** %6
store i8* %22, i8** %3
br label %23
23: ; preds = %10, %9
%24 = load i8*, i8** %3
ret i8* %24
}

LLVM IR (-O2):
@.str = [12 x i8] c"copy_prefix\00"
define i8* @copy_prefix(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = add nsw i32 %1, 1
%6 = tail call i8* @malloc_or_die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %1)
%8 = sext i32 %1 to i64
%9 = getelementptr inbounds i8, i8* %6, i64 %8
store i8 0, i8* %9
br label %10
10: ; preds = %2, %4
%11 = phi i8* [ %6, %4 ], [ null, %2 ]
ret i8* %11
}