Skip to content

Instantly share code, notes, and snippets.

@WGH-
Last active January 3, 2016 18:39
Show Gist options
  • Select an option

  • Save WGH-/8503425 to your computer and use it in GitHub Desktop.

Select an option

Save WGH-/8503425 to your computer and use it in GitHub Desktop.
A wrapper script that can be used to conveniently check the assembly/LLVM IR of given C/C++ source. Example usage: ./decompile.py clang -emit-llvm -O2 test.cpp | less
#!/usr/bin/env python3
import os
import sys
import subprocess
import tempfile
import contextlib
@contextlib.contextmanager
def mktemp_contextmanager(*args, **kwargs):
name = tempfile.mktemp(*args, **kwargs)
yield name
try:
os.unlink(name)
except FileNotFoundError:
pass
def main():
argv = sys.argv
with mktemp_contextmanager(suffix=".o") as temp_name:
extargs = []
extargs.append(argv[1])
extargs += ["-c", "-o", temp_name]
extargs += argv[2:]
try:
subprocess.check_call(extargs)
except subprocess.CalledProcessError:
return 1
if "-emit-llvm" in argv:
if "-S" in argv:
extargs = ["cat", temp_name]
else:
extargs = ["llvm-dis", "-o", "-", temp_name]
else:
if "-S" in argv:
extargs = ["cat", temp_name]
else:
# assume ELF format
extargs = ["objdump", "-d", "-Mintel", temp_name]
try:
subprocess.check_call(extargs)
except subprocess.CalledProcessError:
return 1
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment