#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 */
struct timespec {
  int tv_nsec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int getpid();
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
void cross(int direction) {
  if (direction == 1)
    printf("\nPID: %d IS CROSSING LEFT TO RIGHT NOW", getpid());
  else
    printf("\nPID: %d IS CROSSING RIGHT TO LEFT NOW", getpid());
  struct timespec sleepValue = {0};
  sleepValue.tv_nsec = 500 * 1000000;
  nanosleep(&sleepValue, NULL);
  printf("\nPID: %d HAS CROSSED NOW", getpid());
}


x86 assembly (clang, -O0):
<cross>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    116f <cross+0x2f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe9b(%rip),%rdi
call   1040 <printf@plt>
jmp    1184 <cross+0x44>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xea8(%rip),%rdi
call   1040 <printf@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
movl   $0x1dcd6500,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <nanosleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe96(%rip),%rdi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cross>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    $0x1,%ebx
lea    0xeb8(%rip),%rcx
lea    0xed8(%rip),%rdi
cmove  %rcx,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
movq   $0x1dcd6500,0x8(%rsp)
cd 1d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <nanosleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xed1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [39 x i8] c"\0APID: %d IS CROSSING LEFT TO RIGHT NOW\00"
@.str.1 = [39 x i8] c"\0APID: %d IS CROSSING RIGHT TO LEFT NOW\00"
@.str.2 = [25 x i8] c"\0APID: %d HAS CROSSED NOW\00"
define void @cross(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i32 (...) @getpid()
%8 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %12
9: ; preds = %1
%10 = call i32 (...) @getpid()
%11 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %9, %6
%13 = bitcast %struct.timespec* %3 to i8*
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 500000000, i32* %14
%15 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
%16 = call i32 (...) @getpid()
%17 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %16)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [39 x i8] c"\0APID: %d IS CROSSING LEFT TO RIGHT NOW\00"
@.str.1 = [39 x i8] c"\0APID: %d IS CROSSING RIGHT TO LEFT NOW\00"
@.str.2 = [25 x i8] c"\0APID: %d HAS CROSSED NOW\00"
define void @cross(i32 %0) {
%2 = alloca i64
%3 = icmp eq i32 %0, 1
%4 = tail call i32 (...) @getpid()
%5 = select i1 %3, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0)
%6 = tail call i32 @printf(i8* %5, i32 %4)
%7 = bitcast i64* %2 to %struct.timespec*
%8 = bitcast i64* %2 to i8*
store i64 500000000, i64* %2
%9 = call i32 @nanosleep(%struct.timespec* %7, i32* null)
%10 = call i32 (...) @getpid()
%11 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %10)
ret void
}