#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 ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
#define EAGAIN 128
int errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ *strstr(char *, char *);
int recv_data_from_modem(int fd, char *modem_reply_msg) {
  char buffer[1024];
  char *bufptr;
  ssize_t n;
  memset(buffer, 0, sizeof(buffer));
  bufptr = buffer;
  while (1) {
    n = read(fd, bufptr, 1);
    if (n == -1) {
      switch (errno) {
      case EAGAIN:
        continue;
      default:
        printf("read port error!\n");
        break;
      }
    }
    if (n == 0)
      break;
    bufptr += n;
    if (strstr(buffer, "OK") != NULL || strstr(buffer, "ERROR") != NULL)
      break;
  }
  *bufptr = '\0';
  printf("%s\n", buffer);
  strncpy(modem_reply_msg, buffer, 1024);
  return 0;
}


x86 assembly (clang, -O0):
<recv_data_from_modem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x418(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0xffffffff,-0x41c(%rbp)
jne    11db <recv_data_from_modem+0x8b>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    11c8 <recv_data_from_modem+0x78>
jmp    11c3 <recv_data_from_modem+0x73>
jmp    1183 <recv_data_from_modem+0x33>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11db <recv_data_from_modem+0x8b>
cmpl   $0x0,-0x41c(%rbp)
jne    11ed <recv_data_from_modem+0x9d>
jmp    124b <recv_data_from_modem+0xfb>
mov    -0x41c(%rbp),%ecx
mov    -0x418(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
lea    0xdfd(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    1241 <recv_data_from_modem+0xf1>
lea    -0x410(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     1246 <recv_data_from_modem+0xf6>
jmp    124b <recv_data_from_modem+0xfb>
jmp    1183 <recv_data_from_modem+0x33>
mov    -0x418(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x410(%rbp),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_data_from_modem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    0x2e61(%rip),%r13
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11bf <recv_data_from_modem+0x6f>
cmpl   $0x80,0x0(%r13)
je     1190 <recv_data_from_modem+0x40>
lea    0xe51(%rip),%rdi
call   1040 <puts@plt>
jmp    11c4 <recv_data_from_modem+0x74>
test   %r12d,%r12d
je     11f2 <recv_data_from_modem+0xa2>
movslq %r12d,%rax
add    %rax,%rbx
mov    %r15,%rdi
lea    0xe2c(%rip),%rsi
call   1070 <strstr@plt>
test   %rax,%rax
jne    11f2 <recv_data_from_modem+0xa2>
mov    %r15,%rdi
lea    0xe1b(%rip),%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1190 <recv_data_from_modem+0x40>
movb   $0x0,(%rbx)
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [18 x i8] c"read port error!\0A\00"
@.str.1 = [3 x i8] c"OK\00"
@.str.2 = [6 x i8] c"ERROR\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @recv_data_from_modem(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 1024)
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
store i8* %10, i8** %6
br label %11
11: ; preds = %2, %19, %40
%12 = load i32, i32* %3
%13 = load i8*, i8** %6
%14 = call i32 @read(i32 %12, i8* %13, i32 1)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = load i32, i32* @errno
switch i32 %18, label %20 [
i32 128, label %19
]
19: ; preds = %17
br label %11
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20
br label %23
23: ; preds = %22, %11
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
br label %41
27: ; preds = %23
%28 = load i32, i32* %7
%29 = load i8*, i8** %6
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %6
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = call i32* @strstr(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp ne i32* %33, null
br i1 %34, label %39, label %35
35: ; preds = %27
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%37 = call i32* @strstr(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %40
39: ; preds = %35, %27
br label %41
40: ; preds = %35
br label %11
41: ; preds = %39, %26
%42 = load i8*, i8** %6
store i8 0, i8* %42
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = load i8*, i8** %4
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%47 = call i32 @strncpy(i8* %45, i8* %46, i32 1024)
ret i32 0
}

LLVM IR (-O2):
@errno = global i32 0
@.str.1 = [3 x i8] c"OK\00"
@.str.2 = [6 x i8] c"ERROR\00"
@str = [17 x i8] c"read port error!\00"
define i32 @recv_data_from_modem(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 1024)
br label %6
6: ; preds = %20, %2
%7 = phi i8* [ %17, %20 ], [ %4, %2 ]
br label %8
8: ; preds = %6, %10
%9 = call i32 @read(i32 %0, i8* %7, i32 1)
switch i32 %9, label %15 [
i32 -1, label %10
i32 0, label %23
]
10: ; preds = %8
%11 = load i32, i32* @errno
%12 = icmp eq i32 %11, 128
br i1 %12, label %8, label %13
13: ; preds = %10
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %8, %13
%16 = sext i32 %9 to i64
%17 = getelementptr inbounds i8, i8* %7, i64 %16
%18 = call i32* @strstr(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32* @strstr(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %6, label %23
23: ; preds = %15, %20, %8
%24 = phi i8* [ %7, %8 ], [ %17, %20 ], [ %17, %15 ]
store i8 0, i8* %24
%25 = call i32 @puts(i8* %4)
%26 = call i32 @strncpy(i8* %1, i8* %4, i32 1024)
ret i32 0
}