// File: /workspace/github_cpp_makefile_star2_O3/mrlordkaj[P]i2cdevlib-pi/Arduino/wiringPi/wiringPi.c
// Function: wiringPiSetup
int wiringPiSetup (void)
{
  int   fd ;
  int   model, rev, mem, maker, overVolted ;

  if (wiringPiSetuped)
    return 0 ;

  wiringPiSetuped = TRUE ;

  if (getenv (ENV_DEBUG) != NULL)
    wiringPiDebug = TRUE ;

  if (getenv (ENV_CODES) != NULL)
    wiringPiReturnCodes = TRUE ;

  if (wiringPiDebug)
    printf ("wiringPi: wiringPiSetup called\n") ;

// Get the board ID information. We're not really using the information here,
//	but it will give us information like the GPIO layout scheme (2 variants
//	on the older 26-pin Pi's) and the GPIO peripheral base address.
//	and if we're running on a compute module, then wiringPi pin numbers
//	don't really many anything, so force native BCM mode anyway.

  piBoardId (&model, &rev, &mem, &maker, &overVolted) ;

  if ((model == PI_MODEL_CM) ||
      (model == PI_MODEL_CM3) ||
      (model == PI_MODEL_CM3P))
    wiringPiMode = WPI_MODE_GPIO ;
  else
    wiringPiMode = WPI_MODE_PINS ;

  /**/ if (piGpioLayout () == 1)	// A, B, Rev 1, 1.1
  {
     pinToGpio =  pinToGpioR1 ;
    physToGpio = physToGpioR1 ;
  }
  else 					// A2, B2, A+, B+, CM, Pi2, Pi3, Zero, Zero W, Zero 2 W
  {
     pinToGpio =  pinToGpioR2 ;
    physToGpio = physToGpioR2 ;
  }

// ...

  switch (model)
  {
    case PI_MODEL_A:	case PI_MODEL_B:
    case PI_MODEL_AP:	case PI_MODEL_BP:
    case PI_ALPHA:	case PI_MODEL_CM:
    case PI_MODEL_ZERO:	case PI_MODEL_ZERO_W:
      piGpioBase = GPIO_PERI_BASE_OLD ;
      piGpioPupOffset = GPPUD ;
      break ;

    case PI_MODEL_4B:
    case PI_MODEL_400:
    case PI_MODEL_CM4:
      piGpioBase = GPIO_PERI_BASE_2711 ;
      piGpioPupOffset = GPPUPPDN0 ;
      break ;

    default:
      piGpioBase = GPIO_PERI_BASE_2835 ;
      piGpioPupOffset = GPPUD ;
      break ;
  }

// Open the master /dev/ memory control device
// Device strategy: December 2016:
//	Try /dev/mem. If that fails, then
//	try /dev/gpiomem. If that fails then game over.

  if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
  {
    if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0)	// We're using gpiomem
    {
      piGpioBase   = 0 ;
      usingGpioMem = TRUE ;
    }
    else
      return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: %s.\n"
	"  Aborting your program because if it can not access the GPIO\n"
	"  hardware then it most certianly won't work\n"
	"  Try running with sudo?\n", strerror (errno)) ;
  }

// Set the offsets into the memory interface.

  GPIO_PADS 	  = piGpioBase + 0x00100000 ;
  GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
  GPIO_BASE	  = piGpioBase + 0x00200000 ;
  GPIO_TIMER	  = piGpioBase + 0x0000B000 ;
  GPIO_PWM	  = piGpioBase + 0x0020C000 ;

// Map the individual hardware components

