Wei-Qi Wei

60 papers Misc 21Journal 36Unranked 3
YearRankTypeTitle / Venue / Authors
2025 J jnl
Patterns
Ruowang Li, Luke Benz, Rui Duan, Joshua C. Denny, Hakon Hakonarson, Jonathan D. Mosley, Jordan W. Smoller, Wei-Qi Wei, Thomas Lumley, Marylyn D. Ritchie, Jason H. Moore, Yong Chen
2025 J jnl
J. Am. Medical Informatics Assoc.
Chao Yan, Monika E. Grabowska, Rut Thakkar, Alyson L. Dickson, Peter J. Embí, QiPing Feng, Joshua C. Denny, Vern Eric Kerchberger, Bradley A. Malin, Wei-Qi Wei
2025 J jnl
Database J. Biol. Databases Curation
Samar Binkheder, Xiaofu Liu, Michael Wu, Lei Wang, Aditi Shendre, Sara K. Quinney, Wei-Qi Wei, Lang Li
2025 J jnl
CoRR
Xiao Li, Yanfan Zhu, Ruining Deng, Wei-Qi Wei, Yu Wang, Shilin Zhao, Yaohong Wang, Haichun Yang, Yuankai Huo
2025 J jnl
npj Digit. Medicine
Su Xian, Monika E. Grabowska, Iftikhar J. Kullo, Yuan Luo, Jordan W. Smoller, Theresa L. Walunas, Wei-Qi Wei, Gail P. Jarvik, Sean D. Mooney, David R. Crosslin
2024 J jnl
J. Am. Medical Informatics Assoc.
Monika E. Grabowska, Sara L. Van Driest, Jamie R. Robinson, Anna E. Patrick, Chris Guardo, Srushti Gangireddy, Henry H. Ong, QiPing Feng, Robert J. Carroll, Prince J. Kannankeril, Wei-Qi Wei
2024 J jnl
J. Am. Medical Informatics Assoc.
Wei-Qi Wei, Robb Rowley, Angela M. Wood, Jacqueline Macarthur, Peter J. Embí, Spiros C. Denaxas
2024 J jnl
J. Am. Medical Informatics Assoc.
Chao Yan, Henry H. Ong, Monika E. Grabowska, Matthew S. Krantz, Wu-Chen Su, Alyson L. Dickson, Josh F. Peterson, QiPing Feng, Dan M. Roden, C. Michael Stein, Vern Eric Kerchberger, Bradley A. Malin, Wei-Qi Wei
2024 J jnl
npj Digit. Medicine
Chao Yan, Monika E. Grabowska, Alyson L. Dickson, Bingshan Li, Zhexing Wen, Dan M. Roden, C. Michael Stein, Peter J. Embí, Josh F. Peterson, QiPing Feng, Bradley A. Malin, Wei-Qi Wei
2023 J jnl
J. Am. Medical Informatics Assoc.
Pascal S. Brandt, Abel N. Kho, Yuan Luo, Jennifer A. Pacheco, Theresa L. Walunas, Hakon Hakonarson, George Hripcsak, Cong Liu, Ning Shang, Chunhua Weng, Nephi Walton, David S. Carrell, Paul K. Crane, Eric B. Larson, Christopher G. Chute, Iftikhar J. Kullo, Robert J. Carroll, Joshua C. Denny, Andrea H. Ramirez, Wei-Qi Wei, Jyotishman Pathak, Laura K. Wiley, Rachel L. Richesson, Justin B. Starren, Luke V. Rasmussen
2023 J jnl
J. Am. Medical Informatics Assoc.
Emily R. Pfaff, Andrew T. Girvin, Miles Crosskey, Srushti Gangireddy, Hiral Master, Wei-Qi Wei, Vern Eric Kerchberger, Mark G. Weiner, Paul A. Harris, Melissa A. Basford, Christopher Lunt, Christopher G. Chute, Richard A. Moffitt, Melissa A. Haendel
2023 J jnl
J. Biomed. Informatics
Fuchen Li, Patrick Wu, Henry H. Ong, Josh F. Peterson, Wei-Qi Wei, Juan Zhao
2023 J jnl
J. Am. Medical Informatics Assoc.
Nicholas C. Wan, Ali A Yaqoob, Henry H. Ong, Juan Zhao, Wei-Qi Wei
2023 J jnl
J. Biomed. Informatics
Vipina Kuttichi Keloth, Juan M. Banda, Michael J. Gurley, Paul M. Heider, Georgina Kennedy, Hongfang Liu, Feifan Liu, Timothy Miller, Karthik Natarajan, Olga V. Patterson, Yifan Peng, Kalpana Raja, Ruth M. Reeves, Masoud Rouhizadeh, Jianlin Shi, Xiaoyan Wang, Yanshan Wang, Wei-Qi Wei, Andrew E. Williams, Rui Zhang, Rimma Belenkaya, Christian G. Reich, Clair Blacketer, Patrick B. Ryan, George Hripcsak, Noémie Elhadad, Hua Xu
2023 J jnl
J. Am. Medical Informatics Assoc.
Vern Eric Kerchberger, Josh F. Peterson, Wei-Qi Wei
2022 Misc conf
AMIA
Monika E. Grabowska, Sara L. Van Driest, Patrick Wu, Wei-Qi Wei
2022 Misc conf
AMIA
Yanwei Li, Wei-Qi Wei, Juan Zhao, Chao Yan, Henry H. Ong
2022 J jnl
BMC Medical Informatics Decis. Mak.
Yu Deng, Jennifer A. Pacheco, Anika S. Ghosh, Anh H. Chung, Chengsheng Mao, Joshua C. Smith, Juan Zhao, Wei-Qi Wei, April Barnado, Chad Dorn, Chunhua Weng, Cong Liu, Adam Cordon, Jingzhi Yu, Yacob Tedla, Abel Kho, Rosalind Ramsey-Goldman, Theresa Walunas, Yuan Luo
2022 J jnl
BMC Medical Informatics Decis. Mak.
Jingzhi Yu, Jennifer A. Pacheco, Anika S. Ghosh, Yuan Luo, Chunhua Weng, Ning Shang, Barbara Benoit, David S. Carrell, Robert J. Carroll, Ozan Dikilitas, Robert R. Freimuth, Vivian S. Gainer, Hakon Hakonarson, George Hripcsak, Iftikhar J. Kullo, Frank D. Mentch, Shawn N. Murphy, Peggy L. Peissig, Andrea H. Ramirez, Nephi Walton, Wei-Qi Wei, Luke V. Rasmussen
2021 J jnl
J. Biomed. Informatics
Neil S. Zheng, Jeremy L. Warner, Travis J. Osterman, Quinn Stanton Wells, Xiao-Ou Shu, Stephen A. Deppen, Seth J. Karp, Shon Dwyer, QiPing Feng, Nancy J. Cox, Josh F. Peterson, C. Michael Stein, Dan M. Roden, Kevin B. Johnson, Wei-Qi Wei
2021 J jnl
J. Biomed. Informatics
Juan Zhao, Monika E. Grabowska, Vern Eric Kerchberger, Joshua C. Smith, H. Nur Eken, QiPing Feng, Josh F. Peterson, S. Trent Rosenbloom, Kevin B. Johnson, Wei-Qi Wei
2021 J jnl
J. Am. Medical Informatics Assoc.
Patrick Wu, Scott D. Nelson, Juan Zhao, Cosby A. Stone Jr., QiPing Feng, Qingxia Chen, Eric A. Larson, Bingshan Li, Nancy J. Cox, C. Michael Stein, Elizabeth Phillips, Dan M. Roden, Joshua C. Denny, Wei-Qi Wei
2021 Misc conf
AMIA
Juan Zhao, Monika E. Grabowska, Wei-Qi Wei
2021 Misc conf
AMIA
Jennifer A. Pacheco, Luke V. Rasmussen, Ken Wiley, Thomas N. Person, David J. Cronkite, Sunghwan Sohn, Shawn N. Murphy, Justin H. Gundelach, Vivian S. Gainer, Victor M. Castro, Cong Liu, Todd Lingren, Frank D. Mentch, Agnes S. Sundaresan, Garrett Eickelberg, Valerie Willis, Al'ona Furmanchuk, Roshan Patel, David S. Carrell, Marc S. Williams, Elizabeth W. Karlson, Jodell E. Linder, Yuan Luo, Chunhua Weng, Wei-Qi Wei
2021 J jnl
J. Biomed. Informatics
Mullai Murugan, Lawrence J. Babb, Casey Overby Taylor, Luke V. Rasmussen, Robert R. Freimuth, Eric Venner, Fei Yan, Victoria Yi, Stephen J. Granite, Hana Zouk, Samuel J. Aronson, Kevin Power, Alexander Fedotov, David R. Crosslin, David Fasel, Gail P. Jarvik, Hakon Hakonarson, Hana Bangash, Iftikhar J. Kullo, John J. Connolly, Jordan G. Nestor, Pedro J. Caraballo, Wei-Qi Wei, Ken Wiley, Heidi L. Rehm, Richard A. Gibbs
2021 Misc conf
AMIA
Spiros C. Denaxas, Ge Liu, QiPing Feng, Ghazaleh Fatemifar, Lisa Bastarache, Vern Eric Kerchberger, Aroon D. Hingorani, R. Tom Lumbers, Josh F. Peterson, Wei-Qi Wei, Harry Hemingway
2021 J jnl
CoRR
Yu Deng, Jennifer A. Pacheco, Anh H. Chung, Chengsheng Mao, Joshua C. Smith, Juan Zhao, Wei-Qi Wei, April Barnado, Chunhua Weng, Cong Liu, Adam Cordon, Jingzhi Yu, Yacob Tedla, Abel N. Kho, Rosalind Ramsey-Goldman, Theresa Walunas, Yuan Luo
2021 J jnl
J. Biomed. Informatics
Sarah DeLozier, Sarah Bland, Melissa McPheeters, Quinn Stanton Wells, Eric Farber-Eger, Cosmin Adrian Bejan, Daniel Fabbri, S. Trent Rosenbloom, Dan M. Roden, Kevin B. Johnson, Wei-Qi Wei, Josh F. Peterson, Lisa Bastarache
2021 J jnl
npj Digit. Medicine
Danqing Xu, Chen Wang, Atlas Khan, Ning Shang, Zihuai He, Adam S. Gordon, Iftikhar J. Kullo, Shawn N. Murphy, Yizhao Ni, Wei-Qi Wei, Ali Gharavi, Krzysztof Kiryluk, Chunhua Weng, Iuliana Ionita-Laza
2020 Misc conf
AMIA
Juan Zhao, Yu Wang, QiPing Feng, Wei-Qi Wei
2020 J jnl
J. Am. Medical Informatics Assoc.
Neil S. Zheng, QiPing Feng, Vern Eric Kerchberger, Juan Zhao, Todd L. Edwards, Nancy J. Cox, C. Michael Stein, Dan M. Roden, Joshua C. Denny, Wei-Qi Wei
2020 Misc conf
AMIA
Izabelle P. Humes, Roxana Loperena-Cortes, Melissa A. Basford, Kelsey R. Mayo, Joseph DiPaolo, David J. Schlueter, Jun Qian, Wei-Qi Wei, Robert J. Carroll, David Glazer, Paul A. Harris, Anthony A. Philippakis, Dan M. Roden, Andrea H. Ramirez
2019 Misc conf
AMIA
Patrick Wu, QiPing Feng, Joshua C. Denny, Wei-Qi Wei
2019 Misc conf
AMIA
Juan Zhao, QiPing Feng, Patrick Wu, Joshua C. Denny, Wei-Qi Wei
2019 Misc conf
PSB
Xinyuan Zhang, Yogasudha Veturi, Shefali Setia Verma, William Bone, Anurag Verma, Anastasia Lucas, Scott J. Hebbring, Joshua C. Denny, Ian B. Stanaway, Gail P. Jarvik, David R. Crosslin, Eric B. Larson, Laura Rasmussen-Torvik, Sarah A. Pendergrass, Jordan W. Smoller, Hakon Hakonarson, Patrick Sleiman, Chunhua Weng, David Fasel, Wei-Qi Wei, Iftikhar J. Kullo, Daniel J. Schaid, Wendy K. Chung, Marylyn D. Ritchie
2019 J jnl
J. Biomed. Informatics
Juan Zhao, Yun Zhang, David J. Schlueter, Patrick Wu, Vern Eric Kerchberger, S. Trent Rosenbloom, Quinn Stanton Wells, QiPing Feng, Joshua C. Denny, Wei-Qi Wei
2019 J jnl
J. Biomed. Informatics
Ning Shang, Cong Liu, Luke V. Rasmussen, Casey N. Ta, Robert J. Carroll, Barbara Benoit, Todd Lingren, Ozan Dikilitas, Frank D. Mentch, David S. Carrell, Wei-Qi Wei, Yuan Luo, Vivian S. Gainer, Iftikhar J. Kullo, Jennifer A. Pacheco, Hakon Hakonarson, Theresa L. Walunas, Joshua C. Denny, Chunhua Weng
2018 conf
BIBM
Yizhen Zhong, Luke V. Rasmussen, Yu Deng, Jennifer A. Pacheco, Maureen E. Smith, Justin Starren, Wei-Qi Wei, Peter Speltz, Joshua C. Denny, Nephi Walton, George Hripcsak, Christopher G. Chute, Yuan Luo
2018 J jnl
CoRR
Yizhen Zhong, Luke V. Rasmussen, Yu Deng, Jennifer A. Pacheco, Maureen E. Smith, Justin Starren, Wei-Qi Wei, Peter Speltz, Joshua C. Denny, Nephi Walton, George Hripcsak, Christopher G. Chute, Yuan Luo
2018 Misc conf
AMIA
Juan Zhao, QiPing Feng, Patrick Wu, Joshua C. Denny, Wei-Qi Wei
2017 Misc conf
AMIA
Patrick Wu, Joshua C. Denny, Wei-Qi Wei
2017 Misc conf
AMIA
Jamie R. Robinson, Zongyang Mou, Lisa Bastarache, Wei-Qi Wei, Robert J. Carroll, Joshua C. Denny
2017 J jnl
J. Am. Medical Informatics Assoc.
Pedro L. Teixeira, Wei-Qi Wei, Robert M. Cronin, Huan Mo, Jacob P. VanHouten, Robert J. Carroll, Eric LaRose, Lisa Bastarache, S. Trent Rosenbloom, Todd L. Edwards, Dan M. Roden, Thomas A. Lasko, Richard A. Dart, Anne M. Nikolai, Peggy L. Peissig, Joshua C. Denny
2016 J jnl
J. Am. Medical Informatics Assoc.
Wei-Qi Wei, Pedro L. Teixeira, Huan Mo, Robert M. Cronin, Jeremy L. Warner, Joshua C. Denny
2015 J jnl
J. Am. Medical Informatics Assoc.
Cosmin Adrian Bejan, Wei-Qi Wei, Joshua C. Denny
2015 Misc conf
AMIA
Yaomin Xu, Todd L. Edwards, Lisa Bastarache, Rebecca N. Jerome, Shilin Zho, Eric Torstenson, Wei-Qi Wei, Jana Shirey-Rice, Erica A. Bowton, Yu Shyr, Jill M. Pulley, Joshua C. Denny
2015 Misc conf
AMIA
Travis Osterman, Wei-Qi Wei, Joshua C. Denny
2014 Misc conf
AMIA
Wei-Qi Wei, Pedro L. Teixeira, Huan Mo, Robert M. Cronin, Jeremy L. Warner, Joshua C. Denny
2013 conf
MedInfo
Yonghui Wu, Jianbo Lei, Wei-Qi Wei, Buzhou Tang, Joshua C. Denny, S. Trent Rosenbloom, Randolph A. Miller, Dario A. Giuse, Kai Zheng, Hua Xu
2013 Misc conf
AMIA
Lisa Bastarache, Wei-Qi Wei, Joshua C. Denny
2013 J jnl
J. Am. Medical Informatics Assoc.
Wei-Qi Wei, Robert M. Cronin, Hua Xu, Thomas A. Lasko, Lisa Bastarache, Joshua C. Denny
2013 J jnl
J. Biomed. Informatics
Cui Tao, Jyotishman Pathak, Harold R. Solbrig, Wei-Qi Wei, Christopher G. Chute
2013 J jnl
Int. J. Medical Informatics
Wei-Qi Wei, Cynthia L. Leibson, Jeanine E. Ransom, Abel N. Kho, Christopher G. Chute
2013 Misc conf
AMIA
Christine M. McEvoy, Robert J. Carroll, Lisa Bastarache, Wei-Qi Wei, Joshua C. Denny
2013 Misc conf
AMIA
Wei-Qi Wei, Jonathan D. Mosley, Lisa Bastarache, Joshua C. Denny
2012 Misc conf
AMIA
Wei-Qi Wei, Lisa Bastarache, Joshua C. Denny
2012 Misc conf
AMIA
Lisa Bastarache, Wei-Qi Wei, Joshua C. Denny
2012 J jnl
J. Am. Medical Informatics Assoc.
Wei-Qi Wei, Cynthia L. Leibson, Jeanine E. Ransom, Abel N. Kho, Pedro J. Caraballo, High Seng Chai, Barbara P. Yawn, Jennifer A. Pacheco, Christopher G. Chute
2012 J jnl
J. Am. Medical Informatics Assoc.
Abel N. Kho, M. Geoffrey Hayes, Laura Rasmussen-Torvik, Jennifer A. Pacheco, William K. Thompson, Loren L. Armstrong, Joshua C. Denny, Peggy L. Peissig, Aaron W. Miller, Wei-Qi Wei, Suzette J. Bielinski, Christopher G. Chute, Cynthia L. Leibson, Gail P. Jarvik, David R. Crosslin, Christopher S. Carlson, Katherine M. Newton, Wendy A. Wolf, Rex L. Chisholm, William L. Lowe
2010 conf
ISWC (2)
Cui Tao, Harold R. Solbrig, Deepak K. Sharma, Wei-Qi Wei, Guergana K. Savova, Christopher G. Chute
redb/extractors/decompiler/_archive/GhidraDecompilerScript-v2.java
← Index redb/extractors/decompiler/_archive/GhidraDecompilerScript-v2.java java
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.*;
import ghidra.app.decompiler.*;
import ghidra.program.model.block.*;
import ghidra.program.model.symbol.*;
import ghidra.program.model.pcode.*;
import ghidra.program.model.address.*;
import org.json.JSONObject;
import org.json.JSONArray;
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;

