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


x86 assembly (clang, -O0):
<GprsGetGSMCPAS>:
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   1090 <GprsM590eLock@plt>
lea    0xe3c(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%esi
lea    0xe2e(%rip),%rdi
call   1080 <GprsM590eWrite@plt>
cmpl   $0x190,-0x4(%rbp)
jge    125f <GprsGetGSMCPAS+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     123b <GprsGetGSMCPAS+0xbb>
cmpl   $0x800,-0x814(%rbp)
jb     1247 <GprsGetGSMCPAS+0xc7>
movl   $0x1,-0x8(%rbp)
jmp    125f <GprsGetGSMCPAS+0xdf>
mov    $0x1388,%edi
call   10a0 <usleep@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <GprsGetGSMCPAS+0x57>
mov    $0x0,%al
call   1050 <GprsM590eUnLock@plt>
mov    -0x8(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GprsGetGSMCPAS>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rsp,%r14
xor    %ebp,%ebp
mov    $0x800,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
xor    %eax,%eax
call   1090 <GprsM590eLock@plt>
lea    0xe4f(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <GprsM590eWrite@plt>
mov    $0x190,%r12d
mov    $0x1,%r15d
nop
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     120f <GprsGetGSMCPAS+0x8f>
add    %ebx,%ebp
cmp    $0x7ff,%ebp
ja     120f <GprsGetGSMCPAS+0x8f>
mov    $0x1388,%edi
call   10a0 <usleep@plt>
add    $0xffffffff,%r12d
jne    11d0 <GprsGetGSMCPAS+0x50>
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <GprsM590eUnLock@plt>
mov    %r15d,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"AT+CPAS=?\0D\00"
define i32 @GprsGetGSMCPAS() {
%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 ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @GprsM590eWrite(i8* getelementptr inbounds ([11 x i8], [11 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 = call i32 (...) @GprsM590eUnLock()
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@.str = [11 x i8] c"AT+CPAS=?\0D\00"
define i32 @GprsGetGSMCPAS() {
%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 ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @GprsM590eWrite(i8* getelementptr inbounds ([11 x i8], [11 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 %23, label %6
23: ; preds = %6, %19
%24 = phi i32 [ 0, %19 ], [ 1, %6 ]
%25 = call i32 (...) @GprsM590eUnLock()
ret i32 %24
}