//	GPIO:

  gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  if (gpio == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;

//	PWM

  pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  if (pwm == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;

//	Clock control (needed for PWM)

  clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  if (clk == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;

//	The drive pads

  pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  if (pads == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;

//	The system timer

  timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  if (timer == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;

// Set the timer to free-running, 1MHz.
//	0xF9 is 249, the timer divide is base clock / (divide+1)
//	so base clock is 250MHz / 250 = 1MHz.

  *(timer + TIMER_CONTROL) = 0x0000280 ;
  *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  timerIrqRaw = timer + TIMER_IRQ_RAW ;

// Export the base addresses for any external software that might need them

  _wiringPiGpio  = gpio ;
  _wiringPiPwm   = pwm ;
  _wiringPiClk   = clk ;
  _wiringPiPads  = pads ;
  _wiringPiTimer = timer ;

  initialiseEpoch () ;

  return 0 ;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x28, %rsp
xorl %r14d, %r14d
cmpb $0x0, 0x238e2(%rip)     # 0x486f8
jne 0x25233
movb $0x1, 0x238d5(%rip)     # 0x486f8
leaq 0x10e02(%rip), %rdi     # 0x35c2c
callq 0x1f710
testq %rax, %rax
je 0x24e41
movq 0x22125(%rip), %rax     # 0x46f60
movl $0x1, (%rax)
leaq 0x10df3(%rip), %rdi     # 0x35c3b
callq 0x1f710
testq %rax, %rax
je 0x24e5f
movq 0x22157(%rip), %rax     # 0x46fb0
movl $0x1, (%rax)
movq 0x220fa(%rip), %rax     # 0x46f60
cmpl $0x0, (%rax)
je 0x24e77
leaq 0x1109b(%rip), %rdi     # 0x35f0d
callq 0x1f900
leaq 0x14(%rsp), %rbx
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x4(%rsp), %r8
movq %rbx, %rdi
callq 0x1f280
movl (%rbx), %ebx
cmpq $0x10, %rbx
ja 0x24eaf
movl $0x1, %eax
movl $0x10440, %ecx          # imm = 0x10440
btl %ebx, %ecx
jb 0x24eb1
xorl %eax, %eax
movl %eax, 0x228bd(%rip)     # 0x47774
callq 0x1fb20
cmpl $0x1, %eax
leaq 0x229ca(%rip), %rax     # 0x47890
leaq 0x22bc3(%rip), %rcx     # 0x47a90
cmoveq %rax, %rcx
leaq 0x22ab8(%rip), %rax     # 0x47990
leaq 0x22cb1(%rip), %rdx     # 0x47b90
cmoveq %rax, %rdx
movq %rcx, 0x2358e(%rip)     # 0x48478
movq %rdx, 0x2358f(%rip)     # 0x48480
cmpl $0x14, %ebx
ja 0x24f0c
leaq 0x105ff(%rip), %rax     # 0x354fc
movl (%rax,%rbx,4), %eax
leaq 0x10649(%rip), %rcx     # 0x35550
movl (%rcx,%rbx,4), %ecx
jmp 0x24f16
movl $0x25, %ecx
movl $0x3f000000, %eax       # imm = 0x3F000000
movl %eax, 0x23584(%rip)     # 0x484a0
movl %ecx, 0x2358e(%rip)     # 0x484b0
leaq 0x10d21(%rip), %rdi     # 0x35c4a
movl $0x181002, %esi         # imm = 0x181002
xorl %eax, %eax
callq 0x1f3c0
movl %eax, %ebx
testl %eax, %eax
jns 0x24f69
leaq 0x10d11(%rip), %rdi     # 0x35c53
movl $0x181002, %esi         # imm = 0x181002
xorl %eax, %eax
callq 0x1f3c0
testl %eax, %eax
js 0x251cf
movl %eax, %ebx
movl $0x0, 0x2353e(%rip)     # 0x484a0
movb $0x1, 0x23793(%rip)     # 0x486fc
movl $0x100000, %eax         # imm = 0x100000
addl 0x2352c(%rip), %eax     # 0x484a0
movl %eax, 0x23786(%rip)     # 0x48700
movl $0x101000, %eax         # imm = 0x101000
addl 0x2351b(%rip), %eax     # 0x484a0
movl %eax, 0x23779(%rip)     # 0x48704
movl $0x200000, %eax         # imm = 0x200000
addl 0x2350a(%rip), %eax     # 0x484a0
movl %eax, 0x2376c(%rip)     # 0x48708
movl $0xb000, %eax           # imm = 0xB000
addl 0x234f9(%rip), %eax     # 0x484a0
movl %eax, 0x2375f(%rip)     # 0x4870c
movl $0x20c000, %eax         # imm = 0x20C000
addl 0x234e8(%rip), %eax     # 0x484a0
movl %eax, 0x23752(%rip)     # 0x48710
movl 0x23743(%rip), %r9d     # 0x48708
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x234ab(%rip)     # 0x48490
cmpq $-0x1, %rax
je 0x251a5
movl 0x2371a(%rip), %r9d     # 0x48710
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x23482(%rip)     # 0x48498
cmpq $-0x1, %rax
je 0x251ba
movl 0x236dd(%rip), %r9d     # 0x48704
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x23461(%rip)     # 0x484a8
cmpq $-0x1, %rax
je 0x251e4
movl 0x236a8(%rip), %r9d     # 0x48700
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x23410(%rip)     # 0x48488
cmpq $-0x1, %rax
je 0x251f9
movl 0x23683(%rip), %r9d     # 0x4870c
xorl %r14d, %r14d
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x2366c(%rip)     # 0x48718
cmpq $-0x1, %rax
je 0x2520e
movl $0x280, 0x408(%rax)     # imm = 0x280
movl $0xf9, 0x41c(%rax)
movq %rax, %rcx
addq $0x410, %rcx            # imm = 0x410
movq %rcx, 0x23645(%rip)     # 0x48720
movq 0x233ae(%rip), %rcx     # 0x48490
movq 0x21e7f(%rip), %rdx     # 0x46f68
movq %rcx, (%rdx)
movq 0x233a5(%rip), %rcx     # 0x48498
movq 0x21e76(%rip), %rdx     # 0x46f70
movq %rcx, (%rdx)
movq 0x233a4(%rip), %rcx     # 0x484a8
movq 0x21e45(%rip), %rdx     # 0x46f50
movq %rcx, (%rdx)
movq 0x23373(%rip), %rcx     # 0x48488
movq 0x21e84(%rip), %rdx     # 0x46fa0
movq %rcx, (%rdx)
movq 0x21e9a(%rip), %rcx     # 0x46fc0
movq %rax, (%rcx)
leaq 0x18(%rsp), %rbx
movl $0x4, %edi
movq %rbx, %rsi
callq 0x1f530
movq (%rbx), %rsi
movq 0x8(%rbx), %rcx
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
movq %rcx, %rax
imulq %rdx
imulq $0x3e8, %rsi, %rax      # imm = 0x3E8
movq %rdx, %rdi
shrq $0x3f, %rdi
sarq $0x12, %rdx
addq %rdi, %rdx
addq %rax, %rdx
movq %rdx, 0x23577(%rip)     # 0x486e8
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
movq %rcx, %rax
imulq %rdx
imulq $0xf4240, %rsi, %rax    # imm = 0xF4240
movq %rdx, %rcx
shrq $0x3f, %rcx
sarq $0x7, %rdx
addq %rcx, %rdx
addq %rax, %rdx
movq %rdx, 0x23550(%rip)     # 0x486f0
jmp 0x25233
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b69(%rip), %rsi     # 0x35d21
jmp 0x25221
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b7b(%rip), %rsi     # 0x35d48
jmp 0x25221
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10a7e(%rip), %rsi     # 0x35c60
jmp 0x25221
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b77(%rip), %rsi     # 0x35d6e
jmp 0x25221
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b8a(%rip), %rsi     # 0x35d96
jmp 0x25221
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b9c(%rip), %rsi     # 0x35dbd
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1f270
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
retq