public class GhidraDecompilerScript extends GhidraScript {
    private DecompInterface decompInterface;
    private BasicBlockModel basicBlockModel;

    @Override
    public void run() throws Exception {
        System.err.println("{\"debug\": \"Script starting\"}");

        // Get binary hash and filepath from arguments
        String[] args = getScriptArgs();
        if (args.length < 2) {
            System.err.println("{\"error\": \"Both SHA256 and filepath arguments are required\"}");
            return;
        }
        String sha256 = args[0];
        String filepath = args[1];

        // Add debug output after setup
        System.err.println("{\"debug\": \"Processing file: " + args[1] + "\"}");

        // Initialize analysis components
        setupDecompiler();
        basicBlockModel = new BasicBlockModel(currentProgram);

        // Create the main JSON object for output
        JSONObject output = new JSONObject();
        output.put("sha256", sha256);
        output.put("decompiled", new JSONArray());
        output.put("disassembled", new JSONArray());
        output.put("cfg", new JSONArray());

        // Process all functions
        FunctionIterator functions = currentProgram.getFunctionManager().getFunctions(true);
        for (Function function : functions) {
            processFunction(function, output);
        }

        System.err.println("{\"debug\": \"Preparing final output\"}");
        // Output the final JSON to stdout
        System.out.println(output.toString());
    }

