Why C Language Should Have Several Return Values


// Original C
int f(int *i)
{
	*i = 1; // if i was a pointer to protected memory, it would be exploited!
	return 0;
}

int main()
{
	int i;
	f(i);
	return i;
}
@ Original C in ARM
f:
	mov r1, #1
	str r1, [r0] @ Overhead
	mov r0, #0
	bx lr
main:
	push {lr}
	mov r0, sp @ Overhead
	sub sp, sp, #4 @ Overhead
	bl f
	add sp, sp, #4 @ Overhead
	ldr r0, [sp] @ Overhead
	pop {pc}
// Custom C
int f(return int i)
{
	i = 1;
	return 0;
}
int main()
{
	int i;
	f(i);
	return i;
}
f:
	mov r0, #0
	mov r1, #1
main:
	push {lr}
	bl f
	mov r0, r1
	pop {pc}

[top]