#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 scalar_t__ pid_t;

/* Variables and functions */
int Read(char *, int);
int Write(char *, int /*<<< orphan*/);
int /*<<< orphan*/ dump(char *, int);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
void QuerySignal() {
  char buffer[1024];
  int ret;
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork");
    return;
  }
  if (pid == 0) {
    strcpy(buffer, "AT+CSQ\r");
    printf("%s\n", buffer);
    ret = Write(buffer, strlen(buffer));
    return;
  } else {
    while (true) {
      memset(buffer, 0, 1024);
      ret = Read(buffer, 1024);
      if (ret < 0) {
        return;
      }
      dump(buffer, ret);
    }
  }
}


x86 assembly (clang, -O0):
<QuerySignal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
jge    11c8 <QuerySignal+0x38>
lea    0xe42(%rip),%rdi
call   10a0 <perror@plt>
jmp    1287 <QuerySignal+0xf7>
cmpq   $0x0,-0x410(%rbp)
jne    122f <QuerySignal+0x9f>
lea    -0x400(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x400(%rbp),%rsi
lea    0xe16(%rip),%rdi
call   1060 <printf@plt>
lea    -0x400(%rbp),%rax
mov    %rax,-0x418(%rbp)
lea    -0x400(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x418(%rbp),%rdi
mov    %eax,%esi
call   1080 <Write@plt>
mov    %eax,-0x404(%rbp)
jmp    1287 <QuerySignal+0xf7>
jmp    1234 <QuerySignal+0xa4>
lea    -0x400(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x400(%rbp),%rdi
mov    $0x400,%esi
call   1040 <Read@plt>
mov    %eax,-0x404(%rbp)
cmpl   $0x0,-0x404(%rbp)
jge    1270 <QuerySignal+0xe0>
jmp    1287 <QuerySignal+0xf7>
lea    -0x400(%rbp),%rdi
mov    -0x404(%rbp),%esi
call   1090 <dump@plt>
jmp    1234 <QuerySignal+0xa4>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<QuerySignal>:
push   %rbx
sub    $0x400,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1238 <QuerySignal+0xa8>
je     11fc <QuerySignal+0x6c>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
call   1040 <Read@plt>
test   %eax,%eax
js     122f <QuerySignal+0x9f>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <dump@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
call   1040 <Read@plt>
test   %eax,%eax
jns    11d0 <QuerySignal+0x40>
jmp    122f <QuerySignal+0x9f>
lea    0xe02(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xdf8(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <Write@plt>
add    $0x400,%rsp
pop    %rbx
ret
lea    0xdc1(%rip),%rdi
add    $0x400,%rsp
pop    %rbx
jmp    10a0 <perror@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [8 x i8] c"AT+CSQ\0D\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @QuerySignal() {
%1 = alloca [1024 x i8]
%2 = alloca i32
%3 = alloca i64
%4 = call i64 (...) @fork()
store i64 %4, i64* %3
%5 = load i64, i64* %3
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %34
9: ; preds = %0
%10 = load i64, i64* %3
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%14 = call i32 @strcpy(i8* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%16 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @Write(i8* %17, i32 %19)
store i32 %20, i32* %2
br label %34
21: ; preds = %9
br label %22
22: ; preds = %21, %30
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 1024)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%26 = call i32 @Read(i8* %25, i32 1024)
store i32 %26, i32* %2
%27 = load i32, i32* %2
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %22
br label %34
30: ; preds = %22
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%32 = load i32, i32* %2
%33 = call i32 @dump(i8* %31, i32 %32)
br label %22
34: ; preds = %29, %12, %7
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [8 x i8] c"AT+CSQ\0D\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @QuerySignal() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %24
7: ; preds = %0
%8 = icmp eq i64 %3, 0
br i1 %8, label %13, label %9
9: ; preds = %7
%10 = call i32 @memset(i8* %2, i32 0, i32 1024)
%11 = call i32 @Read(i8* %2, i32 1024)
%12 = icmp slt i32 %11, 0
br i1 %12, label %24, label %18
13: ; preds = %7
%14 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%16 = call i32 @strlen(i8* %2)
%17 = call i32 @Write(i8* %2, i32 %16)
br label %24
18: ; preds = %9, %18
%19 = phi i32 [ %22, %18 ], [ %11, %9 ]
%20 = call i32 @dump(i8* %2, i32 %19)
%21 = call i32 @memset(i8* %2, i32 0, i32 1024)
%22 = call i32 @Read(i8* %2, i32 1024)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %18
24: ; preds = %18, %9, %13, %5
ret void
}