#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 */
scalar_t__ ReadAdrPort(char *, int);
scalar_t__ WriteAdrPort(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int init_gatectl(int nCom) {
  char sCmd[254];
  char sResult[254];
  char sMess[254];
  int iSpot;
  strcpy(sCmd, "$KE,RST");
  iSpot = strlen(sCmd);
  sCmd[iSpot] = 0x0d;
  sCmd[iSpot + 1] = 0x0a;
  sCmd[iSpot + 2] = 0x00;
  if (WriteAdrPort(sCmd) < 0) {
    return -1;
  }
  sleep(1);
  if (ReadAdrPort(sResult, 254) > 0) {
    sprintf(sMess, "****Response is %s\n", sResult);
  }
  strcpy(sCmd, "$KE,WRA,000000000111100000000000");
  iSpot = strlen(sCmd);
  sCmd[iSpot] = 0x0d;
  sCmd[iSpot + 1] = 0x0a;
  sCmd[iSpot + 2] = 0x00;
  if (WriteAdrPort(sCmd) < 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<init_gatectl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x320,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x314(%rbp)
movslq -0x314(%rbp),%rax
movb   $0xd,-0x110(%rbp,%rax,1)
0d
mov    -0x314(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0xa,-0x110(%rbp,%rax,1)
0a
mov    -0x314(%rbp),%eax
add    $0x2,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1030 <WriteAdrPort@plt>
cmp    $0x0,%rax
jge    11ea <init_gatectl+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <init_gatectl+0x14c>
mov    $0x1,%edi
call   1080 <sleep@plt>
lea    -0x210(%rbp),%rdi
mov    $0xfe,%esi
call   1060 <ReadAdrPort@plt>
cmp    $0x0,%rax
jle    1229 <init_gatectl+0xc9>
lea    -0x310(%rbp),%rdi
lea    -0x210(%rbp),%rdx
lea    0xde4(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xde5(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x314(%rbp)
movslq -0x314(%rbp),%rax
movb   $0xd,-0x110(%rbp,%rax,1)
0d
mov    -0x314(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0xa,-0x110(%rbp,%rax,1)
0a
mov    -0x314(%rbp),%eax
add    $0x2,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1030 <WriteAdrPort@plt>
cmp    $0x0,%rax
jge    12a5 <init_gatectl+0x145>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <init_gatectl+0x14c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x320,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_gatectl>:
push   %rbx
sub    $0x300,%rsp
lea    0xe91(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movw   $0xa0d,(%rsp,%rax,1)
movb   $0x0,0x2(%rsp,%rax,1)
mov    %rbx,%rdi
call   1030 <WriteAdrPort@plt>
test   %rax,%rax
js     120e <init_gatectl+0xae>
mov    $0x1,%edi
call   1080 <sleep@plt>
lea    0x100(%rsp),%rdi
mov    $0xfe,%esi
call   1060 <ReadAdrPort@plt>
test   %rax,%rax
jle    11d9 <init_gatectl+0x79>
lea    0xe44(%rip),%rsi
lea    0x200(%rsp),%rdi
lea    0x100(%rsp),%rdx
call   1070 <sprintf@plt>
lea    0xe3c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movw   $0xa0d,(%rsp,%rax,1)
movb   $0x0,0x2(%rsp,%rax,1)
mov    %rbx,%rdi
call   1030 <WriteAdrPort@plt>
sar    $0x3f,%rax
jmp    1213 <init_gatectl+0xb3>
mov    $0xffffffff,%eax
add    $0x300,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"$KE,RST\00"
@.str.1 = [20 x i8] c"****Response is %s\0A\00"
@.str.2 = [33 x i8] c"$KE,WRA,000000000111100000000000\00"
define i32 @init_gatectl(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [254 x i8]
%5 = alloca [254 x i8]
%6 = alloca [254 x i8]
%7 = alloca i32
store i32 %0, i32* %3
%8 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 0
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 %13
store i8 13, i8* %14
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 %17
store i8 10, i8* %18
%19 = load i32, i32* %7
%20 = add nsw i32 %19, 2
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 0
%24 = call i64 @WriteAdrPort(i8* %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %1
store i32 -1, i32* %2
br label %57
27: ; preds = %1
%28 = call i32 @sleep(i32 1)
%29 = getelementptr inbounds [254 x i8], [254 x i8]* %5, i64 0, i64 0
%30 = call i64 @ReadAdrPort(i8* %29, i32 254)
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = getelementptr inbounds [254 x i8], [254 x i8]* %6, i64 0, i64 0
%34 = getelementptr inbounds [254 x i8], [254 x i8]* %5, i64 0, i64 0
%35 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %32, %27
%37 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 0
%38 = call i32 @strcpy(i8* %37, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%39 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 %42
store i8 13, i8* %43
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 %46
store i8 10, i8* %47
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 2
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 %50
store i8 0, i8* %51
%52 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 0
%53 = call i64 @WriteAdrPort(i8* %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %36
store i32 -1, i32* %2
br label %57
56: ; preds = %36
store i32 0, i32* %2
br label %57
57: ; preds = %56, %55, %26
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@.str = [8 x i8] c"$KE,RST\00"
@.str.1 = [20 x i8] c"****Response is %s\0A\00"
@.str.2 = [33 x i8] c"$KE,WRA,000000000111100000000000\00"
define i32 @init_gatectl(i32 %0) {
%2 = alloca [254 x i8]
%3 = alloca [254 x i8]
%4 = alloca [254 x i8]
%5 = getelementptr inbounds [254 x i8], [254 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [254 x i8], [254 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [254 x i8], [254 x i8]* %4, i64 0, i64 0
%8 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @strlen(i8* %5)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [254 x i8], [254 x i8]* %2, i64 0, i64 %10
store i8 13, i8* %11
%12 = add nsw i32 %9, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [254 x i8], [254 x i8]* %2, i64 0, i64 %13
store i8 10, i8* %14
%15 = add nsw i32 %9, 2
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [254 x i8], [254 x i8]* %2, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i64 @WriteAdrPort(i8* %5)
%19 = icmp slt i64 %18, 0
br i1 %19, label %40, label %20
20: ; preds = %1
%21 = call i32 @sleep(i32 1)
%22 = call i64 @ReadAdrPort(i8* %6, i32 254)
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %26
26: ; preds = %24, %20
%27 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @strlen(i8* %5)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [254 x i8], [254 x i8]* %2, i64 0, i64 %29
store i8 13, i8* %30
%31 = add nsw i32 %28, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [254 x i8], [254 x i8]* %2, i64 0, i64 %32
store i8 10, i8* %33
%34 = add nsw i32 %28, 2
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [254 x i8], [254 x i8]* %2, i64 0, i64 %35
store i8 0, i8* %36
%37 = call i64 @WriteAdrPort(i8* %5)
%38 = ashr i64 %37, 63
%39 = trunc i64 %38 to i32
br label %40
40: ; preds = %26, %1
%41 = phi i32 [ -1, %1 ], [ %39, %26 ]
ret i32 %41
}