#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 */
char receive_char_by_UART();
int receive_str_by_UART(char *buffer, int length) {
  int count = 0;
  char c;
  while (count < length - 1) {
    c = receive_char_by_UART();
    buffer[count++] = c;
    if (c == '\0')
      return 1;
  }
  buffer[count] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<receive_str_by_UART>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1177 <receive_str_by_UART+0x67>
mov    $0x0,%al
call   1030 <receive_char_by_UART@plt>
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jne    1172 <receive_str_by_UART+0x62>
movl   $0x1,-0x4(%rbp)
jmp    118a <receive_str_by_UART+0x7a>
jmp    1126 <receive_str_by_UART+0x16>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_str_by_UART>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
add    $0xffffffff,%r15d
xor    %eax,%eax
test   %r15d,%r15d
cmovle %eax,%r15d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
cmp    %rbx,%r15
je     114f <receive_str_by_UART+0x3f>
xor    %eax,%eax
call   1030 <receive_char_by_UART@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
test   %al,%al
jne    1130 <receive_str_by_UART+0x20>
mov    $0x1,%eax
jmp    1156 <receive_str_by_UART+0x46>
movb   $0x0,(%r14,%r15,1)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @receive_str_by_UART(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %25, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = sub nsw i32 %10, 1
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %26
13: ; preds = %8
%14 = call signext i8 (...) @receive_char_by_UART()
store i8 %14, i8* %7
%15 = load i8, i8* %7
%16 = load i8*, i8** %4
%17 = load i32, i32* %6
%18 = add nsw i32 %17, 1
store i32 %18, i32* %6
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
store i8 %15, i8* %20
%21 = load i8, i8* %7
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %13
store i32 1, i32* %3
br label %31
25: ; preds = %13
br label %8
26: ; preds = %8
%27 = load i8*, i8** %4
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
store i32 0, i32* %3
br label %31
31: ; preds = %26, %24
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
define i32 @receive_str_by_UART(i8* nocapture writeonly %0, i32 %1) {
%3 = add i32 %1, -1
%4 = call i32 @llvm.smax.i32(i32 %3, i32 0)
%5 = zext i32 %4 to i64
br label %6
6: ; preds = %9, %2
%7 = phi i64 [ %11, %9 ], [ 0, %2 ]
%8 = icmp eq i64 %7, %5
br i1 %8, label %14, label %9
9: ; preds = %6
%10 = tail call signext i8 (...) @receive_char_by_UART()
%11 = add nuw nsw i64 %7, 1
%12 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 %10, i8* %12
%13 = icmp eq i8 %10, 0
br i1 %13, label %16, label %6
14: ; preds = %6
%15 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 0, i8* %15
br label %16
16: ; preds = %9, %14
%17 = phi i32 [ 0, %14 ], [ 1, %9 ]
ret i32 %17
}