#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef scalar_t__ uint32_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ printf(char *, int);
int recv(int, char *, scalar_t__, int /*<<< orphan*/);
int readPackets(int sock, uint32_t length, uint32_t count) {
  int read_size = 1;
  int32_t total = 0;
  for (uint32_t counter = 0; read_size > 0 && counter < count; counter++) {
    char *incoming = malloc(length * sizeof(char) + 1);
    read_size = recv(sock, incoming, length - 1, 0);
    free(incoming);
    if (read_size <= 0) {
      printf("rs: %i\n", read_size);
      return read_size;
    }
    total += read_size;
  }
  return total;
}


x86 assembly (clang, -O0):
<readPackets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x31(%rbp)
jle    1186 <readPackets+0x46>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1196 <readPackets+0x56>
jmp    120b <readPackets+0xcb>
mov    -0x10(%rbp),%rdi
shl    $0x0,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x1c(%rbp)
jg     11f1 <readPackets+0xb1>
mov    -0x1c(%rbp),%esi
lea    0xe1f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1211 <readPackets+0xd1>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1169 <readPackets+0x29>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readPackets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,0x4(%rsp)
test   %rdx,%rdx
jle    11a4 <readPackets+0x64>
mov    %rdx,%r13
mov    %rsi,%r14
lea    0x1(%rsi),%r12
add    $0xffffffffffffffff,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    0x4(%rsp),%edi
mov    %rax,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jle    11a8 <readPackets+0x68>
add    %ebp,%ebx
add    $0xffffffffffffffff,%r13
jne    1170 <readPackets+0x30>
jmp    11b8 <readPackets+0x78>
xor    %ebx,%ebx
jmp    11b8 <readPackets+0x78>
lea    0xe51(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"rs: %i\0A\00"
define i32 @readPackets(i32 %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 1, i32* %8
store i32 0, i32* %9
store i64 0, i64* %10
br label %12
12: ; preds = %43, %3
%13 = load i32, i32* %8
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i64, i64* %10
%17 = load i64, i64* %7
%18 = icmp slt i64 %16, %17
br label %19
19: ; preds = %15, %12
%20 = phi i1 [ false, %12 ], [ %18, %15 ]
br i1 %20, label %21, label %46
21: ; preds = %19
%22 = load i64, i64* %6
%23 = mul i64 %22, 1
%24 = add i64 %23, 1
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %11
%26 = load i32, i32* %5
%27 = load i8*, i8** %11
%28 = load i64, i64* %6
%29 = sub nsw i64 %28, 1
%30 = call i32 @recv(i32 %26, i8* %27, i64 %29, i32 0)
store i32 %30, i32* %8
%31 = load i8*, i8** %11
%32 = call i32 @free(i8* %31)
%33 = load i32, i32* %8
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %21
%36 = load i32, i32* %8
%37 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %48
39: ; preds = %21
%40 = load i32, i32* %8
%41 = load i32, i32* %9
%42 = add nsw i32 %41, %40
store i32 %42, i32* %9
br label %43
43: ; preds = %39
%44 = load i64, i64* %10
%45 = add nsw i64 %44, 1
store i64 %45, i64* %10
br label %12
46: ; preds = %19
%47 = load i32, i32* %9
store i32 %47, i32* %4
br label %48
48: ; preds = %46, %35
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str = [8 x i8] c"rs: %i\0A\00"
define i32 @readPackets(i32 %0, i64 %1, i64 %2) {
%4 = icmp sgt i64 %2, 0
br i1 %4, label %5, label %21
5: ; preds = %3
%6 = add i64 %1, 1
%7 = add nsw i64 %1, -1
br label %8
8: ; preds = %5, %17
%9 = phi i64 [ 0, %5 ], [ %19, %17 ]
%10 = phi i32 [ 0, %5 ], [ %18, %17 ]
%11 = tail call i8* @malloc(i64 %6)
%12 = tail call i32 @recv(i32 %0, i8* %11, i64 %7, i32 0)
%13 = tail call i32 @free(i8* %11)
%14 = icmp slt i32 %12, 1
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %21
17: ; preds = %8
%18 = add nsw i32 %12, %10
%19 = add nuw nsw i64 %9, 1
%20 = icmp eq i64 %19, %2
br i1 %20, label %21, label %8
21: ; preds = %17, %3, %15
%22 = phi i32 [ %12, %15 ], [ 0, %3 ], [ %18, %17 ]
ret i32 %22
}