#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*/ asm_sub_str(char *, char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ sub_str(char *, char *, int, int);
int main() {
  char s[100], d[100], f[100];
  int sidx, eidx, n;
  printf("Enter String(100 chars max) :\n");
  n = read(0, s, 100);
  s[n - 1] = '\0';
  printf("Enter start index :");
  scanf("%d", &sidx);
  if (sidx < 0 || sidx > n - 1) {
    printf("Invalid start index\n");
    return 0;
  }
  printf("Enter end index :");
  scanf("%d", &eidx);
  if (eidx < sidx || sidx > n - 1) {
    printf("Invalid end index\n");
    return 0;
  }
  printf("\n-----using inline asm code-----\n");
  printf("before: s=[%s] d=[%s]\n", s, d);
  asm_sub_str(d, s, sidx, eidx);
  printf("after:  s=[%s] d=[%s]\n", s, d);
  printf("\n-----using normal c code-------\n");
  printf("before: s=[%s] f=[%s]\n", s, f);
  sub_str(f, s, sidx, eidx);
  printf("after:  s=[%s] f=[%s]\n", s, f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x70(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1040 <read@plt>
mov    %eax,-0x15c(%rbp)
mov    -0x15c(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x70(%rbp,%rax,1)
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe88(%rip),%rdi
lea    -0x154(%rbp),%rsi
call   1050 <scanf@plt>
cmpl   $0x0,-0x154(%rbp)
jl     11db <main+0x8b>
mov    -0x154(%rbp),%eax
mov    -0x15c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11f5 <main+0xa5>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1318 <main+0x1c8>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe29(%rip),%rdi
lea    -0x158(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x158(%rbp),%eax
cmp    -0x154(%rbp),%eax
jl     123f <main+0xef>
mov    -0x154(%rbp),%eax
mov    -0x15c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1259 <main+0x109>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1318 <main+0x1c8>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x70(%rbp),%rsi
lea    -0xe0(%rbp),%rdx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xe0(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    -0x154(%rbp),%edx
mov    -0x158(%rbp),%ecx
call   1060 <asm_sub_str@plt>
lea    -0x70(%rbp),%rsi
lea    -0xe0(%rbp),%rdx
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x70(%rbp),%rsi
lea    -0x150(%rbp),%rdx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x150(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    -0x154(%rbp),%edx
mov    -0x158(%rbp),%ecx
call   1070 <sub_str@plt>
lea    -0x70(%rbp),%rsi
lea    -0x150(%rbp),%rdx
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x158,%rsp
lea    0xf15(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,%ebp
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe67(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe6d(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
js     129f <main+0x13f>
cmp    %ebp,%eax
jge    129f <main+0x13f>
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe3a(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x8(%rsp),%eax
cmp    %eax,0xc(%rsp)
jl     12a8 <main+0x148>
cmp    %ebp,%eax
jge    12a8 <main+0x148>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1c(%rip),%rdi
lea    0x10(%rsp),%rbx
lea    0xf0(%rsp),%rbp
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <asm_sub_str@plt>
lea    0xdff(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf6(%rip),%rdi
lea    0x80(%rsp),%rbp
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <sub_str@plt>
lea    0xdde(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12b4 <main+0x154>
lea    0xe51(%rip),%rdi
jmp    12af <main+0x14f>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x158,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Enter String(100 chars max) :\0A\00"
@.str.1 = [20 x i8] c"Enter start index :\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [21 x i8] c"Invalid start index\0A\00"
@.str.4 = [18 x i8] c"Enter end index :\00"
@.str.5 = [19 x i8] c"Invalid end index\0A\00"
@.str.6 = [34 x i8] c"\0A-----using inline asm code-----\0A\00"
@.str.7 = [23 x i8] c"before: s=[%s] d=[%s]\0A\00"
@.str.8 = [23 x i8] c"after: s=[%s] d=[%s]\0A\00"
@.str.9 = [34 x i8] c"\0A-----using normal c code-------\0A\00"
@.str.10 = [23 x i8] c"before: s=[%s] f=[%s]\0A\00"
@.str.11 = [23 x i8] c"after: s=[%s] f=[%s]\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%10 = call i32 @read(i32 0, i8* %9, i32 100)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %0
%20 = load i32, i32* %5
%21 = load i32, i32* %7
%22 = sub nsw i32 %21, 1
%23 = icmp sgt i32 %20, %22
br i1 %23, label %24, label %26
24: ; preds = %19, %0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %64
26: ; preds = %19
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%29 = load i32, i32* %6
%30 = load i32, i32* %5
%31 = icmp slt i32 %29, %30
br i1 %31, label %37, label %32
32: ; preds = %26
%33 = load i32, i32* %5
%34 = load i32, i32* %7
%35 = sub nsw i32 %34, 1
%36 = icmp sgt i32 %33, %35
br i1 %36, label %37, label %39
37: ; preds = %32, %26
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %64
39: ; preds = %32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %41, i8* %42)
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%46 = load i32, i32* %5
%47 = load i32, i32* %6
%48 = call i32 @asm_sub_str(i8* %44, i8* %45, i32 %46, i32 %47)
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i8* %49, i8* %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0))
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i8* %53, i8* %54)
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%57 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%58 = load i32, i32* %5
%59 = load i32, i32* %6
%60 = call i32 @sub_str(i8* %56, i8* %57, i32 %58, i32 %59)
%61 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0), i8* %61, i8* %62)
store i32 0, i32* %1
br label %64
64: ; preds = %39, %37, %24
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"Enter start index :\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.4 = [18 x i8] c"Enter end index :\00"
@.str.7 = [23 x i8] c"before: s=[%s] d=[%s]\0A\00"
@.str.8 = [23 x i8] c"after: s=[%s] d=[%s]\0A\00"
@.str.10 = [23 x i8] c"before: s=[%s] f=[%s]\0A\00"
@.str.11 = [23 x i8] c"after: s=[%s] f=[%s]\0A\00"
@str = [30 x i8] c"Enter String(100 chars max) :\00"
@str.12 = [33 x i8] c"\0A-----using inline asm code-----\00"
@str.13 = [33 x i8] c"\0A-----using normal c code-------\00"
@str.14 = [18 x i8] c"Invalid end index\00"
@str.15 = [20 x i8] c"Invalid start index\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%12 = call i32 @read(i32 0, i8* %6, i32 100)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %14
store i8 0, i8* %15
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, -1
%20 = icmp slt i32 %18, %12
%21 = and i1 %19, %20
br i1 %21, label %24, label %22
22: ; preds = %0
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.15, i64 0, i64 0))
br label %47
24: ; preds = %0
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%27 = load i32, i32* %5
%28 = load i32, i32* %4
%29 = icmp sge i32 %27, %28
%30 = icmp slt i32 %28, %12
%31 = and i1 %29, %30
br i1 %31, label %34, label %32
32: ; preds = %24
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.14, i64 0, i64 0))
br label %47
34: ; preds = %24
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.12, i64 0, i64 0))
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %6, i8* %7)
%37 = load i32, i32* %4
%38 = load i32, i32* %5
%39 = call i32 @asm_sub_str(i8* %7, i8* %6, i32 %37, i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i8* %6, i8* %7)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.13, i64 0, i64 0))
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i8* %6, i8* %8)
%43 = load i32, i32* %4
%44 = load i32, i32* %5
%45 = call i32 @sub_str(i8* %8, i8* %6, i32 %43, i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0), i8* %6, i8* %8)
br label %47
47: ; preds = %34, %32, %22
ret i32 0
}