#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__ errno;
int /*<<< orphan*/ logcat(char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int readn(int, char *, int, int);
char *strerror(scalar_t__);
scalar_t__ strstr(char *, char *);
int Modem_WaitResponse(int fd, char *expect, int retry) {
  char rbuf[512];
  int i, nread;
  for (i = 0; i < retry; i++) {
    memset(rbuf, 0, 512);
    nread = readn(fd, rbuf, 512, 5);
    if (nread < 0) {
      if (errno == ETIME)
        continue;
      else {
        logcat("Modem read uart: %s\n", strerror(errno));
        return -1;
      }
    }
    if (strstr(rbuf, expect)) {
      logcat("SMS WaitResponse: found string: %s \n", expect);
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<Modem_WaitResponse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x224(%rbp)
mov    -0x224(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1255 <Modem_WaitResponse+0x105>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
mov    $0x5,%ecx
call   1040 <readn@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
jge    1206 <Modem_WaitResponse+0xb6>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
jne    11dc <Modem_WaitResponse+0x8c>
jmp    1241 <Modem_WaitResponse+0xf1>
mov    0x2df5(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe0b(%rip),%rdi
call   1050 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <Modem_WaitResponse+0x10c>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     123c <Modem_WaitResponse+0xec>
mov    -0x10(%rbp),%rsi
lea    0xdea(%rip),%rdi
call   1050 <logcat@plt>
movl   $0x0,-0x4(%rbp)
jmp    125c <Modem_WaitResponse+0x10c>
jmp    1241 <Modem_WaitResponse+0xf1>
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
jmp    116f <Modem_WaitResponse+0x1f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Modem_WaitResponse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    $0xffffffff,%eax
test   %edx,%edx
jle    1202 <Modem_WaitResponse+0xb2>
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r12d
mov    %rsp,%rbx
mov    0x2e58(%rip),%r13
mov    0x2e61(%rip),%r14
jmp    119e <Modem_WaitResponse+0x4e>
nopl   0x0(%rax)
mov    0x0(%r13),%rdi
cmp    (%r14),%rdi
jne    11e9 <Modem_WaitResponse+0x99>
add    $0xffffffff,%ebp
je     11fd <Modem_WaitResponse+0xad>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1030 <memset@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
mov    $0x5,%ecx
call   1040 <readn@plt>
test   %eax,%eax
js     1190 <Modem_WaitResponse+0x40>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1199 <Modem_WaitResponse+0x49>
lea    0xe38(%rip),%rdi
mov    %r15,%rsi
call   1050 <logcat@plt>
xor    %eax,%eax
jmp    1202 <Modem_WaitResponse+0xb2>
call   1060 <strerror@plt>
lea    0xe0b(%rip),%rdi
mov    %rax,%rsi
call   1050 <logcat@plt>
mov    $0xffffffff,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ETIME = global i64 0
@.str = [21 x i8] c"Modem read uart: %s\0A\00"
@.str.1 = [37 x i8] c"SMS WaitResponse: found string: %s \0A\00"
define i32 @Modem_WaitResponse(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %11
11: ; preds = %41, %3
%12 = load i32, i32* %9
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %44
15: ; preds = %11
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%17 = call i32 @memset(i8* %16, i32 0, i32 512)
%18 = load i32, i32* %5
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%20 = call i32 @readn(i32 %18, i8* %19, i32 512, i32 5)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %15
%24 = load i64, i64* @errno
%25 = load i64, i64* @ETIME
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %41
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = call i8* @strerror(i64 %29)
%31 = call i32 @logcat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %30)
store i32 -1, i32* %4
br label %45
32: ; preds = %15
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%34 = load i8*, i8** %6
%35 = call i64 @strstr(i8* %33, i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i8*, i8** %6
%39 = call i32 @logcat(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %38)
store i32 0, i32* %4
br label %45
40: ; preds = %32
br label %41
41: ; preds = %40, %27
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %11
44: ; preds = %11
store i32 -1, i32* %4
br label %45
45: ; preds = %44, %37, %28
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@errno = global i64 0
@ETIME = global i64 0
@.str = [21 x i8] c"Modem read uart: %s\0A\00"
@.str.1 = [37 x i8] c"SMS WaitResponse: found string: %s \0A\00"
define i32 @Modem_WaitResponse(i32 %0, i8* %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %27
7: ; preds = %3, %24
%8 = phi i32 [ %25, %24 ], [ 0, %3 ]
%9 = call i32 @memset(i8* %5, i32 0, i32 512)
%10 = call i32 @readn(i32 %0, i8* %5, i32 512, i32 5)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @ETIME
%15 = icmp eq i64 %13, %14
br i1 %15, label %24, label %16
16: ; preds = %12
%17 = call i8* @strerror(i64 %13)
%18 = call i32 @logcat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %27
19: ; preds = %7
%20 = call i64 @strstr(i8* %5, i8* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @logcat(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %27
24: ; preds = %19, %12
%25 = add nuw nsw i32 %8, 1
%26 = icmp eq i32 %25, %2
br i1 %26, label %27, label %7
27: ; preds = %24, %3, %22, %16
%28 = phi i32 [ -1, %16 ], [ 0, %22 ], [ -1, %3 ], [ -1, %24 ]
ret i32 %28
}