#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*/ printf(char *, ...);
int strlen(char *);
void GetNext(char *str, int *next) {
  int i = 0, j = -1;
  next[i] = -1;
  printf("next[%d] = %2d\n", i, next[i]);
  while (i < strlen(str) - 1) {
    printf("i = %d, j = %d\n", i, j);
    if (j == -1 || str[i] == str[j]) {
      if (str[i] == str[j]) {
        if (next[i] == j) {
          printf("un back : str[%d] == str[%d], ", i, j);
        } else {
          printf("back to : str[%d] == str[%d], ", i, j);
        }
      } else {
        printf("back to begin : j == -1, ");
      }
      printf("next[%d] = j + 1 = %d\n", i, j + 1);
      i++;
      j++;
      next[i] = j;
    } else {
      printf("start back : str[%d] != str[%d], j = next[j] = %d\n", i, j, j);
      j = next[j];
    }
    printf("next[%d] = %2d\n", i, next[i]);
  }
  printf("\n");
  for (int i = 0; i < strlen(str); i++) {
    printf("%d  ", next[i]);
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<GetNext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12bc <GetNext+0x19c>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xffffffff,-0x18(%rbp)
je     11c6 <GetNext+0xa6>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x8(%rbp),%rcx
movslq -0x18(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    1276 <GetNext+0x156>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x8(%rbp),%rcx
movslq -0x18(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    122c <GetNext+0x10c>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x18(%rbp),%eax
jne    1213 <GetNext+0xf3>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1227 <GetNext+0x107>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <GetNext+0x11a>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
add    $0x1,%edx
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    129b <GetNext+0x17b>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1169 <GetNext+0x49>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    1314 <GetNext+0x1f4>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12d1 <GetNext+0x1b1>
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetNext>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
movl   $0xffffffff,(%rsi)
lea    0xeb2(%rip),%rdi
mov    $0xffffffff,%ebp
xor    %esi,%esi
mov    $0xffffffff,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     126e <GetNext+0x13e>
xor    %r13d,%r13d
mov    %rbx,(%rsp)
jmp    11ce <GetNext+0x9e>
nopl   0x0(%rax,%rax,1)
lea    0xf08(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rax
mov    (%rax,%rbx,4),%r12d
mov    %rax,%rbx
mov    (%rax,%r15,4),%ebp
lea    0xe55(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0xffffffff,%eax
mov    %r12d,%ebp
cmp    %eax,%r13d
jge    126e <GetNext+0x13e>
lea    0xe3b(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movslq %r13d,%r15
movzbl (%r14,%r15,1),%eax
cmp    $0xffffffff,%ebp
je     1200 <GetNext+0xd0>
movslq %ebp,%rbx
cmp    (%r14,%rbx,1),%al
jne    1180 <GetNext+0x50>
jmp    1206 <GetNext+0xd6>
nopl   0x0(%rax)
cmp    -0x1(%r14),%al
jne    1235 <GetNext+0x105>
mov    (%rsp),%rbx
cmp    %ebp,(%rbx,%r15,4)
jne    1220 <GetNext+0xf0>
lea    0xe09(%rip),%rdi
jmp    1227 <GetNext+0xf7>
nopl   0x0(%rax)
lea    0xe18(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1247 <GetNext+0x117>
lea    0xe22(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rbx
add    $0x1,%ebp
lea    0xe27(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r13d
mov    %ebp,0x4(%rbx,%r15,4)
mov    %ebp,%r12d
jmp    11a4 <GetNext+0x74>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    12b0 <GetNext+0x180>
lea    0xe37(%rip),%r15
xor    %ebp,%ebp
nopl   (%rax)
mov    (%rbx,%rbp,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     1290 <GetNext+0x160>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"next[%d] = %2d\0A\00"
@.str.1 = [16 x i8] c"i = %d, j = %d\0A\00"
@.str.2 = [31 x i8] c"un back : str[%d] == str[%d], \00"
@.str.3 = [31 x i8] c"back to : str[%d] == str[%d], \00"
@.str.4 = [26 x i8] c"back to begin : j == -1, \00"
@.str.5 = [23 x i8] c"next[%d] = j + 1 = %d\0A\00"
@.str.6 = [51 x i8] c"start back : str[%d] != str[%d], j = next[j] = %d\0A\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [5 x i8] c"%d \00"
define void @GetNext(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %5
store i32 -1, i32* %6
%8 = load i32*, i32** %4
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i32, i32* %8, i64 %10
store i32 -1, i32* %11
%12 = load i32, i32* %5
%13 = load i32*, i32** %4
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i32, i32* %13, i64 %15
%17 = load i32, i32* %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %17)
br label %19
19: ; preds = %102, %2
%20 = load i32, i32* %5
%21 = load i8*, i8** %3
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 1
%24 = icmp slt i32 %20, %23
br i1 %24, label %25, label %110
25: ; preds = %19
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %45, label %31
31: ; preds = %25
%32 = load i8*, i8** %3
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = load i8*, i8** %3
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %37, %43
br i1 %44, label %45, label %92
45: ; preds = %31, %25
%46 = load i8*, i8** %3
%47 = load i32, i32* %5
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = load i8*, i8** %3
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %51, %57
br i1 %58, label %59, label %76
59: ; preds = %45
%60 = load i32*, i32** %4
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
%64 = load i32, i32* %63
%65 = load i32, i32* %6
%66 = icmp eq i32 %64, %65
br i1 %66, label %67, label %71
67: ; preds = %59
%68 = load i32, i32* %5
%69 = load i32, i32* %6
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %68, i32 %69)
br label %75
71: ; preds = %59
%72 = load i32, i32* %5
%73 = load i32, i32* %6
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %72, i32 %73)
br label %75
75: ; preds = %71, %67
br label %78
76: ; preds = %45
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %78
78: ; preds = %76, %75
%79 = load i32, i32* %5
%80 = load i32, i32* %6
%81 = add nsw i32 %80, 1
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %79, i32 %81)
%83 = load i32, i32* %5
%84 = add nsw i32 %83, 1
store i32 %84, i32* %5
%85 = load i32, i32* %6
%86 = add nsw i32 %85, 1
store i32 %86, i32* %6
%87 = load i32, i32* %6
%88 = load i32*, i32** %4
%89 = load i32, i32* %5
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i32, i32* %88, i64 %90
store i32 %87, i32* %91
br label %102
92: ; preds = %31
%93 = load i32, i32* %5
%94 = load i32, i32* %6
%95 = load i32, i32* %6
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0), i32 %93, i32 %94, i32 %95)
%97 = load i32*, i32** %4
%98 = load i32, i32* %6
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds i32, i32* %97, i64 %99
%101 = load i32, i32* %100
store i32 %101, i32* %6
br label %102
102: ; preds = %92, %78
%103 = load i32, i32* %5
%104 = load i32*, i32** %4
%105 = load i32, i32* %5
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds i32, i32* %104, i64 %106
%108 = load i32, i32* %107
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %103, i32 %108)
br label %19
110: ; preds = %19
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %7
br label %112
112: ; preds = %124, %110
%113 = load i32, i32* %7
%114 = load i8*, i8** %3
%115 = call i32 @strlen(i8* %114)
%116 = icmp slt i32 %113, %115
br i1 %116, label %117, label %127
117: ; preds = %112
%118 = load i32*, i32** %4
%119 = load i32, i32* %7
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds i32, i32* %118, i64 %120
%122 = load i32, i32* %121
%123 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i32 %122)
br label %124
124: ; preds = %117
%125 = load i32, i32* %7
%126 = add nsw i32 %125, 1
store i32 %126, i32* %7
br label %112
127: ; preds = %112
%128 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"next[%d] = %2d\0A\00"
@.str.1 = [16 x i8] c"i = %d, j = %d\0A\00"
@.str.2 = [31 x i8] c"un back : str[%d] == str[%d], \00"
@.str.3 = [31 x i8] c"back to : str[%d] == str[%d], \00"
@.str.4 = [26 x i8] c"back to begin : j == -1, \00"
@.str.5 = [23 x i8] c"next[%d] = j + 1 = %d\0A\00"
@.str.6 = [51 x i8] c"start back : str[%d] != str[%d], j = next[j] = %d\0A\00"
@.str.8 = [5 x i8] c"%d \00"
define void @GetNext(i8* %0, i32* nocapture %1) {
store i32 -1, i32* %1
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 0, i32 -1)
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp sgt i32 %4, 1
br i1 %5, label %6, label %54
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 -1
br label %8
8: ; preds = %6, %46
%9 = phi i32 [ %49, %46 ], [ -1, %6 ]
%10 = phi i32 [ %48, %46 ], [ 0, %6 ]
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %9)
%12 = icmp eq i32 %9, -1
%13 = sext i32 %10 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
br i1 %12, label %21, label %16
16: ; preds = %8
%17 = sext i32 %9 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %15, %19
br i1 %20, label %24, label %40
21: ; preds = %8
%22 = load i8, i8* %7
%23 = icmp eq i8 %15, %22
br i1 %23, label %24, label %32
24: ; preds = %16, %21
%25 = getelementptr inbounds i32, i32* %1, i64 %13
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, %9
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %9)
br label %34
30: ; preds = %24
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %10, i32 %9)
br label %34
32: ; preds = %21
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %28, %30, %32
%35 = add nsw i32 %9, 1
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %10, i32 %35)
%37 = add nsw i32 %10, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %1, i64 %38
store i32 %35, i32* %39
br label %46
40: ; preds = %16
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0), i32 %10, i32 %9, i32 %9)
%42 = getelementptr inbounds i32, i32* %1, i64 %17
%43 = load i32, i32* %42
%44 = getelementptr inbounds i32, i32* %1, i64 %13
%45 = load i32, i32* %44
br label %46
46: ; preds = %40, %34
%47 = phi i32 [ %45, %40 ], [ %35, %34 ]
%48 = phi i32 [ %10, %40 ], [ %37, %34 ]
%49 = phi i32 [ %43, %40 ], [ %35, %34 ]
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %48, i32 %47)
%51 = tail call i32 @strlen(i8* %0)
%52 = add nsw i32 %51, -1
%53 = icmp slt i32 %48, %52
br i1 %53, label %8, label %54
54: ; preds = %46, %2
%55 = tail call i32 @putchar(i32 10)
%56 = tail call i32 @strlen(i8* %0)
%57 = icmp sgt i32 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %60, %54
%59 = tail call i32 @putchar(i32 10)
ret void
60: ; preds = %54, %60
%61 = phi i64 [ %65, %60 ], [ 0, %54 ]
%62 = getelementptr inbounds i32, i32* %1, i64 %61
%63 = load i32, i32* %62
%64 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i32 %63)
%65 = add nuw nsw i64 %61, 1
%66 = tail call i32 @strlen(i8* %0)
%67 = sext i32 %66 to i64
%68 = icmp slt i64 %65, %67
br i1 %68, label %60, label %58
}