#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 AT_EXP_CREG(unsigned char *);
int AT_EXP_OK(unsigned char *);
int /*<<< orphan*/ GprsM590eLock();
scalar_t__ GprsM590eReadWithoutDelay(unsigned char *, int);
int /*<<< orphan*/ GprsM590eUnLock();
int /*<<< orphan*/ GprsM590eWrite(unsigned char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ usleep(int);
int GprsGetCREGStatus(void) {
  int count = 0;
  int ret = 0;
  unsigned char buffer[2048] = {0};
  unsigned int index = 0;
  GprsM590eLock();
  GprsM590eWrite((unsigned char *)"AT+CREG?\r", strlen("AT+CREG?\r"));
  while (count < 400) {
    index += GprsM590eReadWithoutDelay(&buffer[index], 2048 - index);
    if ((AT_EXP_OK(buffer) == 1) || (index >= 2048)) {
      ret = 1;
      break;
    }
    usleep(5000);
    count++;
  }
  if (ret) {
    ret = 0;
    if (AT_EXP_CREG(buffer) == 1)
      ret = 1;
  };
  GprsM590eUnLock();
  return ret;
}


x86 assembly (clang, -O0):
<GprsGetCREGStatus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
movl   $0x0,-0x814(%rbp)
mov    $0x0,%al
call   10a0 <GprsM590eLock@plt>
lea    0xe2c(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%esi
lea    0xe1e(%rip),%rdi
call   1090 <GprsM590eWrite@plt>
cmpl   $0x190,-0x4(%rbp)
jge    126f <GprsGetCREGStatus+0xdf>
mov    -0x814(%rbp),%eax
lea    -0x810(%rbp),%rdi
add    %rax,%rdi
mov    $0x800,%esi
sub    -0x814(%rbp),%esi
call   1030 <GprsM590eReadWithoutDelay@plt>
mov    %rax,%rcx
mov    -0x814(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x814(%rbp)
lea    -0x810(%rbp),%rdi
call   1060 <AT_EXP_OK@plt>
cmp    $0x1,%eax
je     124b <GprsGetCREGStatus+0xbb>
cmpl   $0x800,-0x814(%rbp)
jb     1257 <GprsGetCREGStatus+0xc7>
movl   $0x1,-0x8(%rbp)
jmp    126f <GprsGetCREGStatus+0xdf>
mov    $0x1388,%edi
call   10b0 <usleep@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <GprsGetCREGStatus+0x57>
cmpl   $0x0,-0x8(%rbp)
je     12a1 <GprsGetCREGStatus+0x111>
movl   $0x0,-0x8(%rbp)
lea    -0x810(%rbp),%rdi
call   1080 <AT_EXP_CREG@plt>
cmp    $0x1,%eax
jne    129c <GprsGetCREGStatus+0x10c>
movl   $0x1,-0x8(%rbp)
jmp    12a1 <GprsGetCREGStatus+0x111>
mov    $0x0,%al
call   1050 <GprsM590eUnLock@plt>
mov    -0x8(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GprsGetCREGStatus>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rsp,%r14
xor    %ebp,%ebp
mov    $0x800,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
xor    %eax,%eax
call   10a0 <GprsM590eLock@plt>
lea    0xe41(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <GprsM590eWrite@plt>
mov    $0x190,%r15d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%eax
lea    (%rsp,%rax,1),%rdi
mov    $0x800,%esi
sub    %ebp,%esi
call   1030 <GprsM590eReadWithoutDelay@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <AT_EXP_OK@plt>
cmp    $0x1,%eax
je     1220 <GprsGetCREGStatus+0x90>
add    %ebx,%ebp
cmp    $0x7ff,%ebp
ja     1220 <GprsGetCREGStatus+0x90>
mov    $0x1388,%edi
call   10b0 <usleep@plt>
add    $0xffffffff,%r15d
jne    11e0 <GprsGetCREGStatus+0x50>
xor    %ebx,%ebx
jmp    1230 <GprsGetCREGStatus+0xa0>
mov    %rsp,%rdi
call   1080 <AT_EXP_CREG@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
sete   %bl
xor    %eax,%eax
call   1050 <GprsM590eUnLock@plt>
mov    %ebx,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"AT+CREG?\0D\00"
define i32 @GprsGetCREGStatus() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2048 x i8]
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%5 = bitcast [2048 x i8]* %3 to i8*
store i32 0, i32* %4
%6 = call i32 (...) @GprsM590eLock()
%7 = call i32 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @GprsM590eWrite(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %30, %0
%10 = load i32, i32* %1
%11 = icmp slt i32 %10, 400
br i1 %11, label %12, label %34
12: ; preds = %9
%13 = load i32, i32* %4
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %14
%16 = load i32, i32* %4
%17 = sub i32 2048, %16
%18 = call i64 @GprsM590eReadWithoutDelay(i8* %15, i32 %17)
%19 = load i32, i32* %4
%20 = zext i32 %19 to i64
%21 = add nsw i64 %20, %18
%22 = trunc i64 %21 to i32
store i32 %22, i32* %4
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%24 = call i32 @AT_EXP_OK(i8* %23)
%25 = icmp eq i32 %24, 1
br i1 %25, label %29, label %26
26: ; preds = %12
%27 = load i32, i32* %4
%28 = icmp uge i32 %27, 2048
br i1 %28, label %29, label %30
29: ; preds = %26, %12
store i32 1, i32* %2
br label %34
30: ; preds = %26
%31 = call i32 @usleep(i32 5000)
%32 = load i32, i32* %1
%33 = add nsw i32 %32, 1
store i32 %33, i32* %1
br label %9
34: ; preds = %29, %9
%35 = load i32, i32* %2
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %34
store i32 0, i32* %2
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%39 = call i32 @AT_EXP_CREG(i8* %38)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 1, i32* %2
br label %42
42: ; preds = %41, %37
br label %43
43: ; preds = %42, %34
%44 = call i32 (...) @GprsM590eUnLock()
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@.str = [10 x i8] c"AT+CREG?\0D\00"
define i32 @GprsGetCREGStatus() {
%1 = alloca [2048 x i8]
%2 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @GprsM590eLock()
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @GprsM590eWrite(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %6
6: ; preds = %0, %19
%7 = phi i32 [ 0, %0 ], [ %14, %19 ]
%8 = phi i32 [ 0, %0 ], [ %21, %19 ]
%9 = zext i32 %7 to i64
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %9
%11 = sub i32 2048, %7
%12 = call i64 @GprsM590eReadWithoutDelay(i8* %10, i32 %11)
%13 = trunc i64 %12 to i32
%14 = add i32 %7, %13
%15 = call i32 @AT_EXP_OK(i8* %2)
%16 = icmp eq i32 %15, 1
%17 = icmp ugt i32 %14, 2047
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %23, label %19
19: ; preds = %6
%20 = call i32 @usleep(i32 5000)
%21 = add nuw nsw i32 %8, 1
%22 = icmp eq i32 %21, 400
br i1 %22, label %27, label %6
23: ; preds = %6
%24 = call i32 @AT_EXP_CREG(i8* %2)
%25 = icmp eq i32 %24, 1
%26 = zext i1 %25 to i32
br label %27
27: ; preds = %19, %23
%28 = phi i32 [ %26, %23 ], [ 0, %19 ]
%29 = call i32 (...) @GprsM590eUnLock()
ret i32 %28
}