This Aug. 20, 2014 file photo shows Joint Chiefs Chairman Gen. Martin Dempsey speaking in New York. President Barack Obama's military leadership made clear in recent days that the threat from the Islamic State militants, who murdered American journalist James Foley, cannot be fully eliminated without going after the group in Syria, as well as Iraq."This is an organization that has an apocalyptic, end-of-days strategic vision and which will eventually have to be defeated," said Dempsey. "Can they be defeated without addressing that part of their organization which resides in Syria? The answer is no. That will have to be addressed on both sides of what is essentially at this point a nonexistent border." (AP Photo/Kathy Willens, File)

KABUL, Afghanistan (AP) — The Pentagon has developed plans that would allow American forces to remain in Afghanistan beyond the end of the year if the contested presidential election drags on and a security agreement isn't signed soon, the top U.S. military officer said Monday.

Shortly before landing in Kabul for a visit, Gen. Martin Dempsey, chairman of the Joint Chief of Staff, told reporters accompanying him on the trip that under optimal circumstances the U.S. would need about 120 days to pull all troops and equipment out of the country if there is no agreement allowing them to stay into 2015.

But Dempsey also said the U.S. can act quickly to pull out if necessary. And he added, "We've got our own planning mechanism in place should this thing extend a little further than we hoped it would."

Dempsey arrived in Afghanistan to attend the change of U.S. command. Marine Gen. Joseph Dunford will turn over control of the war effort to Army Gen. John Campbell.

The transition comes at a critical time as the election of a new president is stalled while an audit is conducted to determine the outcome. The lack of a president-elect creates a dilemma for the United States, which has said that all troops would leave by the end of the year unless the security agreement is signed.

But officials have suggested there is some leeway. If weeks from now there is still no agreement, the military could stay a bit into next year in order to conduct an orderly departure.

"We've said we need a (security agreement), not because necessarily we lack the authority to stay beyond the end of the year, but rather as an expression of good faith and good will" by the Afghan government, said Dempsey.

The April 6 voting to elect a successor to President Hamid Karzai resulted in a runoff between former Foreign Minister Abdullah Abdullah and former Finance Minister Ashraf Ghani Ahmadzai.

Abdullah received the most votes in the first round but failed to get the 50 percent needed to win. Preliminary results indicated that Ahmadzai was ahead in the runoff. Both men have claimed fraud and an audit is being conducted to determine who won.

Both men have said they will sign the security agreement that gives U.S. forces the necessary legal protections to stay in the country. Karzai refused to sign the agreement.

Dempsey acknowledged he is worried about the government transition.

"At some point a winner will be declared, but the loser will have done pretty well with a certain part of the Afghan people," said Dempsey. "I think it's important that they find a way to accommodate (the loser) — that it's not a victor and vanquished, that there's a power sharing agreement."

U.S. officials have been pressing the Afghans to form a national unity government by early September. Secretary of State John Kerry made personal appeals earlier this month to both candidates to find a resolution before the upcoming NATO summit in Wales on Sept. 4.

Dempsey said it's still unclear if either of the candidates would be able to attend the summit, where NATO leaders are expected to make decisions about their nations' future roles in Afghanistan.

Afghan forces, meanwhile, are getting hit hard by the Taliban in the south and the east as the peak summer fighting season continues.

Dempsey said he expected the difficult fighting, but said in many cases the Afghans have been able to regroup and take back gains the Taliban had made.

President Barack Obama has ordered the U.S. to withdraw all but roughly 10,000 troops by the end of this year, and to cut that number in half by the end of 2015. The U.S. would leave only about 1,000 in a security office after the end of 2016.

Copyright 2014 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.

*We welcome your comments on the stories and issues of the day and seek to provide a forum for the community to voice opinions. All comments are subject to moderator approval before being made visible on the website but are not edited. The use of profanity, obscene and vulgar language, hate speech, and racial slurs is strictly prohibited. Advertisements, promotions, and spam will also be rejected. Please read our terms of service for full guides