// File: mrlordkaj[P]i2cdevlib-pi/Arduino/wiringPi/wiringPi.c
// Function: wiringPiISR
int wiringPiISR (int pin, int mode, void (*function)(void))
{
  pthread_t threadId ;
  const char *modeS ;
  char fName   [64] ;
  char  pinS [8] ;
  pid_t pid ;
  int   count, i ;
  char  c ;
  int   bcmGpioPin ;

  if ((pin < 0) || (pin > 63))
    return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;

  /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
    return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  else if (wiringPiMode == WPI_MODE_PINS)
    bcmGpioPin = pinToGpio [pin] ;
  else if (wiringPiMode == WPI_MODE_PHYS)
    bcmGpioPin = physToGpio [pin] ;
  else
    bcmGpioPin = pin ;

// Now export the pin and set the right edge
//	We're going to use the gpio program to do this, so it assumes
//	a full installation of wiringPi. It's a bit 'clunky', but it
//	is a way that will work when we're running in "Sys" mode, as
//	a non-root user. (without sudo)

  if (mode != INT_EDGE_SETUP)
  {
    /**/ if (mode == INT_EDGE_FALLING)
      modeS = "falling" ;
    else if (mode == INT_EDGE_RISING)
      modeS = "rising" ;
    else
      modeS = "both" ;

    sprintf (pinS, "%d", bcmGpioPin) ;

    if ((pid = fork ()) < 0)	// Fail
      return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;

    if (pid == 0)	// Child, exec
    {
      /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
      {
	execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
	return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
      }
      else if (access ("/usr/bin/gpio", X_OK) == 0)
      {
	execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
	return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
      }
      else
	return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
    }
    else		// Parent, wait
      waitpid (pid, NULL, 0) ;
  }

// Now pre-open the /sys/class node - but it may already be open if
//	we are in Sys mode...

  if (sysFds [bcmGpioPin] == -1)
  {
    sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
    if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
      return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
  }

// Clear any initial pending interrupt

  ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
  for (i = 0 ; i < count ; ++i)
    read (sysFds [bcmGpioPin], &c, 1) ;

  isrFunctions [pin] = function ;

  pthread_mutex_lock (&pinMutex) ;
    pinPass = pin ;
    pthread_create (&threadId, NULL, interruptHandler, NULL) ;
    while (pinPass != -1)
      delay (1) ;
  pthread_mutex_unlock (&pinMutex) ;

  return 0 ;
}

