set buffer size for instruction cache in cs_disasm() to @count if @count > 0. this avoids realloc() in cases where @count is pre-determined. thanks Dang Hoang Vu for the idea
diff --git a/cs.c b/cs.c
index a66af43..60dee78 100644
--- a/cs.c
+++ b/cs.c
@@ -426,6 +426,7 @@
 	uint64_t offset_org;
 	size_t size_org;
 	const uint8_t *buffer_org;
+	unsigned int cache_size = INSN_CACHE_SIZE;
 
 	if (!handle) {
 		// FIXME: how to handle this case:
@@ -435,11 +436,14 @@
 
 	handle->errnum = CS_ERR_OK;
 
+	if (count > 0)
+		cache_size = count;
+
 	// save the original offset for SKIPDATA
 	buffer_org = buffer;
 	offset_org = offset;
 	size_org = size;
-	total_size = (sizeof(cs_insn) * INSN_CACHE_SIZE);
+	total_size = (sizeof(cs_insn) * cache_size);
 	total = cs_mem_malloc(total_size);
 	insn_cache = total;
 
@@ -475,10 +479,15 @@
 			handle->printer(&mci, &ss, handle->printer_info);
 			fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
 
+			c++;
+			if (count > 0 && c == count)
+				// disasm requested number of instructions
+				break;
+
 			f++;
-			if (f == INSN_CACHE_SIZE) {
+			if (f == cache_size) {
 				// resize total to contain newly disasm insns
-				total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
+				total_size += (sizeof(cs_insn) * cache_size);
 				tmp = cs_mem_realloc(total, total_size);
 				if (tmp == NULL) {	// insufficient memory
 					if (handle->detail) {
@@ -494,17 +503,13 @@
 				}
 
 				total = tmp;
-				insn_cache = (cs_insn *)((char *)total + total_size - (sizeof(cs_insn) * INSN_CACHE_SIZE));
+				insn_cache = (cs_insn *)((char *)total + total_size - (sizeof(cs_insn) * cache_size));
 
 				// reset f back to 0
 				f = 0;
 			} else
 				insn_cache++;
 
-			c++;
-			if (count > 0 && c == count)
-				break;
-
 			buffer += insn_size;
 			size -= insn_size;
 			offset += insn_size;
@@ -544,10 +549,10 @@
 			insn_cache->detail = NULL;
 
 			f++;
-			if (f == INSN_CACHE_SIZE) {
+			if (f == cache_size) {
 				// resize total to contain newly disasm insns
 
-				total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
+				total_size += (sizeof(cs_insn) * cache_size);
 				tmp = cs_mem_realloc(total, total_size);
 				if (tmp == NULL) {	// insufficient memory
 					if (handle->detail) {
@@ -563,7 +568,7 @@
 				}
 
 				total = tmp;
-				insn_cache = (cs_insn *)((char *)total + total_size - (sizeof(cs_insn) * INSN_CACHE_SIZE));
+				insn_cache = (cs_insn *)((char *)total + total_size - (sizeof(cs_insn) * cache_size));
 
 				// reset f back to 0
 				f = 0;
@@ -579,7 +584,7 @@
 
 	if (f) {
 		// resize total to contain newly disasm insns
-		void *tmp = cs_mem_realloc(total, total_size - (INSN_CACHE_SIZE - f) * sizeof(*insn_cache));
+		void *tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
 		if (tmp == NULL) {	// insufficient memory
 			// free all detail pointers
 			if (handle->detail) {