#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 Get32(int);
int uart_receive_str(char *buffer, unsigned int n) {
  unsigned int byte;
  int i;
  if (n == 0u) {
    return 0;
  }
  i = 0;
  n--;
  do {
    while ((Get32(0x20201018u) & (1u << 4u)) != 0)
      ;
    byte = Get32(0x20201000u) & 0xFFu;
    if (byte == 0u) {
      break;
    }
    *(buffer++) = (char)byte;
    i++;
  } while (--n != 0u);
  *buffer = 0;
  return i;
}


x86 assembly (clang, -O0):
<uart_receive_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1135 <uart_receive_str+0x25>
movl   $0x0,-0x4(%rbp)
jmp    11c4 <uart_receive_str+0xb4>
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    114a <uart_receive_str+0x3a>
mov    $0x20201018,%edi
call   1030 <Get32@plt>
and    $0x10,%eax
cmp    $0x0,%eax
je     1165 <uart_receive_str+0x55>
jmp    114a <uart_receive_str+0x3a>
mov    $0x20201000,%edi
call   1030 <Get32@plt>
and    $0xff,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1186 <uart_receive_str+0x76>
jmp    11b7 <uart_receive_str+0xa7>
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1145 <uart_receive_str+0x35>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_receive_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
je     1163 <uart_receive_str+0x53>
mov    %esi,%r14d
mov    %rdi,%rbx
add    $0xffffffff,%r14d
xor    %r15d,%r15d
mov    %r14d,%ebp
nopl   0x0(%rax,%rax,1)
mov    $0x20201018,%edi
call   1030 <Get32@plt>
test   $0x10,%al
jne    1130 <uart_receive_str+0x20>
mov    $0x20201000,%edi
call   1030 <Get32@plt>
test   %al,%al
je     115e <uart_receive_str+0x4e>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%r15d
add    $0xffffffff,%ebp
jne    1130 <uart_receive_str+0x20>
mov    %r14d,%r15d
movb   $0x0,(%rbx)
jmp    1166 <uart_receive_str+0x56>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @uart_receive_str(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 0, i32* %3
br label %40
11: ; preds = %2
store i32 0, i32* %7
%12 = load i32, i32* %5
%13 = add i32 %12, -1
store i32 %13, i32* %5
br label %14
14: ; preds = %33, %11
br label %15
15: ; preds = %19, %14
%16 = call i32 @Get32(i32 538972184)
%17 = and i32 %16, 16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
br label %15
20: ; preds = %15
%21 = call i32 @Get32(i32 538972160)
%22 = and i32 %21, 255
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
br label %37
26: ; preds = %20
%27 = load i32, i32* %6
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %4
store i8 %28, i8* %29
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %33
33: ; preds = %26
%34 = load i32, i32* %5
%35 = add i32 %34, -1
store i32 %35, i32* %5
%36 = icmp ne i32 %35, 0
br i1 %36, label %14, label %37
37: ; preds = %33, %25
%38 = load i8*, i8** %4
store i8 0, i8* %38
%39 = load i32, i32* %7
store i32 %39, i32* %3
br label %40
40: ; preds = %37, %10
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
define i32 @uart_receive_str(i8* nocapture writeonly %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %27, label %4
4: ; preds = %2
%5 = add i32 %1, -1
br label %6
6: ; preds = %18, %4
%7 = phi i8* [ %0, %4 ], [ %20, %18 ]
%8 = phi i32 [ %5, %4 ], [ %22, %18 ]
%9 = phi i32 [ 0, %4 ], [ %21, %18 ]
br label %10
10: ; preds = %10, %6
%11 = tail call i32 @Get32(i32 538972184)
%12 = and i32 %11, 16
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %10
14: ; preds = %10
%15 = tail call i32 @Get32(i32 538972160)
%16 = and i32 %15, 255
%17 = icmp eq i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %14
%19 = trunc i32 %15 to i8
%20 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %19, i8* %7
%21 = add nuw nsw i32 %9, 1
%22 = add i32 %8, -1
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %6
24: ; preds = %14, %18
%25 = phi i8* [ %7, %14 ], [ %20, %18 ]
%26 = phi i32 [ %9, %14 ], [ %5, %18 ]
store i8 0, i8* %25
br label %27
27: ; preds = %2, %24
%28 = phi i32 [ %26, %24 ], [ 0, %2 ]
ret i32 %28
}