#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*/ memcpy(char *, char *, short);
int /*<<< orphan*/ memmove(char *, char *, short);
short serial_read_string(int, char *, int, short, char *, char *, long);
char *strstr(char *, char *);
char *get_sentence(int fd, long int tmt) {
  static char buf[256] = {0};
  static char snt[256] = {0};
  static short int n = 0;
  n = serial_read_string(fd, buf, 256, n, "$", "\r\n", tmt);
  if (n > 0) {
    short int m;
    m = strstr(buf, "\r\n") + 2 - buf;
    memcpy(snt, buf, m);
    snt[m] = '\0';
    n -= m;
    memmove(buf, buf + m, n);
    buf[n] = '\0';
    return snt;
  } else
    return NULL;
}


x86 assembly (clang, -O0):
<get_sentence>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x30f7(%rip),%cx
mov    -0x18(%rbp),%rax
lea    0x2eec(%rip),%rsi
mov    $0x100,%edx
lea    0xe90(%rip),%r8
lea    0xe8b(%rip),%r9
movswl %cx,%ecx
mov    %rax,(%rsp)
call   1030 <serial_read_string@plt>
mov    %ax,0x30c6(%rip)
movswl 0x30bf(%rip),%eax
cmp    $0x0,%eax
jle    1246 <get_sentence+0x106>
lea    0x2eaf(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   1060 <strstr@plt>
add    $0x2,%rax
lea    0x2e98(%rip),%rcx
sub    %rcx,%rax
mov    %ax,-0x1a(%rbp)
mov    -0x1a(%rbp),%ax
movswl %ax,%edx
lea    0x2f83(%rip),%rdi
lea    0x2e7c(%rip),%rsi
call   1040 <memcpy@plt>
movswq -0x1a(%rbp),%rcx
lea    0x2f6b(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
movswl -0x1a(%rbp),%ecx
movswl 0x305c(%rip),%eax
sub    %ecx,%eax
mov    %ax,0x3053(%rip)
movswl -0x1a(%rbp),%eax
cltq
lea    0x2e46(%rip),%rsi
add    %rax,%rsi
mov    0x303c(%rip),%ax
movswl %ax,%edx
lea    0x2e32(%rip),%rdi
call   1050 <memmove@plt>
movswq 0x3025(%rip),%rcx
lea    0x2e1e(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2f13(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    124e <get_sentence+0x10e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sentence>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movswl 0x3100(%rip),%ecx
mov    %rsi,(%rsp)
lea    0x2ef5(%rip),%r14
lea    0xe9e(%rip),%r8
lea    0xe99(%rip),%r9
mov    %r14,%rsi
mov    $0x100,%edx
call   1030 <serial_read_string@plt>
mov    %ax,0x30d3(%rip)
test   %ax,%ax
jle    11e5 <get_sentence+0xa5>
lea    0xe79(%rip),%rsi
mov    %r14,%rdi
call   1060 <strstr@plt>
mov    %rax,%rbx
sub    %r14d,%ebx
add    $0x2,%ebx
movswl %bx,%edx
lea    0x2fac(%rip),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
movswq %bx,%rsi
movb   $0x0,(%rsi,%r15,1)
movzwl 0x3091(%rip),%eax
sub    %ebx,%eax
mov    %ax,0x3088(%rip)
add    %r14,%rsi
movswl %ax,%edx
mov    %r14,%rdi
call   1050 <memmove@plt>
movswq 0x3072(%rip),%rax
movb   $0x0,(%rax,%r14,1)
jmp    11e8 <get_sentence+0xa8>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@get_sentence.buf = internal global [256 x i8] zeroinitializer
@get_sentence.snt = internal global [256 x i8] zeroinitializer
@get_sentence.n = internal global i16 0
@.str = [2 x i8] c"$\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i8* @get_sentence(i32 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i16
store i32 %0, i32* %4
store i64 %1, i64* %5
%7 = load i32, i32* %4
%8 = load i16, i16* @get_sentence.n
%9 = load i64, i64* %5
%10 = call signext i16 @serial_read_string(i32 %7, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i32 256, i16 signext %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64 %9)
store i16 %10, i16* @get_sentence.n
%11 = load i16, i16* @get_sentence.n
%12 = sext i16 %11 to i32
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %40
14: ; preds = %2
%15 = call i8* @strstr(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds i8, i8* %15, i64 2
%17 = ptrtoint i8* %16 to i64
%18 = sub i64 %17, ptrtoint ([256 x i8]* @get_sentence.buf to i64)
%19 = trunc i64 %18 to i16
store i16 %19, i16* %6
%20 = load i16, i16* %6
%21 = call i32 @memcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.snt, i64 0, i64 0), i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i16 signext %20)
%22 = load i16, i16* %6
%23 = sext i16 %22 to i64
%24 = getelementptr inbounds [256 x i8], [256 x i8]* @get_sentence.snt, i64 0, i64 %23
store i8 0, i8* %24
%25 = load i16, i16* %6
%26 = sext i16 %25 to i32
%27 = load i16, i16* @get_sentence.n
%28 = sext i16 %27 to i32
%29 = sub nsw i32 %28, %26
%30 = trunc i32 %29 to i16
store i16 %30, i16* @get_sentence.n
%31 = load i16, i16* %6
%32 = sext i16 %31 to i32
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i64 %33
%35 = load i16, i16* @get_sentence.n
%36 = call i32 @memmove(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i8* %34, i16 signext %35)
%37 = load i16, i16* @get_sentence.n
%38 = sext i16 %37 to i64
%39 = getelementptr inbounds [256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 %38
store i8 0, i8* %39
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.snt, i64 0, i64 0), i8** %3
br label %41
40: ; preds = %2
store i8* null, i8** %3
br label %41
41: ; preds = %40, %14
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
@get_sentence.buf = internal global [256 x i8] zeroinitializer
@get_sentence.snt = internal global [256 x i8] zeroinitializer
@get_sentence.n = internal unnamed_addr global i16 0
@.str = [2 x i8] c"$\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i8* @get_sentence(i32 %0, i64 %1) {
%3 = load i16, i16* @get_sentence.n
%4 = tail call signext i16 @serial_read_string(i32 %0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i32 256, i16 signext %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64 %1)
store i16 %4, i16* @get_sentence.n
%5 = icmp sgt i16 %4, 0
br i1 %5, label %6, label %22
6: ; preds = %2
%7 = tail call i8* @strstr(i8* dereferenceable(1) getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = getelementptr inbounds i8, i8* %7, i64 2
%9 = ptrtoint i8* %8 to i64
%10 = trunc i64 %9 to i16
%11 = sub i16 %10, ptrtoint ([256 x i8]* @get_sentence.buf to i16)
%12 = tail call i32 @memcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.snt, i64 0, i64 0), i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i16 signext %11)
%13 = sext i16 %11 to i64
%14 = getelementptr inbounds [256 x i8], [256 x i8]* @get_sentence.snt, i64 0, i64 %13
store i8 0, i8* %14
%15 = load i16, i16* @get_sentence.n
%16 = sub i16 %15, %11
store i16 %16, i16* @get_sentence.n
%17 = getelementptr inbounds [256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 %13
%18 = tail call i32 @memmove(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 0), i8* %17, i16 signext %16)
%19 = load i16, i16* @get_sentence.n
%20 = sext i16 %19 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* @get_sentence.buf, i64 0, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %2, %6
%23 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @get_sentence.snt, i64 0, i64 0), %6 ], [ null, %2 ]
ret i8* %23
}