#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*/ printf(char *, char *);
int read(size_t, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
void *rec_func(void *param) {
  size_t fd = (size_t)param;
  char rec_buffer[256];
  int n = 0;
  while (n >= 0) {
    do {
      rec_buffer[0] = '\0';
      n = read(fd, rec_buffer, 1);
    } while ((n >= 0) && (rec_buffer[0] != '$'));
    n = read(fd, rec_buffer, 3);
    if (n < 0) {
      continue;
    }
    rec_buffer[2] = '\0';
    int length = 0;
    sscanf(rec_buffer, "%x", &length);
    n = read(fd, rec_buffer, length);
    rec_buffer[n] = '\0';
    if (n < 0) {
      continue;
    }
    printf("Received: %s\n", rec_buffer);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<rec_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jl     1268 <rec_func+0x138>
jmp    1163 <rec_func+0x33>
movb   $0x0,-0x110(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x114(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x114(%rbp)
mov    %al,-0x119(%rbp)
jl     11ad <rec_func+0x7d>
movsbl -0x110(%rbp),%eax
cmp    $0x24,%eax
setne  %al
mov    %al,-0x119(%rbp)
mov    -0x119(%rbp),%al
test   $0x1,%al
jne    1163 <rec_func+0x33>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x3,%edx
call   1040 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    11e8 <rec_func+0xb8>
jmp    1151 <rec_func+0x21>
movb   $0x0,-0x10e(%rbp)
movl   $0x0,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xdf9(%rip),%rsi
lea    -0x118(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    -0x118(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x114(%rbp)
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
cmpl   $0x0,-0x114(%rbp)
jge    1250 <rec_func+0x120>
jmp    1151 <rec_func+0x21>
lea    -0x110(%rbp),%rsi
lea    0xda5(%rip),%rdi
call   1030 <printf@plt>
jmp    1151 <rec_func+0x21>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rec_func>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%r14
lea    0xeb1(%rip),%r15
lea    0xc(%rsp),%r12
lea    0xea8(%rip),%r13
nopl   0x0(%rax,%rax,1)
movb   $0x0,0x10(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1180 <rec_func+0x50>
cmpb   $0x24,0x10(%rsp)
jne    1160 <rec_func+0x30>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11d6 <rec_func+0xa6>
movb   $0x0,0x12(%rsp)
movl   $0x0,0xc(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <sscanf@plt>
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
test   %eax,%eax
js     11d6 <rec_func+0xa6>
mov    %r13,%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
jmp    1160 <rec_func+0x30>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%x\00"
@.str.1 = [14 x i8] c"Received: %s\0A\00"
define i8* @rec_func(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = ptrtoint i8* %7 to i64
store i64 %8, i64* %3
store i32 0, i32* %5
br label %9
9: ; preds = %49, %48, %34, %1
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %52
12: ; preds = %9
br label %13
13: ; preds = %26, %12
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
store i8 0, i8* %14
%15 = load i64, i64* %3
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = call i32 @read(i64 %15, i8* %16, i32 1)
store i32 %17, i32* %5
br label %18
18: ; preds = %13
%19 = load i32, i32* %5
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 36
br label %26
26: ; preds = %21, %18
%27 = phi i1 [ false, %18 ], [ %25, %21 ]
br i1 %27, label %13, label %28
28: ; preds = %26
%29 = load i64, i64* %3
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%31 = call i32 @read(i64 %29, i8* %30, i32 3)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
br label %9
35: ; preds = %28
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 2
store i8 0, i8* %36
store i32 0, i32* %6
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%38 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%39 = load i64, i64* %3
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%41 = load i32, i32* %6
%42 = call i32 @read(i64 %39, i8* %40, i32 %41)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %44
store i8 0, i8* %45
%46 = load i32, i32* %5
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %35
br label %9
49: ; preds = %35
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%51 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %50)
br label %9
52: ; preds = %9
ret i8* null
}

LLVM IR (-O2):
@.str = [3 x i8] c"%x\00"
@.str.1 = [14 x i8] c"Received: %s\0A\00"
define noalias i8* @rec_func(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = ptrtoint i8* %0 to i64
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 2
%7 = bitcast i32* %3 to i8*
br label %8
8: ; preds = %14, %1
store i8 0, i8* %5
%9 = call i32 @read(i64 %4, i8* %5, i32 1)
%10 = icmp sgt i32 %9, -1
%11 = load i8, i8* %5
%12 = icmp ne i8 %11, 36
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %14, label %15
14: ; preds = %8, %18
br label %8
15: ; preds = %8
%16 = call i32 @read(i64 %4, i8* %5, i32 3)
%17 = icmp slt i32 %16, 0
br i1 %17, label %28, label %20
18: ; preds = %20
%19 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %14
20: ; preds = %15
store i8 0, i8* %6
store i32 0, i32* %3
%21 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%22 = load i32, i32* %3
%23 = call i32 @read(i64 %4, i8* %5, i32 %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
%26 = icmp slt i32 %23, 0
br i1 %26, label %27, label %18
27: ; preds = %20
br label %28
28: ; preds = %15, %27
ret i8* null
}