#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 RS232_PollComport(int, unsigned char *, int);
int RS232_GetComCommand(int cport_nr, unsigned char *com_buf) {
  int n;
  int ret_val;
  static int bytes_so_far = 0;
  n = RS232_PollComport(cport_nr, com_buf, 4095);
  if (n > 0) {
    if (com_buf[n - 1] == '\n') {
      ret_val = bytes_so_far + n;
      if (bytes_so_far == 0) {
        com_buf[n - 1] = '\0';
      } else {
        com_buf -= bytes_so_far;
        bytes_so_far = 0;
      }
    } else {
      bytes_so_far += n;
      com_buf += n;
      ret_val = 0;
    }
  } else {
    ret_val = 0;
  }
  return ret_val;
}


x86 assembly (clang, -O0):
<RS232_GetComCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xfff,%edx
call   1030 <RS232_PollComport@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11da <RS232_GetComCommand+0xca>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11ae <RS232_GetComCommand+0x9e>
mov    0x2ecf(%rip),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,0x2ec2(%rip)
jne    1186 <RS232_GetComCommand+0x76>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a9 <RS232_GetComCommand+0x99>
mov    0x2ea0(%rip),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,0x2e83(%rip)
jmp    11d5 <RS232_GetComCommand+0xc5>
mov    -0x14(%rbp),%eax
add    0x2e75(%rip),%eax
mov    %eax,0x2e6f(%rip)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
jmp    11e1 <RS232_GetComCommand+0xd1>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RS232_GetComCommand>:
push   %rbx
mov    %rsi,%rbx
mov    $0xfff,%edx
call   1030 <RS232_PollComport@plt>
test   %eax,%eax
jle    1149 <RS232_GetComCommand+0x39>
lea    -0x1(%rax),%ecx
mov    0x2f01(%rip),%edx
add    %edx,%eax
cmpb   $0xa,(%rbx,%rcx,1)
jne    1143 <RS232_GetComCommand+0x33>
test   %edx,%edx
je     114d <RS232_GetComCommand+0x3d>
movl   $0x0,0x2eeb(%rip)
pop    %rbx
ret
mov    %eax,0x2ee3(%rip)
xor    %eax,%eax
pop    %rbx
ret
movb   $0x0,(%rbx,%rcx,1)
pop    %rbx
ret

LLVM IR (-O0):
@RS232_GetComCommand.bytes_so_far = internal global i32 0
define i32 @RS232_GetComCommand(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i32, i32* %3
%8 = load i8*, i8** %4
%9 = call i32 @RS232_PollComport(i32 %7, i8* %8, i32 4095)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %49
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* %5
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = icmp eq i32 %19, 10
br i1 %20, label %21, label %40
21: ; preds = %12
%22 = load i32, i32* @RS232_GetComCommand.bytes_so_far
%23 = load i32, i32* %5
%24 = add nsw i32 %22, %23
store i32 %24, i32* %6
%25 = load i32, i32* @RS232_GetComCommand.bytes_so_far
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = load i32, i32* %5
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 0, i8* %32
br label %39
33: ; preds = %21
%34 = load i32, i32* @RS232_GetComCommand.bytes_so_far
%35 = load i8*, i8** %4
%36 = sext i32 %34 to i64
%37 = sub i64 0, %36
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8* %38, i8** %4
store i32 0, i32* @RS232_GetComCommand.bytes_so_far
br label %39
39: ; preds = %33, %27
br label %48
40: ; preds = %12
%41 = load i32, i32* %5
%42 = load i32, i32* @RS232_GetComCommand.bytes_so_far
%43 = add nsw i32 %42, %41
store i32 %43, i32* @RS232_GetComCommand.bytes_so_far
%44 = load i32, i32* %5
%45 = load i8*, i8** %4
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8* %47, i8** %4
store i32 0, i32* %6
br label %48
48: ; preds = %40, %39
br label %50
49: ; preds = %2
store i32 0, i32* %6
br label %50
50: ; preds = %49, %48
%51 = load i32, i32* %6
ret i32 %51
}

LLVM IR (-O2):
@RS232_GetComCommand.bytes_so_far = internal unnamed_addr global i32 0
define i32 @RS232_GetComCommand(i32 %0, i8* %1) {
%3 = tail call i32 @RS232_PollComport(i32 %0, i8* %1, i32 4095)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %18
5: ; preds = %2
%6 = add nsw i32 %3, -1
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 10
%11 = load i32, i32* @RS232_GetComCommand.bytes_so_far
%12 = add nsw i32 %11, %3
br i1 %10, label %13, label %17
13: ; preds = %5
%14 = icmp eq i32 %11, 0
br i1 %14, label %15, label %16
15: ; preds = %13
store i8 0, i8* %8
br label %18
16: ; preds = %13
store i32 0, i32* @RS232_GetComCommand.bytes_so_far
br label %18
17: ; preds = %5
store i32 %12, i32* @RS232_GetComCommand.bytes_so_far
br label %18
18: ; preds = %2, %17, %16, %15
%19 = phi i32 [ %12, %15 ], [ %12, %16 ], [ 0, %17 ], [ 0, %2 ]
ret i32 %19
}