#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__ SMS_CMDProcess(char *, char *);
scalar_t__ SMS_CheckPhone(char *);
int /*<<< orphan*/ SMS_DelMessage(int, int);
scalar_t__ SMS_ReadMessage(int, int, char *, char *);
int /*<<< orphan*/ SMS_SendMessage(int, char *, char *);
int atoi(char *);
int /*<<< orphan*/ logcat(char *, ...);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char *);
char *strrchr(char *, int);
int SMS_ProcessMessage(int fd, char *msg) {
  int index = 0;
  char *p = NULL;
  char data[512];
  char phone[32];
  p = strrchr(msg, 0x2c);
  if (p != NULL) {
    index = atoi(p + 1);
    logcat("New Message Index = %d \n", index);
  } else
    return -1;
  memset(data, 0, 512);
  memset(phone, 0, 32);
  if (SMS_ReadMessage(fd, index, data, phone) < 0) {
    logcat("SMS Read message error.\n");
    return -1;
  }
  if (memcmp(phone, "+86", 3) == 0) {
    int len = strlen(phone);
    memmove(phone, (phone + 3), len - 3);
    memset((phone + len - 3), 0, 3);
  }
  if (SMS_CheckPhone(phone) == 0) {
    if (SMS_CMDProcess(data, phone) == 0) {
      SMS_SendMessage(fd, phone, "Command Sucess.");
    } else {
      SMS_SendMessage(fd, phone, "Command Failure.");
    }
  }
  SMS_DelMessage(fd, index);
  return 0;
}


