#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__ Modem_SendCmd(int, char *, int);
scalar_t__ Modem_WaitResponse(int, char *, int);
int /*<<< orphan*/ logcat(char *, ...);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ usleep(int);
int SMS_SendMessage(int fd, char *phone, char *msg) {
  char cmd[256];
  char buf[256] = {0};
  logcat("SMS Start to Send Message, phone = %s, msg = %s\n", phone, msg);
  memset(cmd, 0, 256);
  sprintf(cmd, "AT+CMGS=\"%s\"\r", phone);
  if (Modem_SendCmd(fd, cmd, 5) < 0)
    return -1;
  usleep(500 * 1000);
  if (Modem_WaitResponse(fd, ">", 5) < 0) {
    logcat("SMS Wait > error.\n");
    return -1;
  }
  usleep(500 * 1000);
  memset(buf, 0, 256);
  memcpy(buf, msg, strlen(msg));
  buf[strlen(msg)] = 0x1a;
  if (Modem_SendCmd(fd, buf, 5) < 0)
    return -1;
  usleep(500 * 1000);
  if (Modem_WaitResponse(fd, "OK", 30) < 0) {
    logcat("SMS Send Message error.\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<SMS_SendMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe4d(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x5,%edx
call   1060 <Modem_SendCmd@plt>
cmp    $0x0,%rax
jge    1213 <SMS_SendMessage+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <SMS_SendMessage+0x1ae>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    0xe18(%rip),%rsi
mov    $0x5,%edx
call   1080 <Modem_WaitResponse@plt>
cmp    $0x0,%rax
jge    1255 <SMS_SendMessage+0xd5>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <SMS_SendMessage+0x1ae>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x228(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x230(%rbp),%rdi
mov    -0x228(%rbp),%rsi
mov    %rax,%rdx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
movb   $0x1a,-0x220(%rbp,%rax,1)
1a
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x5,%edx
call   1060 <Modem_SendCmd@plt>
cmp    $0x0,%rax
jge    12e5 <SMS_SendMessage+0x165>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <SMS_SendMessage+0x1ae>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    0xd5b(%rip),%rsi
mov    $0x1e,%edx
call   1080 <Modem_WaitResponse@plt>
cmp    $0x0,%rax
jge    1327 <SMS_SendMessage+0x1a7>
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <SMS_SendMessage+0x1ae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SMS_SendMessage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%r14d
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xdf9(%rip),%rdi
xor    %eax,%eax
call   1050 <logcat@plt>
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    0xe05(%rip),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1090 <sprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x5,%edx
call   1060 <Modem_SendCmd@plt>
test   %rax,%rax
js     1306 <SMS_SendMessage+0x186>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
lea    0xdde(%rip),%rsi
mov    %r14d,%edi
mov    $0x5,%edx
call   1080 <Modem_WaitResponse@plt>
test   %rax,%rax
js     12f8 <SMS_SendMessage+0x178>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1070 <memcpy@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
movb   $0x1a,(%rsp,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x5,%edx
call   1060 <Modem_SendCmd@plt>
test   %rax,%rax
js     1306 <SMS_SendMessage+0x186>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
lea    0xd77(%rip),%rsi
mov    %r14d,%edi
mov    $0x1e,%edx
call   1080 <Modem_WaitResponse@plt>
test   %rax,%rax
jns    130b <SMS_SendMessage+0x18b>
lea    0xd61(%rip),%rdi
jmp    12ff <SMS_SendMessage+0x17f>
lea    0xd42(%rip),%rdi
xor    %eax,%eax
call   1050 <logcat@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"SMS Start to Send Message, phone = %s, msg = %s\0A\00"
@.str.1 = [14 x i8] c"AT+CMGS=\22%s\22\0D\00"
@.str.2 = [2 x i8] c">\00"
@.str.3 = [19 x i8] c"SMS Wait > error.\0A\00"
@.str.4 = [3 x i8] c"OK\00"
@.str.5 = [25 x i8] c"SMS Send Message error.\0A\00"
define i32 @SMS_SendMessage(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca [256 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = bitcast [256 x i8]* %9 to i8*
%11 = load i8*, i8** %6
%12 = load i8*, i8** %7
%13 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i32 0, i32 256)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%17 = load i8*, i8** %6
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %5
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%21 = call i64 @Modem_SendCmd(i32 %19, i8* %20, i32 5)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %3
store i32 -1, i32* %4
br label %56
24: ; preds = %3
%25 = call i32 @usleep(i32 500000)
%26 = load i32, i32* %5
%27 = call i64 @Modem_WaitResponse(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 5)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %56
31: ; preds = %24
%32 = call i32 @usleep(i32 500000)
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 0, i32 256)
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%36 = load i8*, i8** %7
%37 = load i8*, i8** %7
%38 = call i64 @strlen(i8* %37)
%39 = call i32 @memcpy(i8* %35, i8* %36, i64 %38)
%40 = load i8*, i8** %7
%41 = call i64 @strlen(i8* %40)
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 %41
store i8 26, i8* %42
%43 = load i32, i32* %5
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%45 = call i64 @Modem_SendCmd(i32 %43, i8* %44, i32 5)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %31
store i32 -1, i32* %4
br label %56
48: ; preds = %31
%49 = call i32 @usleep(i32 500000)
%50 = load i32, i32* %5
%51 = call i64 @Modem_WaitResponse(i32 %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 30)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %4
br label %56
55: ; preds = %48
store i32 0, i32* %4
br label %56
56: ; preds = %55, %53, %47, %29, %23
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@.str = [49 x i8] c"SMS Start to Send Message, phone = %s, msg = %s\0A\00"
@.str.1 = [14 x i8] c"AT+CMGS=\22%s\22\0D\00"
@.str.2 = [2 x i8] c">\00"
@.str.3 = [19 x i8] c"SMS Wait > error.\0A\00"
@.str.4 = [3 x i8] c"OK\00"
@.str.5 = [25 x i8] c"SMS Send Message error.\0A\00"
define i32 @SMS_SendMessage(i32 %0, i8* %1, i8* %2) {
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = tail call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%9 = call i32 @memset(i8* %6, i32 0, i32 256)
%10 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%11 = call i64 @Modem_SendCmd(i32 %0, i8* %6, i32 5)
%12 = icmp slt i64 %11, 0
br i1 %12, label %33, label %13
13: ; preds = %3
%14 = call i32 @usleep(i32 500000)
%15 = call i64 @Modem_WaitResponse(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 5)
%16 = icmp slt i64 %15, 0
br i1 %16, label %30, label %17
17: ; preds = %13
%18 = call i32 @usleep(i32 500000)
%19 = call i32 @memset(i8* %7, i32 0, i32 256)
%20 = call i64 @strlen(i8* dereferenceable(1) %2)
%21 = call i32 @memcpy(i8* %7, i8* %2, i64 %20)
%22 = call i64 @strlen(i8* dereferenceable(1) %2)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %22
store i8 26, i8* %23
%24 = call i64 @Modem_SendCmd(i32 %0, i8* %7, i32 5)
%25 = icmp slt i64 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %17
%27 = call i32 @usleep(i32 500000)
%28 = call i64 @Modem_WaitResponse(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 30)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26, %13
%31 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), %26 ]
%32 = call i32 (i8*, ...) @logcat(i8* %31)
br label %33
33: ; preds = %30, %26, %17, %3
%34 = phi i32 [ -1, %3 ], [ -1, %17 ], [ 0, %26 ], [ -1, %30 ]
ret i32 %34
}