diff --git a/Cargo.lock b/Cargo.lock
index 7e99e45471829330a11d4abc178b775f27b00426..ea295fdffe60316165c2de9f8ef41d3536579039 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -650,6 +650,7 @@ dependencies = [
  "postcard",
  "serde",
  "take_mut",
+ "tempfile",
 ]
 
 [[package]]
@@ -1392,6 +1393,19 @@ version = "1.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
 
+[[package]]
+name = "tempfile"
+version = "3.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b"
+dependencies = [
+ "cfg-if",
+ "fastrand",
+ "once_cell",
+ "rustix",
+ "windows-sys 0.59.0",
+]
+
 [[package]]
 name = "time"
 version = "0.3.36"
diff --git a/hercules_opt/Cargo.toml b/hercules_opt/Cargo.toml
index 20d734027965ff79c7a295596c63ad6b1688b0e8..84f6aca83e508d905ad0e13f0670e7d45c18d22b 100644
--- a/hercules_opt/Cargo.toml
+++ b/hercules_opt/Cargo.toml
@@ -7,6 +7,7 @@ edition = "2021"
 [dependencies]
 ordered-float = "*"
 bitvec = "*"
+tempfile = "*"
 either = "*"
 itertools = "*"
 take_mut = "*"
diff --git a/hercules_opt/src/pass.rs b/hercules_opt/src/pass.rs
index 7685d3045a6b58101efee699a5691269cc56449f..12444b362952546c51e0512be7600a614e602118 100644
--- a/hercules_opt/src/pass.rs
+++ b/hercules_opt/src/pass.rs
@@ -1,6 +1,5 @@
 use std::cell::RefCell;
 use std::collections::{HashMap, HashSet};
-use std::env::temp_dir;
 use std::fs::File;
 use std::io::Write;
 use std::iter::zip;
@@ -8,6 +7,8 @@ use std::process::{Command, Stdio};
 
 use serde::Deserialize;
 
+use tempfile::TempDir;
+
 use hercules_cg::*;
 use hercules_ir::*;
 
@@ -960,7 +961,8 @@ impl PassManager {
                     println!("{}", rust_rt);
 
                     // Write the LLVM IR into a temporary file.
-                    let mut tmp_path = temp_dir();
+                    let tmp_dir = TempDir::new().unwrap();
+                    let mut tmp_path = tmp_dir.path().to_path_buf();
                     tmp_path.push(format!("{}.ll", module_name));
                     println!("{}", tmp_path.display());
                     let mut file = File::create(&tmp_path)