#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 size_t ssize_t;

/* Variables and functions */
int big2littleEndian(unsigned int);
int /*<<< orphan*/ memmove(unsigned char *, unsigned char *, size_t);
int /*<<< orphan*/ printf(char *);
size_t read(int, unsigned char *, unsigned int);
unsigned int revc_from_server(int sockfd, unsigned char *recvBuff,
                              unsigned int buf_len) {
  int msg_len = 0;
  ssize_t n = 0;
  while ((n = read(sockfd, recvBuff, buf_len)) > 0) {
    recvBuff[n] = 0;
    if (msg_len == 0) {
      msg_len = big2littleEndian(*((unsigned int *)recvBuff));
    }
    if (n >= msg_len)
      break;
  }
  if (n < 0) {
    printf("\n Read error \n");
    return -1;
  }
  memmove(recvBuff, (recvBuff + 4), n - 1);
  *(recvBuff + n - 1) = 0x00;
  return (unsigned int)(n - 4);
}


x86 assembly (clang, -O0):
<revc_from_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jbe    11bd <revc_from_server+0x7d>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x18(%rbp)
jne    11a2 <revc_from_server+0x62>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <big2littleEndian@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmp    %rcx,%rax
jb     11b8 <revc_from_server+0x78>
jmp    11bd <revc_from_server+0x7d>
jmp    1161 <revc_from_server+0x21>
cmpq   $0x0,-0x20(%rbp)
jae    11e0 <revc_from_server+0xa0>
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <revc_from_server+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1060 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
sub    $0x4,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<revc_from_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebp,%ebp
jmp    1158 <revc_from_server+0x28>
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rax
cmp    %rax,%rbx
jae    1183 <revc_from_server+0x53>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1183 <revc_from_server+0x53>
movb   $0x0,(%r14,%rbx,1)
test   %ebp,%ebp
jne    1150 <revc_from_server+0x20>
mov    (%r14),%edi
call   1040 <big2littleEndian@plt>
mov    %eax,%ebp
jmp    1150 <revc_from_server+0x20>
lea    0x4(%r14),%rsi
mov    %rbx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %r14,%rdi
call   1050 <memmove@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
lea    -0x4(%rbx),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"\0A Read error \0A\00"
define i32 @revc_from_server(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i64 0, i64* %9
br label %10
10: ; preds = %33, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = call i64 @read(i32 %11, i8* %12, i32 %13)
store i64 %14, i64* %9
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %10
%17 = load i8*, i8** %6
%18 = load i64, i64* %9
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8 0, i8* %19
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8*, i8** %6
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = call i32 @big2littleEndian(i32 %25)
store i32 %26, i32* %8
br label %27
27: ; preds = %22, %16
%28 = load i64, i64* %9
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = icmp uge i64 %28, %30
br i1 %31, label %32, label %33
32: ; preds = %27
br label %34
33: ; preds = %27
br label %10
34: ; preds = %32, %10
%35 = load i64, i64* %9
%36 = icmp ult i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
39: ; preds = %34
%40 = load i8*, i8** %6
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i64 4
%43 = load i64, i64* %9
%44 = sub i64 %43, 1
%45 = call i32 @memmove(i8* %40, i8* %42, i64 %44)
%46 = load i8*, i8** %6
%47 = load i64, i64* %9
%48 = getelementptr inbounds i8, i8* %46, i64 %47
%49 = getelementptr inbounds i8, i8* %48, i64 -1
store i8 0, i8* %49
%50 = load i64, i64* %9
%51 = sub i64 %50, 4
%52 = trunc i64 %51 to i32
store i32 %52, i32* %4
br label %53
53: ; preds = %39, %37
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
define i32 @revc_from_server(i32 %0, i8* %1, i32 %2) {
%4 = bitcast i8* %1 to i32*
br label %5
5: ; preds = %15, %3
%6 = phi i32 [ 0, %3 ], [ %16, %15 ]
%7 = tail call i64 @read(i32 %0, i8* %1, i32 %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %19, label %9
9: ; preds = %5
%10 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 0, i8* %10
%11 = icmp eq i32 %6, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* %4
%14 = tail call i32 @big2littleEndian(i32 %13)
br label %15
15: ; preds = %12, %9
%16 = phi i32 [ %14, %12 ], [ %6, %9 ]
%17 = sext i32 %16 to i64
%18 = icmp ult i64 %7, %17
br i1 %18, label %5, label %19
19: ; preds = %5, %15
%20 = getelementptr inbounds i8, i8* %1, i64 4
%21 = add i64 %7, -1
%22 = tail call i32 @memmove(i8* %1, i8* %20, i64 %21)
%23 = getelementptr inbounds i8, i8* %1, i64 %7
%24 = getelementptr inbounds i8, i8* %23, i64 -1
store i8 0, i8* %24
%25 = trunc i64 %7 to i32
%26 = add i32 %25, -4
ret i32 %26
}