#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*/ freeSegment(int, int);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ pthread_self();
int /*<<< orphan*/ useSegment(int, int);
int /*<<< orphan*/ usleep(int);
void *trainAD(void *arg) {
  while (1) {
    useSegment(0, 1);
    printf("Train X ID = %d occupies AB\n", (int)pthread_self());
    usleep(6000000);
    freeSegment(0, 1);
    printf("Train X ID = %d left AB\n", (int)pthread_self());
    useSegment(1, 2);
    printf("Train X ID = %d occupies BC\n", (int)pthread_self());
    usleep(6000000);
    freeSegment(1, 2);
    printf("Train X ID = %d left BC\n", (int)pthread_self());
    useSegment(2, 3);
    printf("Train X ID = %d occupies CD\n", (int)pthread_self());
    usleep(6000000);
    freeSegment(2, 3);
    printf("Train X ID = %d left CD [FINISHED]\n", (int)pthread_self());
    break;
  }
}


x86 assembly (clang, -O0):
<trainAD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <useSegment@plt>
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,%esi
lea    0xe88(%rip),%rdi
call   1030 <printf@plt>
mov    $0x5b8d80,%edi
call   1070 <usleep@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <freeSegment@plt>
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,%esi
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
mov    $0x2,%esi
call   1040 <useSegment@plt>
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,%esi
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x5b8d80,%edi
call   1070 <usleep@plt>
mov    $0x1,%edi
mov    $0x2,%esi
call   1060 <freeSegment@plt>
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,%esi
lea    0xe5e(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%edi
mov    $0x3,%esi
call   1040 <useSegment@plt>
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,%esi
lea    0xe53(%rip),%rdi
call   1030 <printf@plt>
mov    $0x5b8d80,%edi
call   1070 <usleep@plt>
mov    $0x2,%edi
mov    $0x3,%esi
call   1060 <freeSegment@plt>
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,%esi
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trainAD>:
push   %rax
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <useSegment@plt>
xor    %eax,%eax
call   1050 <pthread_self@plt>
lea    0xe95(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x5b8d80,%edi
call   1070 <usleep@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <freeSegment@plt>
xor    %eax,%eax
call   1050 <pthread_self@plt>
lea    0xe87(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
mov    $0x2,%esi
call   1040 <useSegment@plt>
xor    %eax,%eax
call   1050 <pthread_self@plt>
lea    0xe7c(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x5b8d80,%edi
call   1070 <usleep@plt>
mov    $0x1,%edi
mov    $0x2,%esi
call   1060 <freeSegment@plt>
xor    %eax,%eax
call   1050 <pthread_self@plt>
lea    0xe6b(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x2,%edi
mov    $0x3,%esi
call   1040 <useSegment@plt>
xor    %eax,%eax
call   1050 <pthread_self@plt>
lea    0xe60(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x5b8d80,%edi
call   1070 <usleep@plt>
mov    $0x2,%edi
mov    $0x3,%esi
call   1060 <freeSegment@plt>
xor    %eax,%eax
call   1050 <pthread_self@plt>
lea    0xe4f(%rip),%rdi
mov    %eax,%esi
pop    %rax
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Train X ID = %d occupies AB\0A\00"
@.str.1 = [25 x i8] c"Train X ID = %d left AB\0A\00"
@.str.2 = [29 x i8] c"Train X ID = %d occupies BC\0A\00"
@.str.3 = [25 x i8] c"Train X ID = %d left BC\0A\00"
@.str.4 = [29 x i8] c"Train X ID = %d occupies CD\0A\00"
@.str.5 = [36 x i8] c"Train X ID = %d left CD [FINISHED]\0A\00"
define i8* @trainAD(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %3
br label %4
4: ; preds = %1
%5 = call i32 @useSegment(i32 0, i32 1)
%6 = call i64 (...) @pthread_self()
%7 = trunc i64 %6 to i32
%8 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @usleep(i32 6000000)
%10 = call i32 @freeSegment(i32 0, i32 1)
%11 = call i64 (...) @pthread_self()
%12 = trunc i64 %11 to i32
%13 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = call i32 @useSegment(i32 1, i32 2)
%15 = call i64 (...) @pthread_self()
%16 = trunc i64 %15 to i32
%17 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @usleep(i32 6000000)
%19 = call i32 @freeSegment(i32 1, i32 2)
%20 = call i64 (...) @pthread_self()
%21 = trunc i64 %20 to i32
%22 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = call i32 @useSegment(i32 2, i32 3)
%24 = call i64 (...) @pthread_self()
%25 = trunc i64 %24 to i32
%26 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = call i32 @usleep(i32 6000000)
%28 = call i32 @freeSegment(i32 2, i32 3)
%29 = call i64 (...) @pthread_self()
%30 = trunc i64 %29 to i32
%31 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %4
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
@.str = [29 x i8] c"Train X ID = %d occupies AB\0A\00"
@.str.1 = [25 x i8] c"Train X ID = %d left AB\0A\00"
@.str.2 = [29 x i8] c"Train X ID = %d occupies BC\0A\00"
@.str.3 = [25 x i8] c"Train X ID = %d left BC\0A\00"
@.str.4 = [29 x i8] c"Train X ID = %d occupies CD\0A\00"
@.str.5 = [36 x i8] c"Train X ID = %d left CD [FINISHED]\0A\00"
define noalias i8* @trainAD(i8* nocapture readnone %0) {
%2 = tail call i32 @useSegment(i32 0, i32 1)
%3 = tail call i64 (...) @pthread_self()
%4 = trunc i64 %3 to i32
%5 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @usleep(i32 6000000)
%7 = tail call i32 @freeSegment(i32 0, i32 1)
%8 = tail call i64 (...) @pthread_self()
%9 = trunc i64 %8 to i32
%10 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i32 @useSegment(i32 1, i32 2)
%12 = tail call i64 (...) @pthread_self()
%13 = trunc i64 %12 to i32
%14 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = tail call i32 @usleep(i32 6000000)
%16 = tail call i32 @freeSegment(i32 1, i32 2)
%17 = tail call i64 (...) @pthread_self()
%18 = trunc i64 %17 to i32
%19 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = tail call i32 @useSegment(i32 2, i32 3)
%21 = tail call i64 (...) @pthread_self()
%22 = trunc i64 %21 to i32
%23 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = tail call i32 @usleep(i32 6000000)
%25 = tail call i32 @freeSegment(i32 2, i32 3)
%26 = tail call i64 (...) @pthread_self()
%27 = trunc i64 %26 to i32
%28 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %27)
ret i8* undef
}