#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 */
scalar_t__ ETIME;
scalar_t__ Modem_SendCmd(int, char *, int);
scalar_t__ SMS_GetPhoneNum(char *, char *);
scalar_t__ errno;
int /*<<< orphan*/ logcat(char *, ...);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memcpy(char *, char *, scalar_t__);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int readn(int, char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ strlen(char *);
char *strsep(char **, char *const);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ usleep(int);
int SMS_ReadMessage(int fd, int index, char *data, char *phone) {
  char rbuf[512];
  char cmd[256];
  char *p = NULL, *sp = NULL;
  char *const delim = "\n\n";
  int nread;
  int i;
  int retry = 5;
SMS_Retry:
  memset(cmd, 0, 256);
  sprintf(cmd, "at+cmgr=%d\r", index);
  if (Modem_SendCmd(fd, cmd, 5) < 0) {
    logcat("SMS Send CMD error.\n");
    return -1;
  }
  usleep(400 * 1000);
  nread = 0;
  for (i = 0; i < 5; i++) {
    memset(rbuf, 0, 512);
    nread = readn(fd, (rbuf + nread), 512, 2);
    if (nread < 0) {
      if (errno == ETIME)
        continue;
      else {
        logcat("Modem read uart: %s\n", strerror(errno));
        return -1;
      }
    }
    if (strstr(rbuf, "OK")) {
      break;
    }
    if (strstr(rbuf, "ERROR")) {
      logcat("Modem: read message error.\n");
      return -1;
    }
  }
  if (i == 5) {
    if (retry > 0) {
      retry--;
      sleep(2);
      goto SMS_Retry;
    }
    return -1;
  }
  sp = rbuf;
  while ((p = strsep(&sp, delim)) != NULL) {
    if (memcmp(p, "+CMGR", 5) == 0) {
      break;
    }
  }
  if (p == NULL)
    return -1;
  if (memcmp(p, "+CMGR", 5) == 0) {
    if (SMS_GetPhoneNum(p, phone) < 0) {
      logcat("SMS Get Phone Number Error.\n");
      return -1;
    }
    while ((p = strsep(&sp, delim)) != NULL) {
      if (strlen(p) > 0) {
        break;
      }
    }
    if (p == NULL) {
      logcat("Message Data is NULL.\n");
    } else {
      memcpy(data, p, strlen(p));
    }
    while ((p = strsep(&sp, delim)) != NULL) {
      if (strlen(p) > 0) {
        break;
      }
    }
    if (p != NULL) {
      if (memcmp(p, "OK", 2) != 0)
        return -1;
    } else
      return -1;
  } else
    return -1;
  logcat("SMS Read Message: phone = %s, data = %s \n", phone, data);
  return 0;
}


x86 assembly (clang, -O0):
<SMS_ReadMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x360,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x328(%rbp)
movq   $0x0,-0x330(%rbp)
lea    0xdea(%rip),%rax
mov    %rax,-0x338(%rbp)
movl   $0x5,-0x344(%rbp)
lea    -0x320(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x320(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xdb8(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x320(%rbp),%rsi
mov    $0x5,%edx
call   1090 <Modem_SendCmd@plt>
cmp    $0x0,%rax
jge    1288 <SMS_ReadMessage+0xa8>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
mov    $0x61a80,%edi
call   1100 <usleep@plt>
movl   $0x0,-0x33c(%rbp)
movl   $0x0,-0x340(%rbp)
cmpl   $0x5,-0x340(%rbp)
jge    13b8 <SMS_ReadMessage+0x1d8>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
movslq -0x33c(%rbp),%rax
add    %rax,%rsi
mov    $0x200,%edx
mov    $0x2,%ecx
call   1060 <readn@plt>
mov    %eax,-0x33c(%rbp)
cmpl   $0x0,-0x33c(%rbp)
jge    1346 <SMS_ReadMessage+0x166>
mov    0x2cd5(%rip),%rax
mov    (%rax),%rax
mov    0x2cdb(%rip),%rcx
cmp    (%rcx),%rax
jne    131b <SMS_ReadMessage+0x13b>
jmp    13a4 <SMS_ReadMessage+0x1c4>
mov    0x2cb6(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    %eax,%esi
lea    0xcf1(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
lea    -0x220(%rbp),%rdi
lea    0xce5(%rip),%rsi
call   10f0 <strstr@plt>
cmp    $0x0,%rax
je     1368 <SMS_ReadMessage+0x188>
jmp    13b8 <SMS_ReadMessage+0x1d8>
lea    -0x220(%rbp),%rdi
lea    0xcc6(%rip),%rsi
call   10f0 <strstr@plt>
cmp    $0x0,%rax
je     139f <SMS_ReadMessage+0x1bf>
lea    0xcb6(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
jmp    13a4 <SMS_ReadMessage+0x1c4>
mov    -0x340(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x340(%rbp)
jmp    12a6 <SMS_ReadMessage+0xc6>
cmpl   $0x5,-0x340(%rbp)
jne    13fc <SMS_ReadMessage+0x21c>
cmpl   $0x0,-0x344(%rbp)
jle    13f0 <SMS_ReadMessage+0x210>
mov    -0x344(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x344(%rbp)
mov    $0x2,%edi
call   10e0 <sleep@plt>
jmp    1227 <SMS_ReadMessage+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
lea    -0x220(%rbp),%rax
mov    %rax,-0x330(%rbp)
lea    -0x330(%rbp),%rdi
lea    0xbe8(%rip),%rsi
call   10b0 <strsep@plt>
mov    %rax,-0x328(%rbp)
cmp    $0x0,%rax
je     145a <SMS_ReadMessage+0x27a>
mov    -0x328(%rbp),%rdi
lea    0xc22(%rip),%rsi
mov    $0x5,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    1455 <SMS_ReadMessage+0x275>
jmp    145a <SMS_ReadMessage+0x27a>
jmp    140a <SMS_ReadMessage+0x22a>
cmpq   $0x0,-0x328(%rbp)
jne    1474 <SMS_ReadMessage+0x294>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
mov    -0x328(%rbp),%rdi
lea    0xbdc(%rip),%rsi
mov    $0x5,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    160a <SMS_ReadMessage+0x42a>
mov    -0x328(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <SMS_GetPhoneNum@plt>
cmp    $0x0,%rax
jge    14ca <SMS_ReadMessage+0x2ea>
lea    0xbad(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
jmp    14cf <SMS_ReadMessage+0x2ef>
lea    -0x330(%rbp),%rdi
lea    0xb23(%rip),%rsi
call   10b0 <strsep@plt>
mov    %rax,-0x328(%rbp)
cmp    $0x0,%rax
je     1513 <SMS_ReadMessage+0x333>
mov    -0x328(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jle    150e <SMS_ReadMessage+0x32e>
jmp    1513 <SMS_ReadMessage+0x333>
jmp    14cf <SMS_ReadMessage+0x2ef>
cmpq   $0x0,-0x328(%rbp)
jne    1534 <SMS_ReadMessage+0x354>
lea    0xb59(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
jmp    156f <SMS_ReadMessage+0x38f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x358(%rbp)
mov    -0x328(%rbp),%rax
mov    %rax,-0x350(%rbp)
mov    -0x328(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x358(%rbp),%rdi
mov    -0x350(%rbp),%rsi
mov    %rax,%rdx
call   10a0 <memcpy@plt>
jmp    1574 <SMS_ReadMessage+0x394>
lea    -0x330(%rbp),%rdi
lea    0xa7e(%rip),%rsi
call   10b0 <strsep@plt>
mov    %rax,-0x328(%rbp)
cmp    $0x0,%rax
je     15b8 <SMS_ReadMessage+0x3d8>
mov    -0x328(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jle    15b3 <SMS_ReadMessage+0x3d3>
jmp    15b8 <SMS_ReadMessage+0x3d8>
jmp    1574 <SMS_ReadMessage+0x394>
cmpq   $0x0,-0x328(%rbp)
je     15f9 <SMS_ReadMessage+0x419>
mov    -0x328(%rbp),%rdi
lea    0xa65(%rip),%rsi
mov    $0x2,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
je     15f4 <SMS_ReadMessage+0x414>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
jmp    1605 <SMS_ReadMessage+0x425>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
jmp    1616 <SMS_ReadMessage+0x436>
movl   $0xffffffff,-0x4(%rbp)
jmp    1633 <SMS_ReadMessage+0x453>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xa73(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x360,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SMS_ReadMessage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x328,%rsp
mov    %rcx,0x18(%rsp)
mov    %rdx,0x10(%rsp)
mov    %esi,%ebp
mov    %edi,%r12d
movq   $0x0,(%rsp)
lea    0x220(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    0xddd(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,0xc(%rsp)
mov    %ebp,%edx
call   10c0 <sprintf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x5,%edx
call   1090 <Modem_SendCmd@plt>
test   %rax,%rax
js     1540 <SMS_ReadMessage+0x360>
mov    $0xfffffffa,%r14d
lea    0x20(%rsp),%rbp
lea    0xdda(%rip),%r15
mov    $0x61a80,%edi
call   1100 <usleep@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
mov    $0x2,%ecx
call   1060 <readn@plt>
mov    %eax,%r13d
test   %eax,%eax
js     12d0 <SMS_ReadMessage+0xf0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
jne    154c <SMS_ReadMessage+0x36c>
mov    %rbp,%rdi
lea    0xd8a(%rip),%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
je     12ea <SMS_ReadMessage+0x10a>
jmp    1612 <SMS_ReadMessage+0x432>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2d01(%rip),%rax
mov    (%rax),%rdi
mov    0x2d07(%rip),%rax
cmp    (%rax),%rdi
jne    1639 <SMS_ReadMessage+0x459>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
movslq %r13d,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    %r12d,%edi
mov    $0x200,%edx
mov    $0x2,%ecx
call   1060 <readn@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1350 <SMS_ReadMessage+0x170>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
jne    154c <SMS_ReadMessage+0x36c>
mov    %rbp,%rdi
lea    0xd02(%rip),%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
je     136a <SMS_ReadMessage+0x18a>
jmp    1612 <SMS_ReadMessage+0x432>
nopl   0x0(%rax)
mov    0x2c81(%rip),%rax
mov    (%rax),%rdi
mov    0x2c87(%rip),%rax
cmp    (%rax),%rdi
jne    1639 <SMS_ReadMessage+0x459>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    %r12d,%edi
mov    $0x200,%edx
mov    $0x2,%ecx
call   1060 <readn@plt>
mov    %eax,%ebx
test   %eax,%eax
js     13d0 <SMS_ReadMessage+0x1f0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
jne    154c <SMS_ReadMessage+0x36c>
mov    %rbp,%rdi
lea    0xc82(%rip),%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
je     13ea <SMS_ReadMessage+0x20a>
jmp    1612 <SMS_ReadMessage+0x432>
nopl   0x0(%rax)
mov    0x2c01(%rip),%rax
mov    (%rax),%rdi
mov    0x2c07(%rip),%rax
cmp    (%rax),%rdi
jne    1639 <SMS_ReadMessage+0x459>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    %r12d,%edi
mov    $0x200,%edx
mov    $0x2,%ecx
call   1060 <readn@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1450 <SMS_ReadMessage+0x270>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
jne    154c <SMS_ReadMessage+0x36c>
mov    %rbp,%rdi
lea    0xc02(%rip),%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
je     146a <SMS_ReadMessage+0x28a>
jmp    1612 <SMS_ReadMessage+0x432>
nopl   0x0(%rax)
mov    0x2b81(%rip),%rax
mov    (%rax),%rdi
mov    0x2b87(%rip),%rax
cmp    (%rax),%rdi
jne    1639 <SMS_ReadMessage+0x459>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    %r12d,%edi
mov    $0x200,%edx
mov    $0x2,%ecx
call   1060 <readn@plt>
test   %eax,%eax
js     14d0 <SMS_ReadMessage+0x2f0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
jne    154c <SMS_ReadMessage+0x36c>
mov    %rbp,%rdi
lea    0xb84(%rip),%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
je     14ea <SMS_ReadMessage+0x30a>
jmp    1612 <SMS_ReadMessage+0x432>
nopw   0x0(%rax,%rax,1)
mov    0x2b01(%rip),%rax
mov    (%rax),%rdi
mov    0x2b07(%rip),%rax
cmp    (%rax),%rdi
jne    1639 <SMS_ReadMessage+0x459>
inc    %r14d
je     1620 <SMS_ReadMessage+0x440>
mov    $0x2,%edi
call   10e0 <sleep@plt>
lea    0x220(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
lea    0xae5(%rip),%rsi
mov    0xc(%rsp),%edx
call   10c0 <sprintf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x5,%edx
call   1090 <Modem_SendCmd@plt>
test   %rax,%rax
jns    125f <SMS_ReadMessage+0x7f>
lea    0xac8(%rip),%rdi
jmp    1619 <SMS_ReadMessage+0x439>
mov    %rbp,(%rsp)
lea    0xaa9(%rip),%r14
mov    %rsp,%r15
lea    0xafd(%rip),%r12
mov    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <strsep@plt>
test   %rax,%rax
je     1625 <SMS_ReadMessage+0x445>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x5,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    1570 <SMS_ReadMessage+0x390>
lea    0xabb(%rip),%rsi
mov    %rbp,%rdi
mov    $0x5,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    1625 <SMS_ReadMessage+0x445>
mov    %rbp,%rdi
mov    0x18(%rsp),%r12
mov    %r12,%rsi
call   1050 <SMS_GetPhoneNum@plt>
test   %rax,%rax
js     1650 <SMS_ReadMessage+0x470>
lea    0xa2b(%rip),%r14
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <strsep@plt>
test   %rax,%rax
je     1660 <SMS_ReadMessage+0x480>
mov    %rax,%rbp
mov    %rax,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
jle    15e0 <SMS_ReadMessage+0x400>
mov    0x10(%rsp),%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   10a0 <memcpy@plt>
jmp    166e <SMS_ReadMessage+0x48e>
lea    0xa29(%rip),%rdi
xor    %eax,%eax
call   1070 <logcat@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x328,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
call   10d0 <strerror@plt>
lea    0x9df(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <logcat@plt>
jmp    1620 <SMS_ReadMessage+0x440>
lea    0xa0d(%rip),%rdi
xor    %eax,%eax
call   1070 <logcat@plt>
jmp    1625 <SMS_ReadMessage+0x445>
lea    0xa1a(%rip),%rdi
xor    %eax,%eax
call   1070 <logcat@plt>
lea    0x98b(%rip),%r14
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <strsep@plt>
test   %rax,%rax
je     1625 <SMS_ReadMessage+0x445>
mov    %rax,%rbp
mov    %rax,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
jle    1680 <SMS_ReadMessage+0x4a0>
lea    0x992(%rip),%rsi
mov    %rbp,%rdi
mov    $0x2,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    1625 <SMS_ReadMessage+0x445>
lea    0x9d4(%rip),%rdi
xor    %ebx,%ebx
mov    %r12,%rsi
mov    0x10(%rsp),%rdx
xor    %eax,%eax
call   1070 <logcat@plt>
jmp    1625 <SMS_ReadMessage+0x445>

LLVM IR (-O0):
@.str = [3 x i8] c"\0A\0A\00"
@.str.1 = [12 x i8] c"at+cmgr=%d\0D\00"
@.str.2 = [21 x i8] c"SMS Send CMD error.\0A\00"
@errno = global i64 0
@ETIME = global i64 0
@.str.3 = [21 x i8] c"Modem read uart: %s\0A\00"
@.str.4 = [3 x i8] c"OK\00"
@.str.5 = [6 x i8] c"ERROR\00"
@.str.6 = [28 x i8] c"Modem: read message error.\0A\00"
@.str.7 = [6 x i8] c"+CMGR\00"
@.str.8 = [29 x i8] c"SMS Get Phone Number Error.\0A\00"
@.str.9 = [23 x i8] c"Message Data is NULL.\0A\00"
@.str.10 = [42 x i8] c"SMS Read Message: phone = %s, data = %s \0A\00"
define i32 @SMS_ReadMessage(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [512 x i8]
%11 = alloca [256 x i8]
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* null, i8** %12
store i8* null, i8** %13
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %14
store i32 5, i32* %17
br label %18
18: ; preds = %76, %4
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 256)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%22 = load i32, i32* %7
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %6
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%26 = call i64 @Modem_SendCmd(i32 %24, i8* %25, i32 5)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %154
30: ; preds = %18
%31 = call i32 @usleep(i32 400000)
store i32 0, i32* %15
store i32 0, i32* %16
br label %32
32: ; preds = %67, %30
%33 = load i32, i32* %16
%34 = icmp slt i32 %33, 5
br i1 %34, label %35, label %70
35: ; preds = %32
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 0, i32 512)
%38 = load i32, i32* %6
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%40 = load i32, i32* %15
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = call i32 @readn(i32 %38, i8* %42, i32 512, i32 2)
store i32 %43, i32* %15
%44 = load i32, i32* %15
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %55
46: ; preds = %35
%47 = load i64, i64* @errno
%48 = load i64, i64* @ETIME
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
br label %67
51: ; preds = %46
%52 = load i64, i64* @errno
%53 = call i32 @strerror(i64 %52)
%54 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %53)
store i32 -1, i32* %5
br label %154
55: ; preds = %35
%56 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%57 = call i64 @strstr(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %55
br label %70
60: ; preds = %55
%61 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%62 = call i64 @strstr(i8* %61, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %5
br label %154
66: ; preds = %60
br label %67
67: ; preds = %66, %50
%68 = load i32, i32* %16
%69 = add nsw i32 %68, 1
store i32 %69, i32* %16
br label %32
70: ; preds = %59, %32
%71 = load i32, i32* %16
%72 = icmp eq i32 %71, 5
br i1 %72, label %73, label %81
73: ; preds = %70
%74 = load i32, i32* %17
%75 = icmp sgt i32 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %73
%77 = load i32, i32* %17
%78 = add nsw i32 %77, -1
store i32 %78, i32* %17
%79 = call i32 @sleep(i32 2)
br label %18
80: ; preds = %73
store i32 -1, i32* %5
br label %154
81: ; preds = %70
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
store i8* %82, i8** %13
br label %83
83: ; preds = %91, %81
%84 = call i8* @strsep(i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0))
store i8* %84, i8** %12
%85 = icmp ne i8* %84, null
br i1 %85, label %86, label %92
86: ; preds = %83
%87 = load i8*, i8** %12
%88 = call i64 @memcmp(i8* %87, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 5)
%89 = icmp eq i64 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %86
br label %92
91: ; preds = %86
br label %83
92: ; preds = %90, %83
%93 = load i8*, i8** %12
%94 = icmp eq i8* %93, null
br i1 %94, label %95, label %96
95: ; preds = %92
store i32 -1, i32* %5
br label %154
96: ; preds = %92
%97 = load i8*, i8** %12
%98 = call i64 @memcmp(i8* %97, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 5)
%99 = icmp eq i64 %98, 0
br i1 %99, label %100, label %149
100: ; preds = %96
%101 = load i8*, i8** %12
%102 = load i8*, i8** %9
%103 = call i64 @SMS_GetPhoneNum(i8* %101, i8* %102)
%104 = icmp slt i64 %103, 0
br i1 %104, label %105, label %107
105: ; preds = %100
%106 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %5
br label %154
107: ; preds = %100
br label %108
108: ; preds = %116, %107
%109 = call i8* @strsep(i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0))
store i8* %109, i8** %12
%110 = icmp ne i8* %109, null
br i1 %110, label %111, label %117
111: ; preds = %108
%112 = load i8*, i8** %12
%113 = call i64 @strlen(i8* %112)
%114 = icmp sgt i64 %113, 0
br i1 %114, label %115, label %116
115: ; preds = %111
br label %117
116: ; preds = %111
br label %108
117: ; preds = %115, %108
%118 = load i8*, i8** %12
%119 = icmp eq i8* %118, null
br i1 %119, label %120, label %122
120: ; preds = %117
%121 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0))
br label %128
122: ; preds = %117
%123 = load i8*, i8** %8
%124 = load i8*, i8** %12
%125 = load i8*, i8** %12
%126 = call i64 @strlen(i8* %125)
%127 = call i32 @memcpy(i8* %123, i8* %124, i64 %126)
br label %128
128: ; preds = %122, %120
br label %129
129: ; preds = %137, %128
%130 = call i8* @strsep(i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0))
store i8* %130, i8** %12
%131 = icmp ne i8* %130, null
br i1 %131, label %132, label %138
132: ; preds = %129
%133 = load i8*, i8** %12
%134 = call i64 @strlen(i8* %133)
%135 = icmp sgt i64 %134, 0
br i1 %135, label %136, label %137
136: ; preds = %132
br label %138
137: ; preds = %132
br label %129
138: ; preds = %136, %129
%139 = load i8*, i8** %12
%140 = icmp ne i8* %139, null
br i1 %140, label %141, label %147
141: ; preds = %138
%142 = load i8*, i8** %12
%143 = call i64 @memcmp(i8* %142, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%144 = icmp ne i64 %143, 0
br i1 %144, label %145, label %146
145: ; preds = %141
store i32 -1, i32* %5
br label %154
146: ; preds = %141
br label %148
147: ; preds = %138
store i32 -1, i32* %5
br label %154
148: ; preds = %146
br label %150
149: ; preds = %96
store i32 -1, i32* %5
br label %154
150: ; preds = %148
%151 = load i8*, i8** %9
%152 = load i8*, i8** %8
%153 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.10, i64 0, i64 0), i8* %151, i8* %152)
store i32 0, i32* %5
br label %154
154: ; preds = %150, %149, %147, %145, %105, %95, %80, %64, %51, %28
%155 = load i32, i32* %5
ret i32 %155
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0A\0A\00"
@.str.1 = [12 x i8] c"at+cmgr=%d\0D\00"
@.str.2 = [21 x i8] c"SMS Send CMD error.\0A\00"
@errno = global i64 0
@ETIME = global i64 0
@.str.3 = [21 x i8] c"Modem read uart: %s\0A\00"
@.str.4 = [3 x i8] c"OK\00"
@.str.5 = [6 x i8] c"ERROR\00"
@.str.6 = [28 x i8] c"Modem: read message error.\0A\00"
@.str.7 = [6 x i8] c"+CMGR\00"
@.str.8 = [29 x i8] c"SMS Get Phone Number Error.\0A\00"
@.str.9 = [23 x i8] c"Message Data is NULL.\0A\00"
@.str.10 = [42 x i8] c"SMS Read Message: phone = %s, data = %s \0A\00"
define i32 @SMS_ReadMessage(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [512 x i8]
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = bitcast i8** %7 to i8*
store i8* null, i8** %7
%11 = call i32 @memset(i8* %9, i32 0, i32 256)
%12 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%13 = call i64 @Modem_SendCmd(i32 %0, i8* %9, i32 5)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %105, %4
%16 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %149
17: ; preds = %4, %105
%18 = phi i32 [ %106, %105 ], [ 5, %4 ]
%19 = call i32 @usleep(i32 400000)
%20 = call i32 @memset(i8* %8, i32 0, i32 512)
%21 = call i32 @readn(i32 %0, i8* %8, i32 512, i32 2)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %17
%24 = load i64, i64* @errno
%25 = load i64, i64* @ETIME
%26 = icmp eq i64 %24, %25
br i1 %26, label %39, label %27
27: ; preds = %99, %83, %67, %51, %23
%28 = phi i64 [ %24, %23 ], [ %52, %51 ], [ %68, %67 ], [ %84, %83 ], [ %100, %99 ]
%29 = call i32 @strerror(i64 %28)
%30 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %149
31: ; preds = %17
%32 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %112
34: ; preds = %31
%35 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %96, %80, %64, %48, %34
%38 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %149
39: ; preds = %34, %23
%40 = call i32 @memset(i8* %8, i32 0, i32 512)
%41 = sext i32 %21 to i64
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %41
%43 = call i32 @readn(i32 %0, i8* %42, i32 512, i32 2)
%44 = icmp slt i32 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %39
%46 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %112
48: ; preds = %45
%49 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %55, label %37
51: ; preds = %39
%52 = load i64, i64* @errno
%53 = load i64, i64* @ETIME
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %27
55: ; preds = %51, %48
%56 = call i32 @memset(i8* %8, i32 0, i32 512)
%57 = sext i32 %43 to i64
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %57
%59 = call i32 @readn(i32 %0, i8* %58, i32 512, i32 2)
%60 = icmp slt i32 %59, 0
br i1 %60, label %67, label %61
61: ; preds = %55
%62 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %112
64: ; preds = %61
%65 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%66 = icmp eq i64 %65, 0
br i1 %66, label %71, label %37
67: ; preds = %55
%68 = load i64, i64* @errno
%69 = load i64, i64* @ETIME
%70 = icmp eq i64 %68, %69
br i1 %70, label %71, label %27
71: ; preds = %67, %64
%72 = call i32 @memset(i8* %8, i32 0, i32 512)
%73 = sext i32 %59 to i64
%74 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %73
%75 = call i32 @readn(i32 %0, i8* %74, i32 512, i32 2)
%76 = icmp slt i32 %75, 0
br i1 %76, label %83, label %77
77: ; preds = %71
%78 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%79 = icmp eq i64 %78, 0
br i1 %79, label %80, label %112
80: ; preds = %77
%81 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%82 = icmp eq i64 %81, 0
br i1 %82, label %87, label %37
83: ; preds = %71
%84 = load i64, i64* @errno
%85 = load i64, i64* @ETIME
%86 = icmp eq i64 %84, %85
br i1 %86, label %87, label %27
87: ; preds = %83, %80
%88 = call i32 @memset(i8* %8, i32 0, i32 512)
%89 = sext i32 %75 to i64
%90 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %89
%91 = call i32 @readn(i32 %0, i8* %90, i32 512, i32 2)
%92 = icmp slt i32 %91, 0
br i1 %92, label %99, label %93
93: ; preds = %87
%94 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%95 = icmp eq i64 %94, 0
br i1 %95, label %96, label %112
96: ; preds = %93
%97 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%98 = icmp eq i64 %97, 0
br i1 %98, label %103, label %37
99: ; preds = %87
%100 = load i64, i64* @errno
%101 = load i64, i64* @ETIME
%102 = icmp eq i64 %100, %101
br i1 %102, label %103, label %27
103: ; preds = %96, %99
%104 = icmp eq i32 %18, 0
br i1 %104, label %149, label %105
105: ; preds = %103
%106 = add nsw i32 %18, -1
%107 = call i32 @sleep(i32 2)
%108 = call i32 @memset(i8* %9, i32 0, i32 256)
%109 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%110 = call i64 @Modem_SendCmd(i32 %0, i8* %9, i32 5)
%111 = icmp slt i64 %110, 0
br i1 %111, label %15, label %17
112: ; preds = %31, %45, %61, %77, %93
store i8* %8, i8** %7
br label %113
113: ; preds = %116, %112
%114 = call i8* @strsep(i8** %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%115 = icmp eq i8* %114, null
br i1 %115, label %149, label %116
116: ; preds = %113
%117 = call i64 @memcmp(i8* %114, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 5)
%118 = icmp eq i64 %117, 0
br i1 %118, label %119, label %113
119: ; preds = %116
%120 = call i64 @memcmp(i8* %114, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 5)
%121 = icmp eq i64 %120, 0
br i1 %121, label %122, label %149
122: ; preds = %119
%123 = call i64 @SMS_GetPhoneNum(i8* %114, i8* %3)
%124 = icmp slt i64 %123, 0
br i1 %124, label %125, label %127
125: ; preds = %122
%126 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
br label %149
127: ; preds = %122, %130
%128 = call i8* @strsep(i8** %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%129 = icmp eq i8* %128, null
br i1 %129, label %133, label %130
130: ; preds = %127
%131 = call i64 @strlen(i8* %128)
%132 = icmp sgt i64 %131, 0
br i1 %132, label %135, label %127
133: ; preds = %127
%134 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0))
br label %137
135: ; preds = %130
%136 = call i32 @memcpy(i8* %2, i8* %128, i64 %131)
br label %137
137: ; preds = %135, %133
br label %138
138: ; preds = %137, %141
%139 = call i8* @strsep(i8** %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%140 = icmp eq i8* %139, null
br i1 %140, label %149, label %141
141: ; preds = %138
%142 = call i64 @strlen(i8* %139)
%143 = icmp sgt i64 %142, 0
br i1 %143, label %144, label %138
144: ; preds = %141
%145 = call i64 @memcmp(i8* %139, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%146 = icmp eq i64 %145, 0
br i1 %146, label %147, label %149
147: ; preds = %144
%148 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.10, i64 0, i64 0), i8* %3, i8* %2)
br label %149
149: ; preds = %103, %113, %138, %119, %144, %147, %125, %37, %27, %15
%150 = phi i32 [ -1, %15 ], [ -1, %27 ], [ -1, %125 ], [ 0, %147 ], [ -1, %37 ], [ -1, %144 ], [ -1, %119 ], [ -1, %138 ], [ -1, %113 ], [ -1, %103 ]
ret i32 %150
}