// File: /workspace/github_cpp_makefile_star2_O1/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, 0x2387d(%rip)     # 0x486f8
jne 0x25298
movb $0x1, 0x23870(%rip)     # 0x486f8
leaq 0x10d9d(%rip), %rdi     # 0x35c2c
callq 0x1f710
testq %rax, %rax
je 0x24ea6
movq 0x220c0(%rip), %rax     # 0x46f60
movl $0x1, (%rax)
leaq 0x10d8e(%rip), %rdi     # 0x35c3b
callq 0x1f710
testq %rax, %rax
je 0x24ec4
movq 0x220f2(%rip), %rax     # 0x46fb0
movl $0x1, (%rax)
movq 0x22095(%rip), %rax     # 0x46f60
cmpl $0x0, (%rax)
je 0x24edc
leaq 0x11036(%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 0x24f14
movl $0x1, %eax
movl $0x10440, %ecx          # imm = 0x10440
btl %ebx, %ecx
jb 0x24f16
xorl %eax, %eax
movl %eax, 0x22858(%rip)     # 0x47774
callq 0x1fb20
cmpl $0x1, %eax
leaq 0x22965(%rip), %rax     # 0x47890
leaq 0x22b5e(%rip), %rcx     # 0x47a90
cmoveq %rax, %rcx
leaq 0x22a53(%rip), %rax     # 0x47990
leaq 0x22c4c(%rip), %rdx     # 0x47b90
cmoveq %rax, %rdx
movq %rcx, 0x23529(%rip)     # 0x48478
movq %rdx, 0x2352a(%rip)     # 0x48480
cmpl $0x14, %ebx
ja 0x24f71
leaq 0x1059a(%rip), %rax     # 0x354fc
movl (%rax,%rbx,4), %eax
leaq 0x105e4(%rip), %rcx     # 0x35550
movl (%rcx,%rbx,4), %ecx
jmp 0x24f7b
movl $0x25, %ecx
movl $0x3f000000, %eax       # imm = 0x3F000000
movl %eax, 0x2351f(%rip)     # 0x484a0
movl %ecx, 0x23529(%rip)     # 0x484b0
leaq 0x10cbc(%rip), %rdi     # 0x35c4a
movl $0x181002, %esi         # imm = 0x181002
xorl %eax, %eax
callq 0x1f3c0
movl %eax, %ebx
testl %eax, %eax
jns 0x24fce
leaq 0x10cac(%rip), %rdi     # 0x35c53
movl $0x181002, %esi         # imm = 0x181002
xorl %eax, %eax
callq 0x1f3c0
testl %eax, %eax
js 0x25234
movl %eax, %ebx
movl $0x0, 0x234d9(%rip)     # 0x484a0
movb $0x1, 0x2372e(%rip)     # 0x486fc
movl $0x100000, %eax         # imm = 0x100000
addl 0x234c7(%rip), %eax     # 0x484a0
movl %eax, 0x23721(%rip)     # 0x48700
movl $0x101000, %eax         # imm = 0x101000
addl 0x234b6(%rip), %eax     # 0x484a0
movl %eax, 0x23714(%rip)     # 0x48704
movl $0x200000, %eax         # imm = 0x200000
addl 0x234a5(%rip), %eax     # 0x484a0
movl %eax, 0x23707(%rip)     # 0x48708
movl $0xb000, %eax           # imm = 0xB000
addl 0x23494(%rip), %eax     # 0x484a0
movl %eax, 0x236fa(%rip)     # 0x4870c
movl $0x20c000, %eax         # imm = 0x20C000
addl 0x23483(%rip), %eax     # 0x484a0
movl %eax, 0x236ed(%rip)     # 0x48710
movl 0x236de(%rip), %r9d     # 0x48708
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x23446(%rip)     # 0x48490
cmpq $-0x1, %rax
je 0x2520a
movl 0x236b5(%rip), %r9d     # 0x48710
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x2341d(%rip)     # 0x48498
cmpq $-0x1, %rax
je 0x2521f
movl 0x23678(%rip), %r9d     # 0x48704
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x233fc(%rip)     # 0x484a8
cmpq $-0x1, %rax
je 0x25249
movl 0x23643(%rip), %r9d     # 0x48700
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x1fa50
movq %rax, 0x233ab(%rip)     # 0x48488
cmpq $-0x1, %rax
je 0x2525e
movl 0x2361e(%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, 0x23607(%rip)     # 0x48718
cmpq $-0x1, %rax
je 0x25273
movl $0x280, 0x408(%rax)     # imm = 0x280
movl $0xf9, 0x41c(%rax)
movq %rax, %rcx
addq $0x410, %rcx            # imm = 0x410
movq %rcx, 0x235e0(%rip)     # 0x48720
movq 0x23349(%rip), %rcx     # 0x48490
movq 0x21e1a(%rip), %rdx     # 0x46f68
movq %rcx, (%rdx)
movq 0x23340(%rip), %rcx     # 0x48498
movq 0x21e11(%rip), %rdx     # 0x46f70
movq %rcx, (%rdx)
movq 0x2333f(%rip), %rcx     # 0x484a8
movq 0x21de0(%rip), %rdx     # 0x46f50
movq %rcx, (%rdx)
movq 0x2330e(%rip), %rcx     # 0x48488
movq 0x21e1f(%rip), %rdx     # 0x46fa0
movq %rcx, (%rdx)
movq 0x21e35(%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, 0x23512(%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, 0x234eb(%rip)     # 0x486f0
jmp 0x25298
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b04(%rip), %rsi     # 0x35d21
jmp 0x25286
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b16(%rip), %rsi     # 0x35d48
jmp 0x25286
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10a19(%rip), %rsi     # 0x35c60
jmp 0x25286
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b12(%rip), %rsi     # 0x35d6e
jmp 0x25286
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b25(%rip), %rsi     # 0x35d96
jmp 0x25286
callq 0x1f040
movl (%rax), %edi
callq 0x1f190
leaq 0x10b37(%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