#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*/ fgets(char *, int, int /*<<< orphan*/);
int find_max_len(char *, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int main(int argc, char const *argv[]) {
  char string[256];
  int max_len;
  int index;
  printf("Input test string: ");
  fgets(string, 256, stdin);
  index = find_max_len(string, &max_len);
  printf("[debug] idx = %d\n", index);
  printf("[debug] len = %d\n", max_len);
  printf("result: ");
  if (max_len == 1) {
    printf("not found\n");
  } else {
    for (int i = 0; i < max_len; i++) {
      printf("%c", string[index]);
      index++;
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeb0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1040 <fgets@plt>
lea    -0x110(%rbp),%rdi
lea    -0x114(%rbp),%rsi
call   1050 <find_max_len@plt>
mov    %eax,-0x118(%rbp)
mov    -0x118(%rbp),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x114(%rbp),%esi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x114(%rbp)
jne    11e0 <main+0xb0>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    124a <main+0x11a>
movl   $0x0,-0x11c(%rbp)
mov    -0x11c(%rbp),%eax
cmp    -0x114(%rbp),%eax
jge    123c <main+0x10c>
movslq -0x118(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%esi
ff
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x118(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x118(%rbp)
mov    -0x11c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x11c(%rbp)
jmp    11ea <main+0xba>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x118,%rsp
lea    0xe9f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <find_max_len@plt>
mov    %eax,%ebx
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%esi
lea    0xe79(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%eax
cmp    $0x1,%eax
jne    11d9 <main+0x89>
lea    0xe6f(%rip),%rdi
call   1040 <puts@plt>
jmp    120e <main+0xbe>
jl     1204 <main+0xb4>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%r14
add    $0x10,%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    0xc(%rsp),%ebx
jl     11f0 <main+0xa0>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Input test string: \00"
@stdin = global i32 0
@.str.1 = [18 x i8] c"[debug] idx = %d\0A\00"
@.str.2 = [18 x i8] c"[debug] len = %d\0A\00"
@.str.3 = [9 x i8] c"result: \00"
@.str.4 = [11 x i8] c"not found\0A\00"
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @stdin
%13 = call i32 @fgets(i8* %11, i32 256, i32 %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%15 = call i32 @find_max_len(i8* %14, i32* %7)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %44
25: ; preds = %2
store i32 0, i32* %9
br label %26
26: ; preds = %39, %25
%27 = load i32, i32* %9
%28 = load i32, i32* %7
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %42
30: ; preds = %26
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
br label %39
39: ; preds = %30
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %26
42: ; preds = %26
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %23
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"Input test string: \00"
@stdin = global i32 0
@.str.1 = [18 x i8] c"[debug] idx = %d\0A\00"
@.str.2 = [18 x i8] c"[debug] len = %d\0A\00"
@.str.3 = [9 x i8] c"result: \00"
@str = [10 x i8] c"not found\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %5, i32 256, i32 %8)
%10 = call i32 @find_max_len(i8* %5, i32* %4)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %4
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = icmp sgt i32 %15, 0
br i1 %18, label %19, label %23
19: ; preds = %17
%20 = sext i32 %10 to i64
br label %25
21: ; preds = %2
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %36
23: ; preds = %25, %17
%24 = call i32 @putchar(i32 10)
br label %36
25: ; preds = %19, %25
%26 = phi i64 [ %20, %19 ], [ %32, %25 ]
%27 = phi i32 [ 0, %19 ], [ %33, %25 ]
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %26
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = call i32 @putchar(i32 %30)
%32 = add nsw i64 %26, 1
%33 = add nuw nsw i32 %27, 1
%34 = load i32, i32* %4
%35 = icmp slt i32 %33, %34
br i1 %35, label %25, label %23
36: ; preds = %23, %21
ret i32 0
}