    private void setupDecompiler() {
        decompInterface = new DecompInterface();
        DecompileOptions options = new DecompileOptions();
        decompInterface.setOptions(options);
        decompInterface.openProgram(currentProgram);
    }

    private void processFunction(Function function, JSONObject output) {
        try {
            Address entry = function.getEntryPoint();
            String functionName = function.getName();
            String functionAddress = entry.toString();

            // Process each analysis type independently
            boolean hasAnyResults = false;

            try {
                if (processDecompiledCode(function, output.getJSONArray("decompiled"),
                                        functionName, functionAddress)) {
                    hasAnyResults = true;
                }
            } catch (Exception e) {
                System.err.println(String.format(
                    "{\"error\": \"Decompilation failed for function %s: %s\"}",
                    functionName, e.getMessage().replace("\"", "'")));
            }

            try {
                if (processDisassembledCode(function, output.getJSONArray("disassembled"),
                                        functionName, functionAddress)) {
                    hasAnyResults = true;
                }
            } catch (Exception e) {
                System.err.println(String.format(
                    "{\"error\": \"Disassembly failed for function %s: %s\"}",
                    functionName, e.getMessage().replace("\"", "'")));
            }

            try {
                if (processCFG(function, output.getJSONArray("cfg"), functionAddress)) {
                    hasAnyResults = true;
                }
            } catch (Exception e) {
                System.err.println(String.format(
                    "{\"error\": \"CFG extraction failed for function %s: %s\"}",
                    functionName, e.getMessage().replace("\"", "'")));
            }

            if (!hasAnyResults) {
                System.err.println(String.format(
                    "{\"warning\": \"No results obtained for function %s\"}",
                    functionName));
            }

        } catch (Exception e) {
            System.err.println(String.format(
                "{\"error\": \"Failed to process function: %s\"}",
                e.getMessage().replace("\"", "'")));
        }
    }

