#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 */

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int recv(int, void *, int, int /*<<< orphan*/);
int nsStreamSocketRecvData(int sock, void *buffer, unsigned int buffersize) {
  int recvLeft = buffersize;
  int recvLen = 0;
  char *ptr = (char *)buffer;
  while (recvLeft > 0) {
    recvLen = recv(sock, buffer, recvLeft, 0);
    if (recvLen < 0) {
      perror("\n recvData :");
      return -1;
    } else if (0 == recvLen) {
      printf("recvLeft = %d,recvLen = %d\n", recvLeft, recvLen);
      return (buffersize - recvLeft);
    }
    recvLeft -= recvLen;
    ptr += recvLen;
    printf("recvLeft = %d,recvLen = %d\n", recvLeft, recvLen);
  }
  return buffersize;
}


x86 assembly (clang, -O0):
<nsStreamSocketRecvData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11fa <nsStreamSocketRecvData+0xca>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1197 <nsStreamSocketRecvData+0x67>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <nsStreamSocketRecvData+0xd0>
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    11c2 <nsStreamSocketRecvData+0x92>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <nsStreamSocketRecvData+0xd0>
jmp    11c7 <nsStreamSocketRecvData+0x97>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
jmp    1157 <nsStreamSocketRecvData+0x27>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsStreamSocketRecvData>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
test   %edx,%edx
jle    119e <nsStreamSocketRecvData+0x6e>
mov    %rsi,%r15
mov    %edi,%r13d
lea    0xebe(%rip),%r12
mov    %r14d,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11b0 <nsStreamSocketRecvData+0x80>
je     118b <nsStreamSocketRecvData+0x5b>
mov    %eax,%ebx
mov    %ebp,%esi
sub    %eax,%esi
mov    %r12,%rdi
mov    %eax,%edx
call   1040 <printf@plt>
sub    %ebx,%ebp
jg     1160 <nsStreamSocketRecvData+0x30>
jmp    119e <nsStreamSocketRecvData+0x6e>
lea    0xe7b(%rip),%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1040 <printf@plt>
sub    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe49(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%r14d
jmp    119e <nsStreamSocketRecvData+0x6e>

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

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