#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*/ free(char *);
char getchar();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strlen(char *);
int /*<<< orphan*/ substr(char *, int, int);
int /*<<< orphan*/ substring(char *, char *, int, int);
void substringExample() {
  char *mystr;
  int mystr_len = 10;
  mystr = (char *)(malloc(sizeof(char) * mystr_len + 1));
  printf("---------------------------------------------------------------------"
         "--------------\n");
  printf("*** SUBSTRING EXAMPLE:\n");
  printf("inserire una stringa che verrà ridotta [max 10 char]\n> ");
  char ch;
  int ii = 0;
  while (1) {
    ch = getchar();
    if ((ch != 10) && (ii < mystr_len))
      mystr[ii++] = ch;
    if (ch == 10)
      break;
  }
  mystr[ii] = '\0';
  printf("---------------------------------------------------------------------"
         "--------------\n");
  printf("    mystr|                              |%s| len: %d\n", mystr,
         (int)strlen(mystr));
  char mystr2[10];
  substring(mystr2, mystr, 3, 4);
  printf("\n   mystr2|  substring(mystr2,mystr,3,4) |%s| len: %d\n", mystr2,
         (int)strlen(mystr2));
  substr(mystr, 3, 4);
  printf("\n    mystr|  substr(mystr,3,4)           |%s| len: %d\n", mystr,
         (int)strlen(mystr));
  free(mystr);
  printf("---------------------------------------------------------------------"
         "--------------\n");
}


x86 assembly (clang, -O0):
<substringExample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0xa,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x8(%rbp)
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
je     1202 <substringExample+0x92>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1202 <substringExample+0x92>
mov    -0xd(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
jne    1214 <substringExample+0xa4>
jmp    1219 <substringExample+0xa9>
jmp    11c7 <substringExample+0x57>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x1e(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x3,%edx
mov    $0x4,%ecx
call   1070 <substring@plt>
lea    -0x1e(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x1e(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    $0x3,%esi
mov    $0x4,%edx
call   1050 <substr@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substringExample>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xb,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
lea    0xf56(%rip),%rdi
call   1040 <puts@plt>
lea    0xf33(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4a(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %ebp,%ebp
jmp    11d4 <substringExample+0x54>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0xa,%al
je     11ef <substringExample+0x6f>
xor    %eax,%eax
call   1090 <getchar@plt>
cmp    $0xa,%al
je     11d0 <substringExample+0x50>
cmp    $0x9,%ebp
jg     11d0 <substringExample+0x50>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%rbx,%rcx,1)
jmp    11d0 <substringExample+0x50>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
lea    0xef7(%rip),%r14
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0xe25(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
mov    $0x4,%ecx
call   1080 <substring@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0xe26(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
mov    $0x4,%edx
call   1060 <substr@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0xe30(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [85 x i8] c"-----------------------------------------------------------------------------------\0A\00"
@.str.1 = [24 x i8] c"*** SUBSTRING EXAMPLE:\0A\00"
@.str.2 = [57 x i8] c"inserire una stringa che verr\C3\A0 ridotta [max 10 char]\0A> \00"
@.str.3 = [54 x i8] c" mystr| |%s| len: %d\0A\00"
@.str.4 = [55 x i8] c"\0A mystr2| substring(mystr2,mystr,3,4) |%s| len: %d\0A\00"
@.str.5 = [55 x i8] c"\0A mystr| substr(mystr,3,4) |%s| len: %d\0A\00"
define void @substringExample() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = alloca [10 x i8]
store i32 10, i32* %2
%6 = load i32, i32* %2
%7 = sext i32 %6 to i64
%8 = mul i64 1, %7
%9 = add i64 %8, 1
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %16
16: ; preds = %0, %37
%17 = call signext i8 (...) @getchar()
store i8 %17, i8* %3
%18 = load i8, i8* %3
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 10
br i1 %20, label %21, label %32
21: ; preds = %16
%22 = load i32, i32* %4
%23 = load i32, i32* %2
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = load i8, i8* %3
%27 = load i8*, i8** %1
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 %26, i8* %31
br label %32
32: ; preds = %25, %21, %16
%33 = load i8, i8* %3
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 10
br i1 %35, label %36, label %37
36: ; preds = %32
br label %38
37: ; preds = %32
br label %16
38: ; preds = %36
%39 = load i8*, i8** %1
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0))
%44 = load i8*, i8** %1
%45 = load i8*, i8** %1
%46 = call i64 @strlen(i8* %45)
%47 = trunc i64 %46 to i32
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %47)
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%50 = load i8*, i8** %1
%51 = call i32 @substring(i8* %49, i8* %50, i32 3, i32 4)
%52 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%53 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%54 = call i64 @strlen(i8* %53)
%55 = trunc i64 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i8* %52, i32 %55)
%57 = load i8*, i8** %1
%58 = call i32 @substr(i8* %57, i32 3, i32 4)
%59 = load i8*, i8** %1
%60 = load i8*, i8** %1
%61 = call i64 @strlen(i8* %60)
%62 = trunc i64 %61 to i32
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i8* %59, i32 %62)
%64 = load i8*, i8** %1
%65 = call i32 @free(i8* %64)
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.2 = [57 x i8] c"inserire una stringa che verr\C3\A0 ridotta [max 10 char]\0A> \00"
@.str.3 = [54 x i8] c" mystr| |%s| len: %d\0A\00"
@.str.4 = [55 x i8] c"\0A mystr2| substring(mystr2,mystr,3,4) |%s| len: %d\0A\00"
@.str.5 = [55 x i8] c"\0A mystr| substr(mystr,3,4) |%s| len: %d\0A\00"
@str.6 = [23 x i8] c"*** SUBSTRING EXAMPLE:\00"
@str.8 = [84 x i8] c"-----------------------------------------------------------------------------------\00"
define void @substringExample() {
%1 = alloca [10 x i8]
%2 = tail call i64 @malloc(i32 11)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([84 x i8], [84 x i8]* @str.8, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.6, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
br label %7
7: ; preds = %17, %0
%8 = phi i32 [ 0, %0 ], [ %18, %17 ]
%9 = tail call signext i8 (...) @getchar()
%10 = icmp ne i8 %9, 10
%11 = icmp slt i32 %8, 10
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = add nsw i32 %8, 1
%15 = sext i32 %8 to i64
%16 = getelementptr inbounds i8, i8* %3, i64 %15
store i8 %9, i8* %16
br label %17
17: ; preds = %13, %7
%18 = phi i32 [ %14, %13 ], [ %8, %7 ]
%19 = icmp eq i8 %9, 10
br i1 %19, label %20, label %7
20: ; preds = %17
%21 = sext i32 %18 to i64
%22 = getelementptr inbounds i8, i8* %3, i64 %21
store i8 0, i8* %22
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([84 x i8], [84 x i8]* @str.8, i64 0, i64 0))
%24 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%25 = trunc i64 %24 to i32
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i8* %3, i32 %25)
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%28 = call i32 @substring(i8* %27, i8* %3, i32 3, i32 4)
%29 = call i64 @strlen(i8* %27)
%30 = trunc i64 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i8* %27, i32 %30)
%32 = call i32 @substr(i8* %3, i32 3, i32 4)
%33 = call i64 @strlen(i8* dereferenceable(1) %3)
%34 = trunc i64 %33 to i32
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i8* %3, i32 %34)
%36 = call i32 @free(i8* %3)
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([84 x i8], [84 x i8]* @str.8, i64 0, i64 0))
ret void
}