#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 */
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int str_slice(char *dest, char *src, int len) {
  char *psrc;
  if (!dest || !src || !*src) {
    return -1;
  }
  memset(dest, 0, (len + 1));
  strncpy(dest, src, len);
  psrc = src + strlen(dest);
  memmove(src, psrc, (strlen(psrc) + 1));
  return 0;
}


x86 assembly (clang, -O0):
<str_slice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1176 <str_slice+0x36>
cmpq   $0x0,-0x18(%rbp)
je     1176 <str_slice+0x36>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1182 <str_slice+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <str_slice+0xb6>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memmove@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_slice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11a5 <str_slice+0x65>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11a5 <str_slice+0x65>
cmpb   $0x0,(%rbx)
je     11a5 <str_slice+0x65>
mov    %edx,%r15d
mov    %rdi,%rbp
lea    0x1(%r15),%edx
xor    %r14d,%r14d
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <memmove@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @str_slice(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %18
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i8*, i8** %6
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %14, %11, %3
store i32 -1, i32* %4
br label %39
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
%23 = call i32 @memset(i8* %20, i32 0, i32 %22)
%24 = load i8*, i8** %5
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @strncpy(i8* %24, i8* %25, i32 %26)
%28 = load i8*, i8** %6
%29 = load i8*, i8** %5
%30 = call i32 @strlen(i8* %29)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8* %32, i8** %8
%33 = load i8*, i8** %6
%34 = load i8*, i8** %8
%35 = load i8*, i8** %8
%36 = call i32 @strlen(i8* %35)
%37 = add nsw i32 %36, 1
%38 = call i32 @memmove(i8* %33, i8* %34, i32 %37)
store i32 0, i32* %4
br label %39
39: ; preds = %19, %18
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
define i32 @str_slice(i8* %0, i8* %1, i32 %2) {
%4 = icmp ne i8* %0, null
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
br i1 %6, label %7, label %20
7: ; preds = %3
%8 = load i8, i8* %1
%9 = icmp eq i8 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %7
%11 = add nsw i32 %2, 1
%12 = tail call i32 @memset(i8* %0, i32 0, i32 %11)
%13 = tail call i32 @strncpy(i8* %0, i8* %1, i32 %2)
%14 = tail call i32 @strlen(i8* %0)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = tail call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 1
%19 = tail call i32 @memmove(i8* %1, i8* %16, i32 %18)
br label %20
20: ; preds = %3, %7, %10
%21 = phi i32 [ 0, %10 ], [ -1, %7 ], [ -1, %3 ]
ret i32 %21
}