#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*/ buf;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strerror(scalar_t__);
int strlen(char *);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
void test_for_at(int fd) {
  char buf[1024];
  ssize_t nbytes, nwrite;
  int len, i, towrite;
  printf("\n\nTest for AT command, q(Q) to exit\n\n");
  while (1) {
    printf(">>> ");
    fgets(buf, sizeof(buf), stdin);
    if (buf[0] == 'q' || buf[0] == 'Q')
      break;
    if (buf[0] == '\n')
      continue;
    len = strlen(buf);
    buf[len - 1] = 0x0D;
    buf[len] = 0x0A;
    buf[len + 1] = '\0';
    towrite = len + 1;
    nwrite = 0;
    while (nwrite < towrite) {
      nbytes = write(fd, buf + nwrite, towrite - nwrite);
      if (nbytes < 0) {
        if (errno == EINTR) {
          nwrite = 0;
        } else {
          fprintf(stderr, "write() failed: %s\n", strerror(errno));
          return;
        }
      }
      nwrite += nbytes;
    }
    usleep(500);
    nbytes = read(fd, buf, sizeof(buf));
    buf[nbytes] = '\0';
    printf("[receive %d bytes]\n", (int)nbytes);
    printf("\nASCII:\n%s\n", buf);
  }
  printf("exiting...\n");
}


