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

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
int initmc(int fd) {
  char chout = 'x';
  char *sendc;
  char *recvc;
  int i = 0;
  sendc = (char *)malloc(255);
  recvc = (char *)malloc(255);
  recvc[0] = '\0';
  sendc[0] = 1;
  write(fd, sendc, 1);
  printf("turning on\n");
  while (1) {
    read(fd, &chout, sizeof(chout));
    recvc[i] = chout;
    i++;
    recvc[i] = '\0';
    if (chout == 13)
      break;
    chout = 0;
    usleep(20000);
  }
  printf("%s\n", recvc);
  if (strcmp(recvc, "CCTRL-BASIC Version 1.1 (20.12.96)\r") == 0)
    return 0;
  else
    return -1;
}


x86 assembly (clang, -O0):
<initmc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x78,-0x9(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0xff,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0xff,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    -0x9(%rbp),%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movsbl -0x9(%rbp),%eax
cmp    $0xd,%eax
jne    1205 <initmc+0xa5>
jmp    1218 <initmc+0xb8>
movb   $0x0,-0x9(%rbp)
mov    $0x4e20,%edi
call   1080 <usleep@plt>
jmp    11bf <initmc+0x5f>
mov    -0x20(%rbp),%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1250 <initmc+0xf0>
movl   $0x0,-0x4(%rbp)
jmp    1257 <initmc+0xf7>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initmc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
movb   $0x78,0x7(%rsp)
mov    $0xff,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    $0xff,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
movb   $0x1,(%r15)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    0x7(%rsp),%al
mov    %al,(%r14)
movb   $0x0,0x1(%r14)
cmp    $0xd,%al
je     1211 <initmc+0xb1>
mov    %r14,%rbx
add    $0x2,%rbx
lea    0x7(%rsp),%r15
nopw   0x0(%rax,%rax,1)
movb   $0x0,0x7(%rsp)
mov    $0x4e20,%edi
call   1080 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl 0x7(%rsp),%eax
mov    %al,-0x1(%rbx)
movb   $0x0,(%rbx)
add    $0x1,%rbx
cmp    $0xd,%al
jne    11e0 <initmc+0x80>
mov    %r14,%rdi
call   1030 <puts@plt>
lea    0xde0(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
neg    %rax
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"turning on\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [36 x i8] c"CCTRL-BASIC Version 1.1 (20.12.96)\0D\00"
define i32 @initmc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8 120, i8* %4
store i32 0, i32* %7
%8 = call i64 @malloc(i32 255)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %5
%10 = call i64 @malloc(i32 255)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 1, i8* %15
%16 = load i32, i32* %3
%17 = load i8*, i8** %5
%18 = call i32 @write(i32 %16, i8* %17, i32 1)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %1, %38
%21 = load i32, i32* %3
%22 = call i32 @read(i32 %21, i8* %4, i32 1)
%23 = load i8, i8* %4
%24 = load i8*, i8** %6
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 %23, i8* %27
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i8, i8* %4
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 13
br i1 %36, label %37, label %38
37: ; preds = %20
br label %40
38: ; preds = %20
store i8 0, i8* %4
%39 = call i32 @usleep(i32 20000)
br label %20
40: ; preds = %37
%41 = load i8*, i8** %6
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = load i8*, i8** %6
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
store i32 0, i32* %2
br label %48
47: ; preds = %40
store i32 -1, i32* %2
br label %48
48: ; preds = %47, %46
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
@.str.2 = [36 x i8] c"CCTRL-BASIC Version 1.1 (20.12.96)\0D\00"
@str = [11 x i8] c"turning on\00"
define i32 @initmc(i32 %0) {
%2 = alloca i8
store i8 120, i8* %2
%3 = tail call i64 @malloc(i32 255)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @malloc(i32 255)
%6 = inttoptr i64 %5 to i8*
store i8 0, i8* %6
store i8 1, i8* %4
%7 = tail call i32 @write(i32 %0, i8* %4, i32 1)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%9 = call i32 @read(i32 %0, i8* %2, i32 1)
%10 = load i8, i8* %2
store i8 %10, i8* %6
%11 = getelementptr inbounds i8, i8* %6, i64 1
store i8 0, i8* %11
%12 = icmp eq i8 %10, 13
br i1 %12, label %22, label %13
13: ; preds = %1, %13
%14 = phi i64 [ %19, %13 ], [ 1, %1 ]
store i8 0, i8* %2
%15 = tail call i32 @usleep(i32 20000)
%16 = call i32 @read(i32 %0, i8* %2, i32 1)
%17 = load i8, i8* %2
%18 = getelementptr inbounds i8, i8* %6, i64 %14
store i8 %17, i8* %18
%19 = add nuw i64 %14, 1
%20 = getelementptr inbounds i8, i8* %6, i64 %19
store i8 0, i8* %20
%21 = icmp eq i8 %17, 13
br i1 %21, label %22, label %13
22: ; preds = %13, %1
%23 = tail call i32 @puts(i8* %6)
%24 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
%26 = sext i1 %25 to i32
ret i32 %26
}