    private boolean processDecompiledCode(Function function, JSONArray decompArray,
                                        String functionName, String functionAddress) {
        try {
            DecompileResults results = decompInterface.decompileFunction(function, 30, monitor);
            if (results == null || !results.decompileCompleted()) {
                System.err.println(String.format(
                    "{\"warning\": \"Decompilation incomplete for function %s\"}",
                    functionName));
                return false;
            }

            String decompiledCode = results.getDecompiledFunction().getC();
            if (decompiledCode == null || decompiledCode.trim().isEmpty()) {
                System.err.println(String.format(
                    "{\"warning\": \"Empty decompilation result for function %s\"}",
                    functionName));
                return false;
            }

            String contentHash = calculateHash(decompiledCode);

            JSONObject functionObj = new JSONObject();
            functionObj.put("decompiled_content_hash", contentHash);
            functionObj.put("decompiled_function_name", functionName);
            functionObj.put("decompiled_function_address", functionAddress);
            functionObj.put("decompiled_function", decompiledCode);

            decompArray.put(functionObj);
            return true;

        } catch (Exception e) {
            throw new RuntimeException("Decompilation error: " + e.getMessage(), e);
        }
    }

    private boolean processDisassembledCode(Function function, JSONArray disasmArray,
                                        String functionName, String functionAddress) {
        try {
            StringBuilder disassembly = new StringBuilder();
            StringBuilder normalized = new StringBuilder();
            int instructionCount = 0;
            boolean hasValidInstructions = false;

            Listing listing = currentProgram.getListing();
            AddressSetView functionBody = function.getBody();
            InstructionIterator instructions = listing.getInstructions(functionBody, true);

            while (instructions.hasNext()) {
                try {
                    Instruction instr = instructions.next();
                    if (instr != null) {
                        String disasmLine = instr.toString();
                        if (disasmLine != null && !disasmLine.trim().isEmpty()) {
                            disassembly.append(disasmLine).append("\n");
                            normalized.append(normalizeInstruction(disasmLine)).append("\n");
                            instructionCount++;
                            hasValidInstructions = true;
                        }
                    }
                } catch (Exception e) {
                    System.err.println(String.format(
                        "{\"warning\": \"Skipped invalid instruction in %s: %s\"}",
                        functionName, e.getMessage().replace("\"", "'")));
                }
            }

            if (!hasValidInstructions) {
                System.err.println(String.format(
                    "{\"warning\": \"No valid instructions found in function %s\"}",
                    functionName));
                return false;
            }

            String disassembledCode = disassembly.toString();
            String contentHash = calculateHash(disassembledCode);

            JSONObject functionObj = new JSONObject();
            functionObj.put("disassembled_content_hash", contentHash);
            functionObj.put("disassembled_function_name", functionName);
            functionObj.put("disassembled_function_address", functionAddress);
            functionObj.put("disassembled_function", disassembledCode);
            functionObj.put("normalized_disassembly", normalized.toString());
            functionObj.put("instruction_count", instructionCount);

            // Initialize similarity fields as null
            functionObj.put("minhash_signature", JSONObject.NULL);
            functionObj.put("opcode_frequency_vector", JSONObject.NULL);
            functionObj.put("api_calls_vector", JSONObject.NULL);
            functionObj.put("instruction_embedding", JSONObject.NULL);

            disasmArray.put(functionObj);
            return true;

        } catch (Exception e) {
            throw new RuntimeException("Disassembly error: " + e.getMessage(), e);
        }
    }