// Assembly:
subq $0x98, %rsp
movl %edi, 0x90(%rsp)
movl %esi, 0x8c(%rsp)
movq %rdx, 0x80(%rsp)
cmpl $0x0, 0x90(%rsp)
jl 0x2cc41
cmpl $0x3f, 0x90(%rsp)
jle 0x2cc67
movl 0x90(%rsp), %edx
movl $0x1, %edi
leaq 0x1eb16(%rip), %rsi     # 0x4b76a
movb $0x0, %al
callq 0x225a0
movl %eax, 0x94(%rsp)
jmp 0x2cffc
cmpl $-0x1, 0x30066(%rip)    # 0x5ccd4
jne 0x2cc8f
movl $0x1, %edi
leaq 0x1eb12(%rip), %rsi     # 0x4b78e
movb $0x0, %al
callq 0x225a0
movl %eax, 0x94(%rsp)
jmp 0x2cffc
cmpl $0x0, 0x3003e(%rip)     # 0x5ccd4
jne 0x2ccb0
movq 0x30ff9(%rip), %rax     # 0x5dc98
movslq 0x90(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x14(%rsp)
jmp 0x2ccde
cmpl $0x3, 0x3001d(%rip)     # 0x5ccd4
jne 0x2ccd1
movq 0x30fe0(%rip), %rax     # 0x5dca0
movslq 0x90(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x14(%rsp)
jmp 0x2ccdc
movl 0x90(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x2ccde
jmp 0x2cce0
cmpl $0x0, 0x8c(%rsp)
je 0x2ce98
cmpl $0x1, 0x8c(%rsp)
jne 0x2cd06
leaq 0x1ead4(%rip), %rax     # 0x4b7d3
movq %rax, 0x70(%rsp)
jmp 0x2cd2c
cmpl $0x2, 0x8c(%rsp)
jne 0x2cd1e
leaq 0x1eac4(%rip), %rax     # 0x4b7db
movq %rax, 0x70(%rsp)
jmp 0x2cd2a
leaq 0x1eabd(%rip), %rax     # 0x4b7e2
movq %rax, 0x70(%rsp)
jmp 0x2cd2c
leaq 0x28(%rsp), %rdi
movl 0x14(%rsp), %edx
leaq 0x1eaab(%rip), %rsi     # 0x4b7e7
movb $0x0, %al
callq 0x220d0
callq 0x22210
movl %eax, 0x24(%rsp)
cmpl $0x0, %eax
jge 0x2cd7f
callq 0x22050
movl (%rax), %edi
callq 0x222f0
movq %rax, %rdx
movl $0x1, %edi
leaq 0x1ea7e(%rip), %rsi     # 0x4b7ea
movb $0x0, %al
callq 0x225a0
movl %eax, 0x94(%rsp)
jmp 0x2cffc
cmpl $0x0, 0x24(%rsp)
jne 0x2ce87
leaq 0x1ea77(%rip), %rdi     # 0x4b808
movl $0x1, %esi
callq 0x23260
cmpl $0x0, %eax
jne 0x2cdf9
leaq 0x28(%rsp), %rcx
movq 0x70(%rsp), %r8
leaq 0x1ea57(%rip), %rdi     # 0x4b808
leaq 0x1ea5f(%rip), %rsi     # 0x4b817
leaq 0x1ea5d(%rip), %rdx     # 0x4b81c
xorl %eax, %eax
movl %eax, %r9d
movb $0x0, %al
callq 0x22d70
callq 0x22050
movl (%rax), %edi
callq 0x222f0
movq %rax, %rdx
movl $0x1, %edi
leaq 0x1ea3b(%rip), %rsi     # 0x4b821
movb $0x0, %al
callq 0x225a0
movl %eax, 0x94(%rsp)
jmp 0x2cffc
leaq 0x1ea40(%rip), %rdi     # 0x4b840
movl $0x1, %esi
callq 0x23260
cmpl $0x0, %eax
jne 0x2ce68
leaq 0x28(%rsp), %rcx
movq 0x70(%rsp), %r8
leaq 0x1ea20(%rip), %rdi     # 0x4b840
leaq 0x1e9f0(%rip), %rsi     # 0x4b817
leaq 0x1e9ee(%rip), %rdx     # 0x4b81c
xorl %eax, %eax
movl %eax, %r9d
movb $0x0, %al
callq 0x22d70
callq 0x22050
movl (%rax), %edi
callq 0x222f0
movq %rax, %rdx
movl $0x1, %edi
leaq 0x1e9cc(%rip), %rsi     # 0x4b821
movb $0x0, %al
callq 0x225a0
movl %eax, 0x94(%rsp)
jmp 0x2cffc
movl $0x1, %edi
leaq 0x1e9da(%rip), %rsi     # 0x4b84e
movb $0x0, %al
callq 0x225a0
movl %eax, 0x94(%rsp)
jmp 0x2cffc
movl 0x24(%rsp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x228a0
jmp 0x2ce98
movslq 0x14(%rsp), %rcx
leaq 0x2ff7c(%rip), %rax     # 0x5ce20
cmpl $-0x1, (%rax,%rcx,4)
jne 0x2cf25
leaq 0x30(%rsp), %rdi
movl 0x14(%rsp), %edx
leaq 0x1e9ba(%rip), %rsi     # 0x4b874
movb $0x0, %al
callq 0x220d0
leaq 0x30(%rsp), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x227b0
movslq 0x14(%rsp), %rdx
leaq 0x2ff42(%rip), %rcx     # 0x5ce20
movl %eax, (%rcx,%rdx,4)
cmpl $0x0, %eax
jge 0x2cf23
leaq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
callq 0x22050
movl (%rax), %edi
callq 0x222f0
movq 0x8(%rsp), %rdx
movq %rax, %rcx
movl $0x1, %edi
leaq 0x1e981(%rip), %rsi     # 0x4b891
movb $0x0, %al
callq 0x225a0
movl %eax, 0x94(%rsp)
jmp 0x2cffc
jmp 0x2cf25
movslq 0x14(%rsp), %rcx
leaq 0x2feef(%rip), %rax     # 0x5ce20
movl (%rax,%rcx,4), %edi
movl $0x541b, %esi           # imm = 0x541B
leaq 0x20(%rsp), %rdx
movb $0x0, %al
callq 0x22f60
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x20(%rsp), %eax
jge 0x2cf82
movslq 0x14(%rsp), %rcx
leaq 0x2febd(%rip), %rax     # 0x5ce20
movl (%rax,%rcx,4), %edi
leaq 0x1b(%rsp), %rsi
movl $0x1, %edx
callq 0x23030
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x2cf4d
movq 0x80(%rsp), %rdx
movslq 0x90(%rsp), %rcx
leaq 0x30d47(%rip), %rax     # 0x5dce0
movq %rdx, (%rax,%rcx,8)
leaq 0x30f3c(%rip), %rdi     # 0x5dee0
callq 0x23040
movl 0x90(%rsp), %eax
movl %eax, 0x3002a(%rip)     # 0x5cfe0
leaq 0x78(%rsp), %rdi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x4a(%rip), %rdx        # 0x2d010
movq %rcx, %rsi
callq 0x233f0
movl 0x3000c(%rip), %eax     # 0x5cfe0
cmpl $-0x1, %eax
je 0x2cfe5
movl $0x1, %edi
callq 0x228c0
jmp 0x2cfce
leaq 0x30ef4(%rip), %rdi     # 0x5dee0
callq 0x22800
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nopl (%rax,%rax)