A dynamic binary rewriter comprising: a hardware performance monitor contained within a processor, the hardware performance monitor sampling one or more client processes; a DBR service, the DBR service executing as a separate process, the DBR service configured to: aggregate samples from the hardware performance monitor; perform region selection to identify a frequently executed code section in the one or more client processes, wherein identifying the frequently executed code section includes: determining frequently executed code in the one or more client processes; and performing a discovery operation starting from addresses of code known to be frequently executed, the discovery operation disassembling code of the one or more client processes, wherein a control flow of a client process is followed to provide a control flow graph which contains an arbitrary control flow; optimize the selected region; and generate replacement code for the selected region, wherein the replacement code is functionally equivalent to original code of the selected region; and a DBR agent, the DBR agent executing within the one or more client processes, the DBR agent patching the one or more client processes to use the replacement code. a hardware performance monitor contained within a processor, the hardware performance monitor sampling one or more client processes; a DBR service, the DBR service executing as a separate process, the DBR service configured to: aggregate samples from the hardware performance monitor; perform region selection to identify a frequently executed code section in the one or more client processes, wherein identifying the frequently executed code section includes: determining frequently executed code in the one or more client processes; and performing a discovery operation starting from addresses of code known to be frequently executed, the discovery operation disassembling code of the one or more client processes, wherein a control flow of a client process is followed to provide a control flow graph which contains an arbitrary control flow; optimize the selected region; and generate replacement code for the selected region, wherein the replacement code is functionally equivalent to original code of the selected region; and aggregate samples from the hardware performance monitor; perform region selection to identify a frequently executed code section in the one or more client processes, wherein identifying the frequently executed code section includes: determining frequently executed code in the one or more client processes; and performing a discovery operation starting from addresses of code known to be frequently executed, the discovery operation disassembling code of the one or more client processes, wherein a control flow of a client process is followed to provide a control flow graph which contains an arbitrary control flow; determining frequently executed code in the one or more client processes; and performing a discovery operation starting from addresses of code known to be frequently executed, the discovery operation disassembling code of the one or more client processes, wherein a control flow of a client process is followed to provide a control flow graph which contains an arbitrary control flow; optimize the selected region; and generate replacement code for the selected region, wherein the replacement code is functionally equivalent to original code of the selected region; and a DBR agent, the DBR agent executing within the one or more client processes, the DBR agent patching the one or more client processes to use the replacement code.