#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__ WriteAdrPort(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
int open_gate(int nGate) {
  char sCmd[254];
  int iSpot;
  sprintf(sCmd, "$KE,WR,%u,1", nGate);
  iSpot = strlen(sCmd);
  sCmd[iSpot] = 0x0d;
  sCmd[iSpot + 1] = 0x0a;
  sCmd[iSpot + 2] = 0x00;
  if (WriteAdrPort(sCmd) < 0) {
    return -1;
  }
  sprintf(sCmd, "$KE,WR,%u,0", nGate);
  iSpot = strlen(sCmd);
  sCmd[iSpot] = 0x0d;
  sCmd[iSpot + 1] = 0x0a;
  sCmd[iSpot + 2] = 0x00;
  if (WriteAdrPort(sCmd) < 0) {
    return -1;
  }
  sleep(1);
  sprintf(sCmd, "$KE,WR,%u,1", nGate);
  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):
<open_gate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xea1(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x114(%rbp)
movslq -0x114(%rbp),%rax
movb   $0xd,-0x110(%rbp,%rax,1)
0d
mov    -0x114(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0xa,-0x110(%rbp,%rax,1)
0a
mov    -0x114(%rbp),%eax
add    $0x2,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1030 <WriteAdrPort@plt>
cmp    $0x0,%rax
jge    11cd <open_gate+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <open_gate+0x19c>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe2e(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x114(%rbp)
movslq -0x114(%rbp),%rax
movb   $0xd,-0x110(%rbp,%rax,1)
0d
mov    -0x114(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0xa,-0x110(%rbp,%rax,1)
0a
mov    -0x114(%rbp),%eax
add    $0x2,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1030 <WriteAdrPort@plt>
cmp    $0x0,%rax
jge    124c <open_gate+0x10c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <open_gate+0x19c>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xd99(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x114(%rbp)
movslq -0x114(%rbp),%rax
movb   $0xd,-0x110(%rbp,%rax,1)
0d
mov    -0x114(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0xa,-0x110(%rbp,%rax,1)
0a
mov    -0x114(%rbp),%eax
add    $0x2,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1030 <WriteAdrPort@plt>
cmp    $0x0,%rax
jge    12d5 <open_gate+0x195>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <open_gate+0x19c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_gate>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %edi,%ebp
lea    0xeac(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movw   $0xa0d,(%rsp,%rax,1)
movb   $0x0,0x2(%rsp,%rax,1)
mov    %rbx,%rdi
call   1030 <WriteAdrPort@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     1200 <open_gate+0xc0>
lea    0xe7d(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   1050 <sprintf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
movw   $0xa0d,(%rsp,%rax,1)
movb   $0x0,0x2(%rsp,%rax,1)
mov    %r14,%rdi
call   1030 <WriteAdrPort@plt>
test   %rax,%rax
js     1200 <open_gate+0xc0>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xe31(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movw   $0xa0d,(%rsp,%rax,1)
movb   $0x0,0x2(%rsp,%rax,1)
mov    %rbx,%rdi
call   1030 <WriteAdrPort@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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