x86 assembly (clang, -O0):
<SMS_ProcessMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2c,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1224 <SMS_ProcessMessage+0x64>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
jmp    1230 <SMS_ProcessMessage+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    1383 <SMS_ProcessMessage+0x1c3>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x240(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x220(%rbp),%rdx
lea    -0x240(%rbp),%rcx
call   1090 <SMS_ReadMessage@plt>
cmp    $0x0,%rax
jge    1293 <SMS_ProcessMessage+0xd3>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1383 <SMS_ProcessMessage+0x1c3>
lea    -0x240(%rbp),%rdi
lea    0xd91(%rip),%rsi
mov    $0x3,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    1308 <SMS_ProcessMessage+0x148>
lea    -0x240(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x244(%rbp)
lea    -0x240(%rbp),%rdi
lea    -0x240(%rbp),%rsi
add    $0x3,%rsi
mov    -0x244(%rbp),%edx
sub    $0x3,%edx
call   10b0 <memmove@plt>
lea    -0x240(%rbp),%rdi
movslq -0x244(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffffd,%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1060 <memset@plt>
lea    -0x240(%rbp),%rdi
call   10c0 <SMS_CheckPhone@plt>
cmp    $0x0,%rax
jne    1371 <SMS_ProcessMessage+0x1b1>
lea    -0x220(%rbp),%rdi
lea    -0x240(%rbp),%rsi
call   10e0 <SMS_CMDProcess@plt>
cmp    $0x0,%rax
jne    1356 <SMS_ProcessMessage+0x196>
mov    -0x8(%rbp),%edi
lea    -0x240(%rbp),%rsi
lea    0xcea(%rip),%rdx
call   1050 <SMS_SendMessage@plt>
jmp    136c <SMS_ProcessMessage+0x1ac>
mov    -0x8(%rbp),%edi
lea    -0x240(%rbp),%rsi
lea    0xcdf(%rip),%rdx
call   1050 <SMS_SendMessage@plt>
jmp    1371 <SMS_ProcessMessage+0x1b1>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   10a0 <SMS_DelMessage@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SMS_ProcessMessage>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x220,%rsp
mov    %edi,%r14d
mov    %rsi,%rdi
mov    $0x2c,%esi
call   1040 <strrchr@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     12f3 <SMS_ProcessMessage+0x133>
add    $0x1,%rax
mov    %rax,%rdi
call   10d0 <atoi@plt>
mov    %eax,%r15d
lea    0xdfd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <logcat@plt>
lea    0x20(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    %r14d,%edi
mov    %r15d,%esi
mov    %r12,%rdx
mov    %rbp,%rcx
call   1090 <SMS_ReadMessage@plt>
test   %rax,%rax
js     12e5 <SMS_ProcessMessage+0x125>
lea    0xddf(%rip),%rsi
mov    %rsp,%rdi
mov    $0x3,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    1299 <SMS_ProcessMessage+0xd9>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
lea    0x3(%rsp),%rsi
lea    -0x3(%rbp),%edx
mov    %rbx,%rdi
call   10b0 <memmove@plt>
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rdi
add    $0xfffffffffffffffd,%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1060 <memset@plt>
mov    %rsp,%rdi
call   10c0 <SMS_CheckPhone@plt>
test   %rax,%rax
jne    12d6 <SMS_ProcessMessage+0x116>
lea    0x20(%rsp),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   10e0 <SMS_CMDProcess@plt>
test   %rax,%rax
lea    0xd76(%rip),%rax
lea    0xd7f(%rip),%rdx
cmove  %rax,%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1050 <SMS_SendMessage@plt>
mov    %r14d,%edi
mov    %r15d,%esi
call   10a0 <SMS_DelMessage@plt>
xor    %ebx,%ebx
jmp    12f3 <SMS_ProcessMessage+0x133>
lea    0xd2d(%rip),%rdi
xor    %eax,%eax
call   1070 <logcat@plt>
mov    %ebx,%eax
add    $0x220,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"New Message Index = %d \0A\00"
@.str.1 = [25 x i8] c"SMS Read message error.\0A\00"
@.str.2 = [4 x i8] c"+86\00"
@.str.3 = [16 x i8] c"Command Sucess.\00"
@.str.4 = [17 x i8] c"Command Failure.\00"
define i32 @SMS_ProcessMessage(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [512 x i8]
%9 = alloca [32 x i8]
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i8* null, i8** %7
%11 = load i8*, i8** %5
%12 = call i8* @strrchr(i8* %11, i32 44)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8*, i8** %7
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %22
21: ; preds = %2
store i32 -1, i32* %3
br label %76
22: ; preds = %15
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 512)
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 32)
%27 = load i32, i32* %4
%28 = load i32, i32* %6
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%31 = call i64 @SMS_ReadMessage(i32 %27, i32 %28, i8* %29, i8* %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
35: ; preds = %22
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%37 = call i64 @memcmp(i8* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %35
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
store i32 %41, i32* %10
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%44 = getelementptr inbounds i8, i8* %43, i64 3
%45 = load i32, i32* %10
%46 = sub nsw i32 %45, 3
%47 = call i32 @memmove(i8* %42, i8* %44, i32 %46)
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = getelementptr inbounds i8, i8* %51, i64 -3
%53 = call i32 @memset(i8* %52, i32 0, i32 3)
br label %54
54: ; preds = %39, %35
%55 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%56 = call i64 @SMS_CheckPhone(i8* %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %72
58: ; preds = %54
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%60 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%61 = call i64 @SMS_CMDProcess(i8* %59, i8* %60)
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %58
%64 = load i32, i32* %4
%65 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%66 = call i32 @SMS_SendMessage(i32 %64, i8* %65, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %71
67: ; preds = %58
%68 = load i32, i32* %4
%69 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%70 = call i32 @SMS_SendMessage(i32 %68, i8* %69, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %71
71: ; preds = %67, %63
br label %72
72: ; preds = %71, %54
%73 = load i32, i32* %4
%74 = load i32, i32* %6
%75 = call i32 @SMS_DelMessage(i32 %73, i32 %74)
store i32 0, i32* %3
br label %76
76: ; preds = %72, %33, %21
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [25 x i8] c"New Message Index = %d \0A\00"
@.str.1 = [25 x i8] c"SMS Read message error.\0A\00"
@.str.2 = [4 x i8] c"+86\00"
@.str.3 = [16 x i8] c"Command Sucess.\00"
@.str.4 = [17 x i8] c"Command Failure.\00"
define i32 @SMS_ProcessMessage(i32 %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %1, i32 44)
%8 = icmp eq i8* %7, null
br i1 %8, label %41, label %9
9: ; preds = %2
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = tail call i32 @atoi(i8* %10)
%12 = tail call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 @memset(i8* %5, i32 0, i32 512)
%14 = call i32 @memset(i8* %6, i32 0, i32 32)
%15 = call i64 @SMS_ReadMessage(i32 %0, i32 %11, i8* %5, i8* %6)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %41
19: ; preds = %9
%20 = call i64 @memcmp(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %19
%23 = call i32 @strlen(i8* %6)
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 3
%25 = add nsw i32 %23, -3
%26 = call i32 @memmove(i8* %6, i8* %24, i32 %25)
%27 = sext i32 %23 to i64
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -3
%30 = call i32 @memset(i8* %29, i32 0, i32 3)
br label %31
31: ; preds = %22, %19
%32 = call i64 @SMS_CheckPhone(i8* %6)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = call i64 @SMS_CMDProcess(i8* %5, i8* %6)
%36 = icmp eq i64 %35, 0
%37 = select i1 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0)
%38 = call i32 @SMS_SendMessage(i32 %0, i8* %6, i8* %37)
br label %39
39: ; preds = %34, %31
%40 = call i32 @SMS_DelMessage(i32 %0, i32 %11)
br label %41
41: ; preds = %2, %39, %17
%42 = phi i32 [ -1, %17 ], [ 0, %39 ], [ -1, %2 ]
ret i32 %42
}