I'm analyzing some code in IDA and I can't figure out the purpose of a couple of instructions.
The code begins with a standard function preamble that pushes the current value of EBP on stack and then shifts the current stack pointer into EBP to later refer to arguments and local variables with respect to it.
But then, it pushes ECX on the stack. Why? I don't suppose anything is being passed in it as an argument to the function?
Then it moves the current value from ECX into [ebp+var_4]
, which seems to be the ECX value that has been pushed there already in the previous instruction :| What's the point of that?
My guess is that it tries to reserve some space on the stack for the single local variable var_4
with that push ecx
, but why would it move the current value of ECX into it in the very next instruction if it already contains it? :P
Moreover, despite the fact that the code is very short, I don't see any place in it where this local variable be used :/ It seems to be totally useless piece of code. And this particular sequence of instructions appears in several other functions, precisely in their preambles, so I guess it's something generated by the compiler, but I can't figure out its purpose.
Any ideas?
; Attributes: bp-based frame
; int __stdcall CloseDevice(HANDLE hObject)
CloseDevice proc near
var_4= dword ptr -4
hObject= dword ptr 8
push ebp
mov ebp, esp
push ecx ; WTF?
mov [ebp+var_4], ecx ; WTF?
cmp [ebp+hObject], 0FFFFFFFFh
jz short loc_BE03C5
mov eax, [ebp+hObject]
push eax
call ds:CloseHandle
loc_BE03C5:
mov esp, ebp
pop esp
retn 4
CloseDevice endp