    private boolean processCFG(Function function, JSONArray cfgArray, String functionAddress) {
        try {
            CodeBlockIterator blocks = basicBlockModel.getCodeBlocksContaining(
                function.getBody(), monitor);

            boolean hasValidBlocks = false;

            while (blocks.hasNext()) {
                try {
                    CodeBlock block = blocks.next();
                    String blockInstructions = getBlockInstructions(block);

                    if (blockInstructions == null || blockInstructions.trim().isEmpty()) {
                        continue;
                    }

                    String blockId = calculateHash(blockInstructions);

                    JSONObject blockObj = new JSONObject();
                    blockObj.put("block_id", blockId);
                    blockObj.put("function_address", functionAddress);
                    blockObj.put("block_instructions", blockInstructions);

                    // Process successors with error handling
                    JSONArray successorAddresses = new JSONArray();
                    try {
                        CodeBlockReferenceIterator successors = block.getDestinations(monitor);
                        while (successors.hasNext()) {
                            try {
                                CodeBlockReference ref = successors.next();
                                if (ref != null && ref.getDestinationAddress() != null) {
                                    successorAddresses.put(ref.getDestinationAddress().toString());
                                }
                            } catch (Exception e) {
                                System.err.println(String.format(
                                    "{\"warning\": \"Skipped invalid successor in block %s: %s\"}",
                                    blockId, e.getMessage().replace("\"", "'")));
                            }
                        }
                    } catch (Exception e) {
                        System.err.println(String.format(
                            "{\"warning\": \"Error processing successors for block %s: %s\"}",
                            blockId, e.getMessage().replace("\"", "'")));
                    }

                    blockObj.put("successor_blocks", successorAddresses);
                    cfgArray.put(blockObj);
                    hasValidBlocks = true;

                } catch (Exception e) {
                    System.err.println(String.format(
                        "{\"warning\": \"Skipped invalid block in function %s: %s\"}",
                        functionAddress, e.getMessage().replace("\"", "'")));
                }
            }

            return hasValidBlocks;

        } catch (Exception e) {
            throw new RuntimeException("CFG extraction error: " + e.getMessage(), e);
        }
    }

