#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*/ reply;

/* Variables and functions */
char *huawei_receive();
int huawei_recv_error(char *);
int huawei_send(char const *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char const *);
scalar_t__ strncmp(char const *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *huawei_send_receive(const char *cmd) {
  int run, bytes_send, recv_seq;
  char *tmp;
  static char reply[256];
  bytes_send = huawei_send(cmd);
  strcpy(reply, "");
  if (bytes_send > 0) {
    recv_seq = 0;
    for (run = 0; run < 3; run++) {
      tmp = huawei_receive();
      if (strncmp(cmd, "ATE1 ^CURC=0;^DSFLOWCLR",
                  strlen("ATE1 ^CURC=0;^DSFLOWCLR")) == 0) {
        if (recv_seq == 0 && strncmp(tmp, "ATE1 ^CURC=0;^DSFLOWCLR",
                                     strlen("ATE1 ^CURC=0;^DSFLOWCLR")) == 0) {
          recv_seq = 1;
        } else if (recv_seq == 1 && strncmp(tmp, "OK", 2) == 0) {
          strncpy(reply, tmp, sizeof(reply) - 1);
          break;
        } else if (recv_seq == 1 && huawei_recv_error(tmp) == 1) {
          strncpy(reply, tmp, sizeof(reply) - 1);
          break;
        }
      } else {
        if (recv_seq == 0 && strncmp(tmp, cmd, strlen(cmd)) == 0) {
          recv_seq = 1;
        } else if (recv_seq == 1 && strlen(tmp) > 0 &&
                   huawei_recv_error(tmp) == 0) {
          recv_seq = 2;
          strncpy(reply, tmp, sizeof(reply) - 1);
        } else if (recv_seq == 2 && strncmp(tmp, "OK", 2) == 0) {
          break;
        } else if (recv_seq == 1 && huawei_recv_error(tmp) == 1) {
          strncpy(reply, tmp, sizeof(reply) - 1);
          break;
        }
      }
    }
  }
  return (reply);
}


x86 assembly (clang, -O0):
<huawei_send_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <huawei_send@plt>
mov    %eax,-0x10(%rbp)
lea    0x2ee1(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1050 <strcpy@plt>
cmpl   $0x0,-0x10(%rbp)
jle    13ee <huawei_send_receive+0x27e>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    13e9 <huawei_send_receive+0x279>
mov    $0x0,%al
call   1060 <huawei_receive@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0xe29(%rip),%rdi
call   1070 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe17(%rip),%rsi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12c6 <huawei_send_receive+0x156>
cmpl   $0x0,-0x14(%rbp)
jne    123e <huawei_send_receive+0xce>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xdef(%rip),%rdi
call   1070 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xddd(%rip),%rsi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    123e <huawei_send_receive+0xce>
movl   $0x1,-0x14(%rbp)
jmp    12c1 <huawei_send_receive+0x151>
cmpl   $0x1,-0x14(%rbp)
jne    1281 <huawei_send_receive+0x111>
mov    -0x20(%rbp),%rdi
lea    0xdc5(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1281 <huawei_send_receive+0x111>
mov    -0x20(%rbp),%rsi
lea    0x2dfe(%rip),%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
jmp    13e9 <huawei_send_receive+0x279>
cmpl   $0x1,-0x14(%rbp)
jne    12b7 <huawei_send_receive+0x147>
mov    -0x20(%rbp),%rdi
call   1080 <huawei_recv_error@plt>
cmp    $0x1,%eax
jne    12b7 <huawei_send_receive+0x147>
mov    -0x20(%rbp),%rsi
lea    0x2dc8(%rip),%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
jmp    13e9 <huawei_send_receive+0x279>
jmp    12bc <huawei_send_receive+0x14c>
jmp    12c1 <huawei_send_receive+0x151>
jmp    13d6 <huawei_send_receive+0x266>
cmpl   $0x0,-0x14(%rbp)
jne    130e <huawei_send_receive+0x19e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    130e <huawei_send_receive+0x19e>
movl   $0x1,-0x14(%rbp)
jmp    13d1 <huawei_send_receive+0x261>
cmpl   $0x1,-0x14(%rbp)
jne    135e <huawei_send_receive+0x1ee>
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x0,%rax
jle    135e <huawei_send_receive+0x1ee>
mov    -0x20(%rbp),%rdi
call   1080 <huawei_recv_error@plt>
cmp    $0x0,%eax
jne    135e <huawei_send_receive+0x1ee>
movl   $0x2,-0x14(%rbp)
mov    -0x20(%rbp),%rsi
lea    0x2d21(%rip),%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
jmp    13cc <huawei_send_receive+0x25c>
cmpl   $0x2,-0x14(%rbp)
jne    138c <huawei_send_receive+0x21c>
mov    -0x20(%rbp),%rdi
lea    0xca5(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    138c <huawei_send_receive+0x21c>
jmp    13e9 <huawei_send_receive+0x279>
cmpl   $0x1,-0x14(%rbp)
jne    13c2 <huawei_send_receive+0x252>
mov    -0x20(%rbp),%rdi
call   1080 <huawei_recv_error@plt>
cmp    $0x1,%eax
jne    13c2 <huawei_send_receive+0x252>
mov    -0x20(%rbp),%rsi
lea    0x2cbd(%rip),%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
jmp    13e9 <huawei_send_receive+0x279>
jmp    13c7 <huawei_send_receive+0x257>
jmp    13cc <huawei_send_receive+0x25c>
jmp    13d1 <huawei_send_receive+0x261>
jmp    13d6 <huawei_send_receive+0x266>
jmp    13db <huawei_send_receive+0x26b>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b3 <huawei_send_receive+0x43>
jmp    13ee <huawei_send_receive+0x27e>
lea    0x2c7b(%rip),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<huawei_send_receive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
call   1090 <huawei_send@plt>
mov    %eax,%ebx
lea    0x2ee4(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1050 <strcpy@plt>
test   %ebx,%ebx
jle    12cf <huawei_send_receive+0x15f>
mov    $0x3,%ebp
lea    0xe54(%rip),%r14
lea    0xe65(%rip),%r15
xor    %r12d,%r12d
jmp    11e9 <huawei_send_receive+0x79>
mov    $0x2,%r12d
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     12cf <huawei_send_receive+0x15f>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%ebp
je     12cf <huawei_send_receive+0x15f>
xor    %eax,%eax
call   1060 <huawei_receive@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x17,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1230 <huawei_send_receive+0xc0>
cmp    $0x2,%r12d
je     11b8 <huawei_send_receive+0x48>
cmp    $0x1,%r12d
je     1257 <huawei_send_receive+0xe7>
test   %r12d,%r12d
jne    11e0 <huawei_send_receive+0x70>
mov    %r13,%rdi
call   1070 <strlen@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %eax,%edx
jmp    1246 <huawei_send_receive+0xd6>
nopl   0x0(%rax,%rax,1)
cmp    $0x1,%r12d
je     128f <huawei_send_receive+0x11f>
test   %r12d,%r12d
jne    11e0 <huawei_send_receive+0x70>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x17,%edx
call   1040 <strncmp@plt>
xor    %r12d,%r12d
test   %rax,%rax
sete   %r12b
jmp    11e0 <huawei_send_receive+0x70>
mov    %rbx,%rdi
call   1070 <strlen@plt>
test   %rax,%rax
jle    12a4 <huawei_send_receive+0x134>
mov    %rbx,%rdi
call   1080 <huawei_recv_error@plt>
test   %eax,%eax
jne    12a4 <huawei_send_receive+0x134>
lea    0x2df9(%rip),%rdi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
mov    $0x2,%r12d
jmp    11e0 <huawei_send_receive+0x70>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     12bb <huawei_send_receive+0x14b>
mov    %rbx,%rdi
call   1080 <huawei_recv_error@plt>
mov    $0x1,%r12d
cmp    $0x1,%eax
jne    11e0 <huawei_send_receive+0x70>
lea    0x2dae(%rip),%rdi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
lea    0x2d9a(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@huawei_send_receive.reply = internal global [256 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [24 x i8] c"ATE1 ^CURC=0;^DSFLOWCLR\00"
@.str.2 = [3 x i8] c"OK\00"
define i8* @huawei_send_receive(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @huawei_send(i8* %7)
store i32 %8, i32* %4
%9 = call i32 @strcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %109
12: ; preds = %1
store i32 0, i32* %5
store i32 0, i32* %3
br label %13
13: ; preds = %105, %12
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %108
16: ; preds = %13
%17 = call i8* (...) @huawei_receive()
store i8* %17, i8** %6
%18 = load i8*, i8** %2
%19 = call i64 @strlen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%20 = trunc i64 %19 to i32
%21 = call i64 @strncmp(i8* %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %56
23: ; preds = %16
%24 = load i32, i32* %5
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = call i64 @strlen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%29 = trunc i64 %28 to i32
%30 = call i64 @strncmp(i8* %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
store i32 1, i32* %5
br label %55
33: ; preds = %26, %23
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i8*, i8** %6
%38 = call i64 @strncmp(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i8*, i8** %6
%42 = call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0), i8* %41, i32 255)
br label %108
43: ; preds = %36, %33
%44 = load i32, i32* %5
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = load i8*, i8** %6
%48 = call i32 @huawei_recv_error(i8* %47)
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i8*, i8** %6
%52 = call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0), i8* %51, i32 255)
br label %108
53: ; preds = %46, %43
br label %54
54: ; preds = %53
br label %55
55: ; preds = %54, %32
br label %104
56: ; preds = %16
%57 = load i32, i32* %5
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %68
59: ; preds = %56
%60 = load i8*, i8** %6
%61 = load i8*, i8** %2
%62 = load i8*, i8** %2
%63 = call i64 @strlen(i8* %62)
%64 = trunc i64 %63 to i32
%65 = call i64 @strncmp(i8* %60, i8* %61, i32 %64)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %59
store i32 1, i32* %5
br label %103
68: ; preds = %59, %56
%69 = load i32, i32* %5
%70 = icmp eq i32 %69, 1
br i1 %70, label %71, label %82
71: ; preds = %68
%72 = load i8*, i8** %6
%73 = call i64 @strlen(i8* %72)
%74 = icmp sgt i64 %73, 0
br i1 %74, label %75, label %82
75: ; preds = %71
%76 = load i8*, i8** %6
%77 = call i32 @huawei_recv_error(i8* %76)
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %75
store i32 2, i32* %5
%80 = load i8*, i8** %6
%81 = call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0), i8* %80, i32 255)
br label %102
82: ; preds = %75, %71, %68
%83 = load i32, i32* %5
%84 = icmp eq i32 %83, 2
br i1 %84, label %85, label %90
85: ; preds = %82
%86 = load i8*, i8** %6
%87 = call i64 @strncmp(i8* %86, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%88 = icmp eq i64 %87, 0
br i1 %88, label %89, label %90
89: ; preds = %85
br label %108
90: ; preds = %85, %82
%91 = load i32, i32* %5
%92 = icmp eq i32 %91, 1
br i1 %92, label %93, label %100
93: ; preds = %90
%94 = load i8*, i8** %6
%95 = call i32 @huawei_recv_error(i8* %94)
%96 = icmp eq i32 %95, 1
br i1 %96, label %97, label %100
97: ; preds = %93
%98 = load i8*, i8** %6
%99 = call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0), i8* %98, i32 255)
br label %108
100: ; preds = %93, %90
br label %101
101: ; preds = %100
br label %102
102: ; preds = %101, %79
br label %103
103: ; preds = %102, %67
br label %104
104: ; preds = %103, %55
br label %105
105: ; preds = %104
%106 = load i32, i32* %3
%107 = add nsw i32 %106, 1
store i32 %107, i32* %3
br label %13
108: ; preds = %97, %89, %50, %40, %13
br label %109
109: ; preds = %108, %1
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0)
}

LLVM IR (-O2):
@huawei_send_receive.reply = internal global [256 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [24 x i8] c"ATE1 ^CURC=0;^DSFLOWCLR\00"
@.str.2 = [3 x i8] c"OK\00"
define i8* @huawei_send_receive(i8* %0) {
%2 = tail call i32 @huawei_send(i8* %0)
%3 = tail call i32 @strcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %49
5: ; preds = %1, %43
%6 = phi i32 [ %45, %43 ], [ 0, %1 ]
%7 = phi i32 [ %44, %43 ], [ 0, %1 ]
%8 = tail call i8* (...) @huawei_receive()
%9 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 23)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %5
switch i32 %7, label %43 [
i32 0, label %12
i32 1, label %16
]
12: ; preds = %11
%13 = tail call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 23)
%14 = icmp eq i64 %13, 0
%15 = zext i1 %14 to i32
br label %43
16: ; preds = %11
%17 = tail call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %47, label %19
19: ; preds = %16
%20 = tail call i32 @huawei_recv_error(i8* %8)
%21 = icmp eq i32 %20, 1
br i1 %21, label %47, label %43
22: ; preds = %5
switch i32 %7, label %43 [
i32 0, label %23
i32 1, label %29
i32 2, label %37
]
23: ; preds = %22
%24 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%25 = trunc i64 %24 to i32
%26 = tail call i64 @strncmp(i8* %8, i8* %0, i32 %25)
%27 = icmp eq i64 %26, 0
%28 = zext i1 %27 to i32
br label %43
29: ; preds = %22
%30 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %29
%33 = tail call i32 @huawei_recv_error(i8* %8)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = tail call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0), i8* %8, i32 255)
br label %43
37: ; preds = %22
%38 = tail call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%39 = icmp eq i64 %38, 0
br i1 %39, label %49, label %43
40: ; preds = %29, %32
%41 = tail call i32 @huawei_recv_error(i8* %8)
%42 = icmp eq i32 %41, 1
br i1 %42, label %47, label %43
43: ; preds = %23, %12, %22, %11, %37, %19, %35, %40
%44 = phi i32 [ 1, %19 ], [ 2, %35 ], [ 1, %40 ], [ 2, %37 ], [ %15, %12 ], [ %7, %11 ], [ %7, %22 ], [ %28, %23 ]
%45 = add nuw nsw i32 %6, 1
%46 = icmp eq i32 %45, 3
br i1 %46, label %49, label %5
47: ; preds = %40, %19, %16
%48 = tail call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0), i8* %8, i32 255)
br label %49
49: ; preds = %43, %37, %47, %1
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @huawei_send_receive.reply, i64 0, i64 0)
}