#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 */
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int strlen(char *);
int write(int, char *, int);
int init_modem(int fd) {
  char *cmd = "at\r\0";
  char *ans = "\r\nOK\r\n\0";
  int ansPos = 0;
  char c;
  int n;
  n = write(fd, cmd, strlen(cmd));
  if (n < strlen(cmd))
    return -1;
  printf("AT command sent\nanswer: ");
  while (ansPos < strlen(ans)) {
    do {
      n = read(fd, &c, 1);
    } while (n == 0);
    if (c == ans[ansPos])
      ansPos++;
    else if (c == ans[0])
      ansPos = 1;
    else
      ansPos = 0;
    if (c == '\r')
      printf("\\r");
    else if (c == '\n')
      printf("\\n");
    else
      printf("%c", c);
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<init_modem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    0xeae(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xea8(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    11b8 <init_modem+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <init_modem+0x187>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jge    12b2 <init_modem+0x172>
jmp    11e7 <init_modem+0xa7>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11e7 <init_modem+0xa7>
movsbl -0x1d(%rbp),%eax
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    122b <init_modem+0xeb>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1256 <init_modem+0x116>
movsbl -0x1d(%rbp),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    124a <init_modem+0x10a>
movl   $0x1,-0x1c(%rbp)
jmp    1251 <init_modem+0x111>
movl   $0x0,-0x1c(%rbp)
jmp    1256 <init_modem+0x116>
movsbl -0x1d(%rbp),%eax
cmp    $0xd,%eax
jne    1276 <init_modem+0x136>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ad <init_modem+0x16d>
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    1296 <init_modem+0x156>
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12a8 <init_modem+0x168>
movsbl -0x1d(%rbp),%esi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ad <init_modem+0x16d>
jmp    11c6 <init_modem+0x86>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_modem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xe9c(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    $0xffffffff,%ecx
cmp    %eax,%ebp
jl     1234 <init_modem+0xe4>
lea    0xe77(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe61(%rip),%r15
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    1228 <init_modem+0xd8>
lea    0x7(%rsp),%rbp
lea    0xe6d(%rip),%r12
lea    0xe63(%rip),%r13
xor    %r14d,%r14d
jmp    11e0 <init_modem+0x90>
movsbl %al,%edi
call   1030 <putchar@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    %eax,%r14d
jge    1228 <init_modem+0xd8>
nopl   (%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
je     11e0 <init_modem+0x90>
movzbl 0x7(%rsp),%eax
movslq %r14d,%rcx
xor    %r14d,%r14d
cmp    $0xd,%al
sete   %r14b
cmp    (%rcx,%r15,1),%al
lea    0x1(%rcx),%ecx
cmove  %ecx,%r14d
cmp    $0xa,%al
je     121c <init_modem+0xcc>
cmp    $0xd,%al
jne    11c8 <init_modem+0x78>
mov    %r13,%rdi
jmp    121f <init_modem+0xcf>
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11d0 <init_modem+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"at\0D\00\00"
@.str.1 = [8 x i8] c"\0D\0AOK\0D\0A\00\00"
@.str.2 = [25 x i8] c"AT command sent\0Aanswer: \00"
@.str.3 = [3 x i8] c"\\r\00"
@.str.4 = [3 x i8] c"\\n\00"
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [2 x i8] c"\0A\00"
define i32 @init_modem(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %5
store i32 0, i32* %6
%9 = load i32, i32* %3
%10 = load i8*, i8** %4
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = call i32 @write(i32 %9, i8* %10, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = icmp slt i32 %14, %16
br i1 %17, label %18, label %19
18: ; preds = %1
store i32 -1, i32* %2
br label %77
19: ; preds = %1
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %74, %19
%22 = load i32, i32* %6
%23 = load i8*, i8** %5
%24 = call i32 @strlen(i8* %23)
%25 = icmp slt i32 %22, %24
br i1 %25, label %26, label %75
26: ; preds = %21
br label %27
27: ; preds = %30, %26
%28 = load i32, i32* %3
%29 = call i32 @read(i32 %28, i8* %7, i32 1)
store i32 %29, i32* %8
br label %30
30: ; preds = %27
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %27, label %33
33: ; preds = %30
%34 = load i8, i8* %7
%35 = sext i8 %34 to i32
%36 = load i8*, i8** %5
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %35, %41
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %57
46: ; preds = %33
%47 = load i8, i8* %7
%48 = sext i8 %47 to i32
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i64 0
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %48, %52
br i1 %53, label %54, label %55
54: ; preds = %46
store i32 1, i32* %6
br label %56
55: ; preds = %46
store i32 0, i32* %6
br label %56
56: ; preds = %55, %54
br label %57
57: ; preds = %56, %43
%58 = load i8, i8* %7
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 13
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %74
63: ; preds = %57
%64 = load i8, i8* %7
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 10
br i1 %66, label %67, label %69
67: ; preds = %63
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %73
69: ; preds = %63
%70 = load i8, i8* %7
%71 = sext i8 %70 to i32
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %69, %67
br label %74
74: ; preds = %73, %61
br label %21
75: ; preds = %21
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %2
br label %77
77: ; preds = %75, %18
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
@.str = [5 x i8] c"at\0D\00\00"
@.str.1 = [8 x i8] c"\0D\0AOK\0D\0A\00\00"
@.str.2 = [25 x i8] c"AT command sent\0Aanswer: \00"
@.str.3 = [3 x i8] c"\\r\00"
@.str.4 = [3 x i8] c"\\n\00"
define i32 @init_modem(i32 %0) {
%2 = alloca i8
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %4, %5
br i1 %6, label %38, label %7
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %36
11: ; preds = %7, %33
%12 = phi i32 [ %25, %33 ], [ 0, %7 ]
br label %13
13: ; preds = %11, %13
%14 = call i32 @read(i32 %0, i8* %2, i32 1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %13, label %16
16: ; preds = %13
%17 = load i8, i8* %2
%18 = sext i32 %12 to i64
%19 = getelementptr inbounds [8 x i8], [8 x i8]* @.str.1, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %17, %20
%22 = add nsw i32 %12, 1
%23 = icmp eq i8 %17, 13
%24 = zext i1 %23 to i32
%25 = select i1 %21, i32 %22, i32 %24
switch i8 %17, label %30 [
i8 13, label %26
i8 10, label %28
]
26: ; preds = %16
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %33
28: ; preds = %16
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %33
30: ; preds = %16
%31 = sext i8 %17 to i32
%32 = tail call i32 @putchar(i32 %31)
br label %33
33: ; preds = %28, %30, %26
%34 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%35 = icmp slt i32 %25, %34
br i1 %35, label %11, label %36
36: ; preds = %33, %7
%37 = tail call i32 @putchar(i32 10)
br label %38
38: ; preds = %1, %36
%39 = phi i32 [ 0, %36 ], [ -1, %1 ]
ret i32 %39
}