    private String getBlockInstructions(CodeBlock block) {
        StringBuilder instructions = new StringBuilder();
        try {
            AddressIterator addresses = block.getAddresses(true);
            while (addresses.hasNext()) {
                try {
                    Address addr = addresses.next();
                    if (addr != null) {
                        Instruction instr = currentProgram.getListing().getInstructionAt(addr);
                        if (instr != null) {
                            instructions.append(instr.toString()).append("\n");
                        }
                    }
                } catch (Exception e) {
                    System.err.println(String.format(
                        "{\"warning\": \"Skipped invalid instruction at address %s: %s\"}",
                        addresses.next(), e.getMessage().replace("\"", "'")));
                }
            }
        } catch (Exception e) {
            System.err.println(String.format(
                "{\"warning\": \"Error getting block instructions: %s\"}",
                e.getMessage().replace("\"", "'")));
        }
        return instructions.toString();
    }

    private String normalizeInstruction(String instruction) {
        try {
            if (instruction == null || instruction.trim().isEmpty()) {
                return "";
            }
            return instruction.replaceAll("0x[0-9a-fA-F]+", "IMM")
                            .replaceAll("\\b\\d+\\b", "NUM")
                            .replaceAll("[\\[\\]\\+\\-\\*/%&|^]+", "_OP_");
        } catch (Exception e) {
            System.err.println(String.format(
                "{\"warning\": \"Error normalizing instruction: %s\"}",
                e.getMessage().replace("\"", "'")));
            return instruction;
        }
    }

    private String calculateHash(String content) {
        try {
            if (content == null || content.trim().isEmpty()) {
                return "";
            }
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(content.getBytes(StandardCharsets.UTF_8));
            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                hexString.append(String.format("%02x", b));
            }
            return hexString.toString();
        } catch (Exception e) {
            System.err.println(String.format(
                "{\"error\": \"Error calculating hash: %s\"}",
                e.getMessage().replace("\"", "'")));
            return "";
        }
    }
}