#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int strlen(char const *);
void str_copy_part(const char *str0, char **str1, int m) {
  int len = strlen(str0);
  int i;
  if (m > len) {
    fprintf(stderr, "m is larger than str0\n");
    exit(1);
  }
  *str1 = (char *)malloc(len - m + 2);
  if (str1 == NULL) {
    fprintf(stderr, "malloc error\n");
    exit(1);
  }
  for (i = m - 1; str0[i] != '\0'; i++)
    (*str1)[i - m + 1] = str0[i];
  (*str1)[i - m + 1] = '\0';
}


x86 assembly (clang, -O0):
<str_copy_part>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    118a <str_copy_part+0x4a>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%edi
sub    -0x14(%rbp),%edi
add    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x10(%rbp)
jne    11cc <str_copy_part+0x8c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1219 <str_copy_part+0xd9>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp),%ecx
sub    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d5 <str_copy_part+0x95>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp),%ecx
sub    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_copy_part>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
sub    %ebp,%eax
jl     11ba <str_copy_part+0x7a>
add    $0x2,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
movslq %ebp,%rcx
mov    -0x1(%rbx,%rcx,1),%dl
add    $0xffffffffffffffff,%rcx
mov    $0x1,%edi
sub    %ebp,%edi
test   %dl,%dl
je     11ac <str_copy_part+0x6c>
mov    %dl,(%rax)
mov    0x1(%rbx,%rcx,1),%al
add    $0x1,%rcx
test   %al,%al
je     11a9 <str_copy_part+0x69>
mov    $0x1,%esi
nopl   0x0(%rax,%rax,1)
mov    (%r14),%rdx
movslq %esi,%rsi
mov    %al,(%rdx,%rsi,1)
movzbl 0x1(%rbx,%rcx,1),%eax
add    $0x1,%rcx
add    $0x1,%esi
test   %al,%al
jne    1190 <str_copy_part+0x50>
mov    (%r14),%rax
add    %ecx,%edi
movslq %edi,%rcx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"m is larger than str0\0A\00"
@.str.1 = [14 x i8] c"malloc error\0A\00"
define void @str_copy_part(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %7
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i32, i32* %7
%20 = load i32, i32* %6
%21 = sub nsw i32 %19, %20
%22 = add nsw i32 %21, 2
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
%25 = load i8**, i8*** %5
store i8* %24, i8** %25
%26 = load i8**, i8*** %5
%27 = icmp eq i8** %26, null
br i1 %27, label %28, label %32
28: ; preds = %18
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, 1
store i32 %34, i32* %8
br label %35
35: ; preds = %57, %32
%36 = load i8*, i8** %4
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %60
43: ; preds = %35
%44 = load i8*, i8** %4
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = load i8**, i8*** %5
%50 = load i8*, i8** %49
%51 = load i32, i32* %8
%52 = load i32, i32* %6
%53 = sub nsw i32 %51, %52
%54 = add nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %50, i64 %55
store i8 %48, i8* %56
br label %57
57: ; preds = %43
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %35
60: ; preds = %35
%61 = load i8**, i8*** %5
%62 = load i8*, i8** %61
%63 = load i32, i32* %8
%64 = load i32, i32* %6
%65 = sub nsw i32 %63, %64
%66 = add nsw i32 %65, 1
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %62, i64 %67
store i8 0, i8* %68
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"m is larger than str0\0A\00"
define void @str_copy_part(i8* %0, i8** nocapture %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp slt i32 %4, %2
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = sub i32 2, %2
%12 = add i32 %11, %4
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %1
%15 = add nsw i32 %2, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
%20 = sub i32 1, %2
br i1 %19, label %42, label %21
21: ; preds = %10
store i8 %18, i8* %14
%22 = add nsw i64 %16, 1
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %38, label %26
26: ; preds = %21, %26
%27 = phi i8 [ %36, %26 ], [ %24, %21 ]
%28 = phi i64 [ %34, %26 ], [ %22, %21 ]
%29 = load i8*, i8** %1
%30 = trunc i64 %28 to i32
%31 = add i32 %20, %30
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 %27, i8* %33
%34 = add i64 %28, 1
%35 = getelementptr inbounds i8, i8* %0, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %26
38: ; preds = %26, %21
%39 = phi i64 [ %22, %21 ], [ %34, %26 ]
%40 = trunc i64 %39 to i32
%41 = load i8*, i8** %1
br label %42
42: ; preds = %10, %38
%43 = phi i8* [ %41, %38 ], [ %14, %10 ]
%44 = phi i32 [ %40, %38 ], [ %15, %10 ]
%45 = add i32 %20, %44
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8 0, i8* %47
ret void
}