#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 *);
scalar_t__ AT_EXP_READY(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 GprsGetGSMCPIN(void) {
  int count = 0;
  int ret = 0;
  unsigned char buffer[2048] = {0};
  unsigned int index = 0;
  GprsM590eLock();
  GprsM590eWrite((unsigned char *)"AT+CPIN?\r", strlen("AT+CPIN?\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) {
    if (AT_EXP_READY(buffer))
      ret = 1;
    else
      ret = 0;
  };
  GprsM590eUnLock();
  return ret;
}


x86 assembly (clang, -O0):
<GprsGetGSMCPIN>:
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 <GprsGetGSMCPIN+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 <GprsGetGSMCPIN+0xbb>
cmpl   $0x800,-0x814(%rbp)
jb     1257 <GprsGetGSMCPIN+0xc7>
movl   $0x1,-0x8(%rbp)
jmp    126f <GprsGetGSMCPIN+0xdf>
mov    $0x1388,%edi
call   10b0 <usleep@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <GprsGetGSMCPIN+0x57>
cmpl   $0x0,-0x8(%rbp)
je     12a7 <GprsGetGSMCPIN+0x117>
lea    -0x810(%rbp),%rdi
call   1080 <AT_EXP_READY@plt>
cmp    $0x0,%rax
je     129b <GprsGetGSMCPIN+0x10b>
movl   $0x1,-0x8(%rbp)
jmp    12a2 <GprsGetGSMCPIN+0x112>
movl   $0x0,-0x8(%rbp)
jmp    12a7 <GprsGetGSMCPIN+0x117>
mov    $0x0,%al
call   1050 <GprsM590eUnLock@plt>
mov    -0x8(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GprsGetGSMCPIN>:
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 <GprsGetGSMCPIN+0x90>
add    %ebx,%ebp
cmp    $0x7ff,%ebp
ja     1220 <GprsGetGSMCPIN+0x90>
mov    $0x1388,%edi
call   10b0 <usleep@plt>
add    $0xffffffff,%r15d
jne    11e0 <GprsGetGSMCPIN+0x50>
xor    %ebx,%ebx
jmp    1230 <GprsGetGSMCPIN+0xa0>
mov    %rsp,%rdi
call   1080 <AT_EXP_READY@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %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+CPIN?\0D\00"
define i32 @GprsGetGSMCPIN() {
%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 %44
37: ; preds = %34
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%39 = call i64 @AT_EXP_READY(i8* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 1, i32* %2
br label %43
42: ; preds = %37
store i32 0, i32* %2
br label %43
43: ; preds = %42, %41
br label %44
44: ; preds = %43, %34
%45 = call i32 (...) @GprsM590eUnLock()
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [10 x i8] c"AT+CPIN?\0D\00"
define i32 @GprsGetGSMCPIN() {
%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 i64 @AT_EXP_READY(i8* %2)
%25 = icmp ne i64 %24, 0
%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
}