#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);
int /*<<< orphan*/ usleep(int);
int GprsSetCGATT(bool state) {
  int count = 0;
  int ret = 0;
  unsigned char buffer[2048] = {0};
  unsigned int index = 0;
  GprsM590eLock();
  index = snprintf((char *)buffer, sizeof(buffer), "AT+CGATT=%d\r", state);
  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):
<GprsSetCGATT>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%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    -0x4(%rbp),%ecx
mov    $0x800,%esi
lea    0xe2a(%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,-0x8(%rbp)
jge    1298 <GprsSetCGATT+0x118>
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     1274 <GprsSetCGATT+0xf4>
cmpl   $0x800,-0x814(%rbp)
jb     1280 <GprsSetCGATT+0x100>
movl   $0x1,-0xc(%rbp)
jmp    1298 <GprsSetCGATT+0x118>
mov    $0x1388,%edi
call   10a0 <usleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1210 <GprsSetCGATT+0x90>
mov    $0x0,%al
call   1040 <GprsM590eUnLock@plt>
mov    -0xc(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GprsSetCGATT>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %edi,%ebx
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    0xe4d(%rip),%rdx
mov    %r14,%rdi
mov    $0x800,%esi
mov    %ebx,%ecx
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
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   1050 <AT_EXP_OK@plt>
cmp    $0x1,%eax
je     122f <GprsSetCGATT+0xaf>
add    %ebx,%ebp
cmp    $0x7ff,%ebp
ja     122f <GprsSetCGATT+0xaf>
mov    $0x1388,%edi
call   10a0 <usleep@plt>
add    $0xffffffff,%r12d
jne    11f0 <GprsSetCGATT+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 = [13 x i8] c"AT+CGATT=%d\0D\00"
define i32 @GprsSetCGATT(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2048 x i8]
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%7 = bitcast [2048 x i8]* %5 to i8*
store i32 0, i32* %6
%8 = call i32 (...) @GprsM590eLock()
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @snprintf(i8* %9, i32 2048, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%13 = load i32, i32* %6
%14 = call i32 @GprsM590eWrite(i8* %12, i32 %13)
store i32 0, i32* %6
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 2048)
br label %17
17: ; preds = %38, %1
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 400
br i1 %19, label %20, label %42
20: ; preds = %17
%21 = load i32, i32* %6
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %22
%24 = load i32, i32* %6
%25 = sub i32 2048, %24
%26 = call i64 @GprsM590eReadWithoutDelay(i8* %23, i32 %25)
%27 = load i32, i32* %6
%28 = zext i32 %27 to i64
%29 = add nsw i64 %28, %26
%30 = trunc i64 %29 to i32
store i32 %30, i32* %6
%31 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%32 = call i32 @AT_EXP_OK(i8* %31)
%33 = icmp eq i32 %32, 1
br i1 %33, label %37, label %34
34: ; preds = %20
%35 = load i32, i32* %6
%36 = icmp uge i32 %35, 2048
br i1 %36, label %37, label %38
37: ; preds = %34, %20
store i32 1, i32* %4
br label %42
38: ; preds = %34
%39 = call i32 @usleep(i32 5000)
%40 = load i32, i32* %3
%41 = add nsw i32 %40, 1
store i32 %41, i32* %3
br label %17
42: ; preds = %37, %17
%43 = call i32 (...) @GprsM590eUnLock()
%44 = load i32, i32* %4
ret i32 %44
}

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