_flamegraph() {
    local i cur prev opts cmd
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    cmd=""
    opts=""

    for i in ${COMP_WORDS[@]}
    do
        case "${cmd},${i}" in
            ",$1")
                cmd="flamegraph"
                ;;
            *)
                ;;
        esac
    done

    case "${cmd}" in
        flamegraph)
            opts="-p -v -o -F -c -i -h -V --pid --completions --verbose --output --open --root --freq --cmd --deterministic --inverted --reverse --notes --min-width --image-width --palette --skip-after --flamechart --ignore-status --no-inline --post-process --perfdata --help --version [TRAILING_ARGUMENTS]..."
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
                return 0
            fi
            case "${prev}" in
                --pid)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                -p)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --completions)
                    COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}"))
                    return 0
                    ;;
                --output)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                -o)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --root)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --freq)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                -F)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --cmd)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                -c)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --notes)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --min-width)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --image-width)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --palette)
                    COMPREPLY=($(compgen -W "aqua blue green hot io java js mem orange perl python purple red rust wakeup yellow" -- "${cur}"))
                    return 0
                    ;;
                --skip-after)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --post-process)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --perfdata)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                *)
                    COMPREPLY=()
                    ;;
            esac
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            return 0
            ;;
    esac
}

if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
    complete -F _flamegraph -o nosort -o bashdefault -o default flamegraph
else
    complete -F _flamegraph -o bashdefault -o default flamegraph
fi