#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int index = 0;
int max_size = 5;
void **mem = NULL;
void *newmem(size_t size) {
void *temp
= malloc(size
), **temp2
; int i;
if (temp == NULL) {
}
if (mem == NULL) {
mem
= (void**)malloc(max_size
* sizeof(void*)); if (mem == NULL) {
}
}
mem[index++] = temp;
if (index >= max_size) {
temp2
= (void**)realloc(mem
, (max_size
+ 5) * sizeof(void*)); if (temp2 == NULL) {
}
mem = temp2;
max_size += 5;
}
return temp;
}
void releasemem(void) {
int i;
if (mem == NULL) {
return;
}
for (i = 0; i < index; i++) {
if (mem[i] != NULL) {
mem[i] = NULL;
}
}
index = 0;
mem = NULL;
}
void foo() {
}
void bar() {
}
void baz() {
}
int main(void) {
int i, *p;
signal(SIGABRT, releasemem);
for (i = 0; i < 15; i++) {
p = (int*)newmem(sizeof(int));
*p = i;
}
if (i == '1') {
foo();
} else if (i == '2') {
bar();
} else if (i == '3') {
baz();
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHNpZ25hbC5oPgoKaW50IGluZGV4ID0gMDsKaW50IG1heF9zaXplID0gNTsKdm9pZCAqKm1lbSA9IE5VTEw7Cgp2b2lkICpuZXdtZW0oc2l6ZV90IHNpemUpIHsKCXZvaWQgKnRlbXAgPSBtYWxsb2Moc2l6ZSksICoqdGVtcDI7CglpbnQgaTsKCWlmICh0ZW1wID09IE5VTEwpIHsKCQlleGl0KEVYSVRfRkFJTFVSRSk7Cgl9CglpZiAobWVtID09IE5VTEwpIHsKCQltZW0gPSAodm9pZCoqKW1hbGxvYyhtYXhfc2l6ZSAqIHNpemVvZih2b2lkKikpOwoJCWlmIChtZW0gPT0gTlVMTCkgewoJCQlleGl0KEVYSVRfRkFJTFVSRSk7CgkJfQoJfQoJbWVtW2luZGV4KytdID0gdGVtcDsKCWlmIChpbmRleCA+PSBtYXhfc2l6ZSkgewoJCXRlbXAyID0gKHZvaWQ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