x86 assembly (clang, -O0):
<test_for_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
movsbl -0x410(%rbp),%eax
cmp    $0x71,%eax
je     11e4 <test_for_at+0x64>
movsbl -0x410(%rbp),%eax
cmp    $0x51,%eax
jne    11e9 <test_for_at+0x69>
jmp    1386 <test_for_at+0x206>
movsbl -0x410(%rbp),%eax
cmp    $0xa,%eax
jne    11fe <test_for_at+0x7e>
jmp    119c <test_for_at+0x1c>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0xd,-0x410(%rbp,%rax,1)
0d
movslq -0x41c(%rbp),%rax
movb   $0xa,-0x410(%rbp,%rax,1)
0a
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
movl   $0x0,-0x418(%rbp)
mov    -0x418(%rbp),%eax
cmp    -0x424(%rbp),%eax
jge    1325 <test_for_at+0x1a5>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
movslq -0x418(%rbp),%rax
add    %rax,%rsi
mov    -0x424(%rbp),%edx
sub    -0x418(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    130e <test_for_at+0x18e>
mov    0x2d21(%rip),%rax
mov    (%rax),%rax
mov    0x2d0f(%rip),%rcx
cmp    (%rcx),%rax
jne    12d1 <test_for_at+0x151>
movl   $0x0,-0x418(%rbp)
jmp    1309 <test_for_at+0x189>
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    0x2ce9(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x428(%rbp),%edi
mov    %rax,%rdx
lea    0xd2c(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1394 <test_for_at+0x214>
jmp    130e <test_for_at+0x18e>
mov    -0x414(%rbp),%eax
add    -0x418(%rbp),%eax
mov    %eax,-0x418(%rbp)
jmp    125e <test_for_at+0xde>
mov    $0x1f4,%edi
call   10a0 <usleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x414(%rbp)
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x414(%rbp),%esi
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rsi
lea    0xcd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    119c <test_for_at+0x1c>
lea    0xcd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_for_at>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e24(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
mov    (%rsp),%al
mov    %eax,%ecx
and    $0xdf,%cl
cmp    $0x51,%cl
jne    11f6 <test_for_at+0x66>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
jmp    1314 <test_for_at+0x184>
lea    0xe03(%rip),%rbp
mov    0x2dc4(%rip),%r15
jmp    128c <test_for_at+0xfc>
nopl   0x0(%rax)
mov    $0x1f4,%edi
call   10b0 <usleep@plt>
mov    %ebx,%edi
mov    %r13,%r14
mov    %r13,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
movslq %eax,%rsi
movb   $0x0,(%rsp,%rsi,1)
lea    0xddf(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xde5(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d8f(%rip),%r12
lea    0xda0(%rip),%rbp
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r12),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
mov    (%rsp),%al
mov    %eax,%ecx
and    $0xdf,%cl
cmp    $0x51,%cl
je     11e5 <test_for_at+0x55>
cmp    $0xa,%al
je     1260 <test_for_at+0xd0>
mov    %r14,%rdi
call   1050 <strlen@plt>
movslq %eax,%r12
mov    %r14,%r13
movw   $0xa0d,-0x1(%r12,%r14,1)
0a
movb   $0x0,0x1(%rsp,%r12,1)
test   %r12d,%r12d
js     1210 <test_for_at+0x80>
mov    %eax,%ebp
add    $0x1,%r12
xor    %r14d,%r14d
jmp    12cc <test_for_at+0x13c>
add    %eax,%r14d
cmp    %r14d,%ebp
jl     1210 <test_for_at+0x80>
movslq %r14d,%rax
lea    (%rsp,%rax,1),%rsi
mov    %r12d,%edx
sub    %eax,%edx
mov    %ebx,%edi
call   1040 <write@plt>
test   %eax,%eax
jns    12c0 <test_for_at+0x130>
mov    0x2ce6(%rip),%rcx
mov    (%rcx),%rdi
xor    %r14d,%r14d
cmp    (%r15),%rdi
je     12c0 <test_for_at+0x130>
mov    0x2cdc(%rip),%rax
mov    (%rax),%ebx
call   10a0 <strerror@plt>
lea    0xcfb(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"\0A\0ATest for AT command, q(Q) to exit\0A\0A\00"
@.str.1 = [5 x i8] c">>> \00"
@stdin = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"write() failed: %s\0A\00"
@.str.3 = [20 x i8] c"[receive %d bytes]\0A\00"
@.str.4 = [12 x i8] c"\0AASCII:\0A%s\0A\00"
@.str.5 = [12 x i8] c"exiting...\0A\00"
define void @test_for_at(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %1, %30, %78
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %12, i32 1024, i32 %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 113
br i1 %18, label %24, label %19
19: ; preds = %10
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 81
br i1 %23, label %24, label %25
24: ; preds = %19, %10
br label %90
25: ; preds = %19
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %25
br label %10
31: ; preds = %25
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = sub nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %36
store i8 13, i8* %37
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %39
store i8 10, i8* %40
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %43
store i8 0, i8* %44
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
store i32 0, i32* %5
br label %47
47: ; preds = %74, %31
%48 = load i32, i32* %5
%49 = load i32, i32* %8
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %78
51: ; preds = %47
%52 = load i32, i32* %2
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%54 = load i32, i32* %5
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = load i32, i32* %8
%58 = load i32, i32* %5
%59 = sub nsw i32 %57, %58
%60 = call i32 @write(i32 %52, i8* %56, i32 %59)
store i32 %60, i32* %4
%61 = load i32, i32* %4
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %74
63: ; preds = %51
%64 = load i64, i64* @errno
%65 = load i64, i64* @EINTR
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %63
store i32 0, i32* %5
br label %73
68: ; preds = %63
%69 = load i32, i32* @stderr
%70 = load i64, i64* @errno
%71 = call i8* @strerror(i64 %70)
%72 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %71)
br label %92
73: ; preds = %67
br label %74
74: ; preds = %73, %51
%75 = load i32, i32* %4
%76 = load i32, i32* %5
%77 = add nsw i32 %76, %75
store i32 %77, i32* %5
br label %47
78: ; preds = %47
%79 = call i32 @usleep(i32 500)
%80 = load i32, i32* %2
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%82 = call i32 @read(i32 %80, i8* %81, i32 1024)
store i32 %82, i32* %4
%83 = load i32, i32* %4
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %84
store i8 0, i8* %85
%86 = load i32, i32* %4
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %86)
%88 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %88)
br label %10
90: ; preds = %24
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %92
92: ; preds = %90, %68
ret void
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c">>> \00"
@stdin = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"write() failed: %s\0A\00"
@.str.3 = [20 x i8] c"[receive %d bytes]\0A\00"
@.str.4 = [12 x i8] c"\0AASCII:\0A%s\0A\00"
@str = [37 x i8] c"\0A\0ATest for AT command, q(Q) to exit\0A\00"
@str.6 = [11 x i8] c"exiting...\00"
define void @test_for_at(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32 @fgets(i8* %3, i32 1024, i32 %6)
%8 = load i8, i8* %3
%9 = and i8 %8, -33
%10 = icmp eq i8 %9, 81
br i1 %10, label %58, label %11
11: ; preds = %1, %51
%12 = phi i8 [ %55, %51 ], [ %8, %1 ]
%13 = icmp eq i8 %12, 10
br i1 %13, label %51, label %14
14: ; preds = %11
%15 = call i32 @strlen(i8* %3)
%16 = add nsw i32 %15, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %17
store i8 13, i8* %18
%19 = sext i32 %15 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %19
store i8 10, i8* %20
%21 = add nsw i32 %15, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %22
store i8 0, i8* %23
%24 = icmp slt i32 %15, 0
br i1 %24, label %44, label %25
25: ; preds = %14, %40
%26 = phi i32 [ %42, %40 ], [ 0, %14 ]
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %27
%29 = sub nsw i32 %21, %26
%30 = call i32 @write(i32 %0, i8* %28, i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %25
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = call i8* @strerror(i64 %33)
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %60
40: ; preds = %32, %25
%41 = phi i32 [ %26, %25 ], [ 0, %32 ]
%42 = add nsw i32 %41, %30
%43 = icmp slt i32 %15, %42
br i1 %43, label %44, label %25
44: ; preds = %40, %14
%45 = call i32 @usleep(i32 500)
%46 = call i32 @read(i32 %0, i8* %3, i32 1024)
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %3)
br label %51
51: ; preds = %44, %11
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* @stdin
%54 = call i32 @fgets(i8* %3, i32 1024, i32 %53)
%55 = load i8, i8* %3
%56 = and i8 %55, -33
%57 = icmp eq i8 %56, 81
br i1 %57, label %58, label %11
58: ; preds = %51, %1
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %36
ret void
}