#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*/ O_RDWR;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int main(int argc, char const *argv[]) {
  if (argc < 4) {
    printf("%s\n",
           "Usage: \n\t ./train [h|d|v] <bogey sequence> <fifo_name>\n");
    exit(1);
  };
  char buf[128];
  sprintf(buf, "%s_fifo", argv[1]);
  int ends = open(buf, O_WRONLY);
  sprintf(buf, "%s_fifo", argv[3]);
  mkfifo(buf, 0666);
  int rdwr = open(buf, O_RDWR);
  if (ends == -1 || rdwr == -1) {
    printf("%s\n", "open() error");
    exit(1);
  };
  write(ends, buf, strlen(buf));
  memset(buf, 0, 128);
  int n = read(rdwr, buf, 128);
  if (n == 0 | strcmp(buf, "exit") == 0) {
    {
      printf("%s\n", "Platform Busy! Try Again !");
      exit(1);
    };
  } else {
    printf("Platform %s available\n", buf);
    sleep(0.5);
    write(rdwr, argv[2], strlen(argv[2]));
    sleep(3);
  }
  int fd = open("a_fifo", O_WRONLY);
  sprintf(buf, "%s train left Platform !", argv[3]);
  write(fd, buf, strlen(buf));
  close(fd);
  printf("Leaving station !\n");
  close(ends);
  close(rdwr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1202 <main+0x42>
lea    0xe16(%rip),%rdi
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe24(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x94(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xdee(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    $0x1b6,%esi
call   10a0 <mkfifo@plt>
lea    -0x90(%rbp),%rdi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
je     1299 <main+0xd9>
cmpl   $0xffffffff,-0x98(%rbp)
jne    12b8 <main+0xf8>
lea    0xd60(%rip),%rdi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x94(%rbp),%eax
mov    %eax,-0xb4(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1080 <read@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0x0,-0x9c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xa4(%rbp)
lea    -0x90(%rbp),%rdi
lea    0xd0c(%rip),%rsi
call   1090 <strcmp@plt>
mov    %rax,%rcx
mov    -0xa4(%rbp),%eax
cmp    $0x0,%rcx
sete   %cl
and    $0x1,%cl
movzbl %cl,%ecx
or     %ecx,%eax
cmp    $0x0,%eax
je     138a <main+0x1ca>
lea    0xc8e(%rip),%rdi
lea    0xcdd(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x90(%rbp),%rsi
lea    0xcd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10e0 <sleep@plt>
mov    -0x98(%rbp),%eax
mov    %eax,-0xc4(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0xc0(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
mov    -0xc4(%rbp),%edi
mov    -0xc0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
mov    0x2bdd(%rip),%rax
mov    (%rax),%esi
lea    0xc8c(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0xa0(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xc72(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0xa0(%rbp),%eax
mov    %eax,-0xd4(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xd4(%rbp),%edi
mov    -0xd0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0xa0(%rbp),%edi
call   1070 <close@plt>
lea    0xc3a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x94(%rbp),%edi
call   1070 <close@plt>
mov    -0x98(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
cmp    $0x3,%edi
jle    1366 <main+0x196>
mov    %rsi,%r15
mov    0x8(%rsi),%rdx
lea    0xe40(%rip),%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10d0 <sprintf@plt>
mov    0x2dc3(%rip),%r13
mov    0x0(%r13),%esi
mov    %rbp,%rdi
call   10c0 <open@plt>
mov    %eax,%r14d
mov    0x18(%r15),%rdx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10d0 <sprintf@plt>
mov    %rbp,%rdi
mov    $0x1b6,%esi
call   10b0 <mkfifo@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%r14d
je     136f <main+0x19f>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     136f <main+0x19f>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1090 <read@plt>
mov    %eax,%ebp
lea    0xdae(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcmp@plt>
test   %ebp,%ebp
je     1378 <main+0x1a8>
test   %rax,%rax
je     1378 <main+0x1a8>
lea    0xdae(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10f0 <sleep@plt>
mov    0x10(%r15),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    $0x3,%edi
call   10f0 <sleep@plt>
mov    0x0(%r13),%esi
lea    0xd83(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%ebp
mov    0x18(%r15),%rdx
lea    0xd78(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xd67(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %r12d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc93(%rip),%rdi
jmp    137f <main+0x1af>
lea    0xcca(%rip),%rdi
jmp    137f <main+0x1af>
lea    0xcd3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [56 x i8] c"Usage: \0A\09 ./train [h|d|v] <bogey sequence> <fifo_name>\0A\00"
@.str.2 = [8 x i8] c"%s_fifo\00"
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str.3 = [13 x i8] c"open() error\00"
@.str.4 = [5 x i8] c"exit\00"
@.str.5 = [27 x i8] c"Platform Busy! Try Again !\00"
@.str.6 = [23 x i8] c"Platform %s available\0A\00"
@.str.7 = [7 x i8] c"a_fifo\00"
@.str.8 = [25 x i8] c"%s train left Platform !\00"
@.str.9 = [19 x i8] c"Leaving station !\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [128 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 4
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%23 = load i32, i32* @O_WRONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %7
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 3
%28 = load i8*, i8** %27
%29 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%31 = call i32 @mkfifo(i8* %30, i32 438)
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%33 = load i32, i32* @O_RDWR
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %40, label %37
37: ; preds = %16
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %37, %16
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %37
%44 = load i32, i32* %7
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%47 = call i32 @strlen(i8* %46)
%48 = call i32 @write(i32 %44, i8* %45, i32 %47)
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%50 = call i32 @memset(i8* %49, i32 0, i32 128)
%51 = load i32, i32* %8
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 128)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, 0
%56 = zext i1 %55 to i32
%57 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%58 = call i64 @strcmp(i8* %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%59 = icmp eq i64 %58, 0
%60 = zext i1 %59 to i32
%61 = or i32 %56, %60
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %43
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %43
%67 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %67)
%69 = call i32 @sleep(i32 0)
%70 = load i32, i32* %8
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 2
%73 = load i8*, i8** %72
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 2
%76 = load i8*, i8** %75
%77 = call i32 @strlen(i8* %76)
%78 = call i32 @write(i32 %70, i8* %73, i32 %77)
%79 = call i32 @sleep(i32 3)
br label %80
80: ; preds = %66
%81 = load i32, i32* @O_WRONLY
%82 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %81)
store i32 %82, i32* %10
%83 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 3
%86 = load i8*, i8** %85
%87 = call i32 @sprintf(i8* %83, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %86)
%88 = load i32, i32* %10
%89 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%90 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%91 = call i32 @strlen(i8* %90)
%92 = call i32 @write(i32 %88, i8* %89, i32 %91)
%93 = load i32, i32* %10
%94 = call i32 @close(i32 %93)
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0))
%96 = load i32, i32* %7
%97 = call i32 @close(i32 %96)
%98 = load i32, i32* %8
%99 = call i32 @close(i32 %98)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [56 x i8] c"Usage: \0A\09 ./train [h|d|v] <bogey sequence> <fifo_name>\0A\00"
@.str.2 = [8 x i8] c"%s_fifo\00"
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str.3 = [13 x i8] c"open() error\00"
@.str.4 = [5 x i8] c"exit\00"
@.str.5 = [27 x i8] c"Platform Busy! Try Again !\00"
@.str.6 = [23 x i8] c"Platform %s available\0A\00"
@.str.7 = [7 x i8] c"a_fifo\00"
@.str.8 = [25 x i8] c"%s train left Platform !\00"
@str = [18 x i8] c"Leaving station !\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [128 x i8]
%4 = icmp slt i32 %0, 4
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %9, i32 %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
%17 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = call i32 @mkfifo(i8* %9, i32 438)
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %9, i32 %19)
%21 = icmp eq i32 %14, -1
%22 = icmp eq i32 %20, -1
%23 = or i1 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %8
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %8
%28 = call i32 @strlen(i8* %9)
%29 = call i32 @write(i32 %14, i8* %9, i32 %28)
%30 = call i32 @memset(i8* %9, i32 0, i32 128)
%31 = call i32 @read(i32 %20, i8* %9, i32 128)
%32 = icmp eq i32 %31, 0
%33 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
%35 = or i1 %32, %34
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %27
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %9)
%41 = call i32 @sleep(i32 0)
%42 = getelementptr inbounds i8*, i8** %1, i64 2
%43 = load i8*, i8** %42
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @write(i32 %20, i8* %43, i32 %44)
%46 = call i32 @sleep(i32 3)
%47 = load i32, i32* @O_WRONLY
%48 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %47)
%49 = load i8*, i8** %15
%50 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %49)
%51 = call i32 @strlen(i8* %9)
%52 = call i32 @write(i32 %48, i8* %9, i32 %51)
%53 = call i32 @close(i32 %48)
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%55 = call i32 @close(i32 %14)
%56 = call i32 @close(i32 %20)
ret i32 0
}