#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ MSG_DONTWAIT;
int /*<<< orphan*/ memcpy(char **, char **, scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
char *read_car(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int send_data(int, char *);
scalar_t__ strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int socket_recv(int fd, int cli_fd) {
  char buf[1024] = {0};
  char buf2[1024] = {0};
  int retval = 0;
  retval = recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
  if (retval == -1 || retval == 0) {
    return retval;
  }
  if (strncmp(buf, "*", 1) == 0) {
    printf("remote server request data:%s\n", buf);
    return retval;
  }
  if (strncmp(buf, "$", 1) == 0) {
    printf("remote server request hearbeat data\n");
    return retval;
  }
  memcpy(&buf2, &buf, strlen(buf) - 2);
  char *buf3 = read_car(buf2);
  retval = send_data(cli_fd, buf3);
  return retval;
}


x86 assembly (clang, -O0):
<socket_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
movl   $0x0,-0x814(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
mov    $0x400,%edx
call   1030 <recv@plt>
mov    %eax,-0x814(%rbp)
cmpl   $0xffffffff,-0x814(%rbp)
je     11fe <socket_recv+0x7e>
cmpl   $0x0,-0x814(%rbp)
jne    120c <socket_recv+0x8c>
mov    -0x814(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1302 <socket_recv+0x182>
lea    -0x410(%rbp),%rdi
lea    0xde6(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1251 <socket_recv+0xd1>
lea    -0x410(%rbp),%rsi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x814(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1302 <socket_recv+0x182>
lea    -0x410(%rbp),%rdi
lea    0xdc2(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    128f <socket_recv+0x10f>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x814(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1302 <socket_recv+0x182>
lea    -0x810(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x410(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x830(%rbp),%rdi
mov    -0x828(%rbp),%rsi
mov    %rax,%rdx
sub    $0x2,%rdx
call   1090 <memcpy@plt>
lea    -0x810(%rbp),%rdi
call   1050 <read_car@plt>
mov    %rax,-0x820(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x820(%rbp),%rsi
call   10a0 <send_data@plt>
mov    %eax,-0x814(%rbp)
mov    -0x814(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_recv>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <memset@plt>
lea    0x400(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <recv@plt>
mov    %eax,%ebp
add    $0x1,%eax
cmp    $0x2,%eax
jb     1276 <socket_recv+0xe6>
lea    0xe0d(%rip),%rsi
mov    %rsp,%rdi
mov    $0x1,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1257 <socket_recv+0xc7>
lea    0xe15(%rip),%rsi
mov    %rsp,%rdi
mov    $0x1,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     126a <socket_recv+0xda>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    -0x2(%rax),%rdx
lea    0x400(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10a0 <memcpy@plt>
mov    %rbp,%rdi
call   1050 <read_car@plt>
mov    %r14d,%edi
mov    %rax,%rsi
call   10b0 <send_data@plt>
mov    %eax,%ebp
jmp    1276 <socket_recv+0xe6>
lea    0xda4(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1276 <socket_recv+0xe6>
lea    0xdb2(%rip),%rdi
call   1060 <puts@plt>
mov    %ebp,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@MSG_DONTWAIT = global i32 0
@.str = [2 x i8] c"*\00"
@.str.1 = [31 x i8] c"remote server request data:%s\0A\00"
@.str.2 = [2 x i8] c"$\00"
@.str.3 = [37 x i8] c"remote server request hearbeat data\0A\00"
define i32 @socket_recv(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = bitcast [1024 x i8]* %6 to i8*
%11 = bitcast [1024 x i8]* %7 to i8*
store i32 0, i32* %8
%12 = load i32, i32* %4
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%14 = load i32, i32* @MSG_DONTWAIT
%15 = call i32 @recv(i32 %12, i8* %13, i32 1024, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18, %2
%22 = load i32, i32* %8
store i32 %22, i32* %3
br label %51
23: ; preds = %18
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = call i64 @strncmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* %8
store i32 %30, i32* %3
br label %51
31: ; preds = %23
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%33 = call i64 @strncmp(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* %8
store i32 %37, i32* %3
br label %51
38: ; preds = %31
%39 = bitcast [1024 x i8]* %7 to i8**
%40 = bitcast [1024 x i8]* %6 to i8**
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%42 = call i64 @strlen(i8* %41)
%43 = sub nsw i64 %42, 2
%44 = call i32 @memcpy(i8** %39, i8** %40, i64 %43)
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%46 = call i8* @read_car(i8* %45)
store i8* %46, i8** %9
%47 = load i32, i32* %5
%48 = load i8*, i8** %9
%49 = call i32 @send_data(i32 %47, i8* %48)
store i32 %49, i32* %8
%50 = load i32, i32* %8
store i32 %50, i32* %3
br label %51
51: ; preds = %38, %35, %27, %21
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@MSG_DONTWAIT = global i32 0
@.str = [2 x i8] c"*\00"
@.str.1 = [31 x i8] c"remote server request data:%s\0A\00"
@.str.2 = [2 x i8] c"$\00"
@str = [36 x i8] c"remote server request hearbeat data\00"
define i32 @socket_recv(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @MSG_DONTWAIT
%8 = call i32 @recv(i32 %0, i8* %5, i32 1024, i32 %7)
%9 = add i32 %8, 1
%10 = icmp ult i32 %9, 2
br i1 %10, label %29, label %11
11: ; preds = %2
%12 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %29
16: ; preds = %11
%17 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %29
21: ; preds = %16
%22 = bitcast [1024 x i8]* %4 to i8**
%23 = bitcast [1024 x i8]* %3 to i8**
%24 = call i64 @strlen(i8* %5)
%25 = add nsw i64 %24, -2
%26 = call i32 @memcpy(i8** %22, i8** %23, i64 %25)
%27 = call i8* @read_car(i8* %6)
%28 = call i32 @send_data(i32 %1, i8* %27)
br label %29
29: ; preds = %2, %21, %19, %14
%30 = phi i32 [ %8, %14 ], [ %8, %19 ], [ %28, %21 ], [ %8, %2 ]
ret i32 %30
}