#include <ibcs/unistd.h>
#include <sysdeps.h>
#define ASSEMBLY_FILE
#include <errno.h>

	.text
	.align	ALIGN
#ifdef ELF_LIBRARY
	.globl	SYMBOL_NAME(_brk)
	.type SYMBOL_NAME(_brk),@function
SYMBOL_NAME_LABEL(_brk)
#else
	.globl	SYMBOL_NAME(__brk)
SYMBOL_NAME_LABEL(__brk)
#endif

#ifdef __PIC__
	movl	$__IBCS_brk,%eax
	.byte	0x9a,0,0,0,0,7,0
	jnc	.L1
	movl	$-1,%eax
	pushl %ebx
	call .L2
.L2:
	popl %ebx
	addl $_GLOBAL_OFFSET_TABLE_+[.-.L2],%ebx
	movl	ERRNO@GOT(%ebx),%ebx
	movl	$ENOMEM, (%ebx)
	popl    %ebx
	ret
.L1:
	pushl %ebx
	call .L3
.L3:
	popl %ebx
	addl $_GLOBAL_OFFSET_TABLE_+[.-.L3],%ebx
	movl	SYMBOL_NAME(___brk_addr)@GOT(%ebx),%ebx
	movl	8(%esp),%eax
	movl	%eax, (%ebx)
	movl	$0,%eax
	popl %ebx
	ret
#else
	movl	$__IBCS_brk,%eax
	.byte	0x9a,0,0,0,0,7,0
	jnc	LF(1)
	STORE_NUMBER($ENOMEM,ERRNO)
	movl	$-1,%eax
	ret
LL(1)
	movl	4(%esp), %eax
	movl	%eax, SYMBOL_NAME(___brk_addr)
	movl	$0,%eax
	ret
#endif

#ifdef ELF_LIBRARY
	.type SYMBOL_NAME(brk),@function
	.weak SYMBOL_NAME(brk)
	SYMBOL_NAME(brk) = SYMBOL_NAME(_brk)
#endif

#ifdef AOUT_LIBRARY
	.globl SYMBOL_NAME(brk)
	SYMBOL_NAME(brk) = SYMBOL_NAME(__brk)
#endif
