#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 */
typedef int /*<<< orphan*/ buffer;

/* 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 *, unsigned int);
int /*<<< orphan*/ memset(unsigned char *, int, int);
unsigned int snprintf(char *, int, char *);
int /*<<< orphan*/ usleep(int);
int GprsGetCGATT(void) {
  int count = 0;
  int ret = 0;
  unsigned char buffer[2048] = {0};
  unsigned int index = 0;
  GprsM590eLock();
  index = snprintf((char *)buffer, sizeof(buffer), "AT+CGATT?");
  GprsM590eWrite(buffer, index);
  index = 0;
  memset(buffer, 0x0, sizeof(buffer));
  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):
<GprsGetCGATT>:
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    -0x810(%rbp),%rdi
mov    $0x800,%esi
lea    0xe30(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x814(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x814(%rbp),%esi
call   1080 <GprsM590eWrite@plt>
movl   $0x0,-0x814(%rbp)
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
cmpl   $0x190,-0x4(%rbp)
jge    1292 <GprsGetCGATT+0x112>
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   1050 <AT_EXP_OK@plt>
cmp    $0x1,%eax
je     126e <GprsGetCGATT+0xee>
cmpl   $0x800,-0x814(%rbp)
jb     127a <GprsGetCGATT+0xfa>
movl   $0x1,-0x8(%rbp)
jmp    1292 <GprsGetCGATT+0x112>
mov    $0x1388,%edi
call   10a0 <usleep@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    120a <GprsGetCGATT+0x8a>
mov    $0x0,%al
call   1040 <GprsM590eUnLock@plt>
mov    -0x8(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GprsGetCGATT>:
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),%rdx
mov    %r14,%rdi
mov    $0x800,%esi
call   1060 <snprintf@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1080 <GprsM590eWrite@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
mov    $0x190,%r12d
mov    $0x1,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
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   1050 <AT_EXP_OK@plt>
cmp    $0x1,%eax
je     122f <GprsGetCGATT+0xaf>
add    %ebx,%ebp
cmp    $0x7ff,%ebp
ja     122f <GprsGetCGATT+0xaf>
mov    $0x1388,%edi
call   10a0 <usleep@plt>
add    $0xffffffff,%r12d
jne    11f0 <GprsGetCGATT+0x70>
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <GprsM590eUnLock@plt>
mov    %r15d,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"AT+CGATT?\00"
define i32 @GprsGetCGATT() {
%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 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 2048, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 %8, i32* %4
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @GprsM590eWrite(i8* %9, i32 %10)
store i32 0, i32* %4
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 2048)
br label %14
14: ; preds = %35, %0
%15 = load i32, i32* %1
%16 = icmp slt i32 %15, 400
br i1 %16, label %17, label %39
17: ; preds = %14
%18 = load i32, i32* %4
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %19
%21 = load i32, i32* %4
%22 = sub i32 2048, %21
%23 = call i64 @GprsM590eReadWithoutDelay(i8* %20, i32 %22)
%24 = load i32, i32* %4
%25 = zext i32 %24 to i64
%26 = add nsw i64 %25, %23
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%29 = call i32 @AT_EXP_OK(i8* %28)
%30 = icmp eq i32 %29, 1
br i1 %30, label %34, label %31
31: ; preds = %17
%32 = load i32, i32* %4
%33 = icmp uge i32 %32, 2048
br i1 %33, label %34, label %35
34: ; preds = %31, %17
store i32 1, i32* %2
br label %39
35: ; preds = %31
%36 = call i32 @usleep(i32 5000)
%37 = load i32, i32* %1
%38 = add nsw i32 %37, 1
store i32 %38, i32* %1
br label %14
39: ; preds = %34, %14
%40 = call i32 (...) @GprsM590eUnLock()
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [10 x i8] c"AT+CGATT?\00"
define i32 @GprsGetCGATT() {
%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 = call i32 @snprintf(i8* %2, i32 2048, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @GprsM590eWrite(i8* %2, i32 %4)
%6 = call i32 @memset(i8* %2, i32 0, i32 2048)
br label %7
7: ; preds = %0, %20
%8 = phi i32 [ 0, %0 ], [ %15, %20 ]
%9 = phi i32 [ 0, %0 ], [ %22, %20 ]
%10 = zext i32 %8 to i64
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %10
%12 = sub i32 2048, %8
%13 = call i64 @GprsM590eReadWithoutDelay(i8* %11, i32 %12)
%14 = trunc i64 %13 to i32
%15 = add i32 %8, %14
%16 = call i32 @AT_EXP_OK(i8* %2)
%17 = icmp eq i32 %16, 1
%18 = icmp ugt i32 %15, 2047
%19 = select i1 %17, i1 true, i1 %18
br i1 %19, label %24, label %20
20: ; preds = %7
%21 = call i32 @usleep(i32 5000)
%22 = add nuw nsw i32 %9, 1
%23 = icmp eq i32 %22, 400
br i1 %23, label %24, label %7
24: ; preds = %7, %20
%25 = phi i32 [ 0, %20 ], [ 1, %7 ]
%26 = call i32 (...) @GprsM590eUnLock()
ret i32 %25
}