// File: mrlordkaj[P]i2cdevlib-pi/Arduino/wiringPi/wiringPi.c
// Function: piGpioLayout
int piGpioLayout (void)
{
  FILE *cpuFd ;
  char line [120] ;
  char *c ;
  static int  gpioLayout = -1 ;

  if (gpioLayout != -1)	// No point checking twice
    return gpioLayout ;

  if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
    piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;

// Start by looking for the Architecture to make sure we're really running
//	on a Pi. I'm getting fed-up with people whinging at me because
//	they can't get it to work on weirdFruitPi boards...

  while (fgets (line, 120, cpuFd) != NULL)
    if (strncmp (line, "Hardware", 8) == 0)
      break ;

  if (strncmp (line, "Hardware", 8) != 0)
    piGpioLayoutOops ("No \"Hardware\" line") ;

  if (wiringPiDebug)
    printf ("piGpioLayout: Hardware: %s\n", line) ;

// See if it's BCM2708 or BCM2709 or the new BCM2835.

// OK. As of Kernel 4.8,  we have BCM2835 only, regardless of model.
//	However I still want to check because it will trap the cheapskates and rip-
//	off merchants who want to use wiringPi on non-Raspberry Pi platforms - which
//	I do not support so don't email me your bleating whinges about anything
//	other than a genuine Raspberry Pi.

#ifdef	DONT_CARE_ANYMORE
  if (! (strstr (line, "BCM2708") || strstr (line, "BCM2709") || strstr (line, "BCM2835")))
  {
    fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
    fprintf (stderr, " - expecting BCM2708, BCM2709 or BCM2835.\n") ;
    fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
    fprintf (stderr, "at GitHub.com/wiringPi/wiringPi. If this is not a Raspberry Pi then you\n") ;
    fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
    fprintf (stderr, "Raspberry Pi ONLY.\n") ;
    exit (EXIT_FAILURE) ;
  }
#endif

// Actually... That has caused me more than 10,000 emails so-far. Mosty by
//	people who think they know better by creating a statically linked
//	version that will not run with a new 4.9 kernel. I utterly hate and
//	despise those people.
//
//	I also get bleats from people running other than Raspbian with another
//	distros compiled kernel rather than a foundation compiled kernel, so
//	this might actually help them. It might not - I only have the capacity
//	to support Raspbian.
//
//	However, I've decided to leave this check out and rely purely on the
//	Revision: line for now. It will not work on a non-pi hardware or weird
//	kernels that don't give you a suitable revision line.

// So - we're Probably on a Raspberry Pi. Check the revision field for the real
//	hardware type
//	In-future, I ought to use the device tree as there are now Pi entries in
//	/proc/device-tree/ ...
//	but I'll leave that for the next revision. Or the next.

// Isolate the Revision line

  rewind (cpuFd) ;
  while (fgets (line, 120, cpuFd) != NULL)
    if (strncmp (line, "Revision", 8) == 0)
      break ;

  fclose (cpuFd) ;

  if (strncmp (line, "Revision", 8) != 0)
    piGpioLayoutOops ("No \"Revision\" line") ;

// Chomp trailing CR/NL

  for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
    *c = 0 ;

  if (wiringPiDebug)
    printf ("piGpioLayout: Revision string: %s\n", line) ;

// Scan to the first character of the revision number

  for (c = line ; *c ; ++c)
    if (*c == ':')
      break ;

  if (*c != ':')
    piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;

// Chomp spaces

  ++c ;
  while (isspace (*c))
    ++c ;

  if (!isxdigit (*c))
    piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;

// Make sure its long enough

  if (strlen (c) < 4)
    piGpioLayoutOops ("Bogus revision line (too small)") ;

// Isolate  last 4 characters: (in-case of overvolting or new encoding scheme)

  c = c + strlen (c) - 4 ;

  if (wiringPiDebug)
    printf ("piGpioLayout: last4Chars are: \"%s\"\n", c) ;

  if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
    gpioLayout = 1 ;
  else
    gpioLayout = 2 ;	// Covers everything else from the B revision 2 to the B+, the Pi v2, v3, zero and CM's.

  if (wiringPiDebug)
    printf ("piGpioLayoutOops: Returning revision: %d\n", gpioLayout) ;

  return gpioLayout ;
}

// Assembly:
subq $0xa8, %rsp
cmpl $-0x1, 0x32782(%rip)    # 0x5ccd0
je 0x2a562
movl 0x3277a(%rip), %eax     # 0x5ccd0
movl %eax, 0xa4(%rsp)
jmp 0x2a8a0
leaq 0x20dc9(%rip), %rdi     # 0x4b332
leaq 0x20db1(%rip), %rsi     # 0x4b321
callq 0x22cb0
movq %rax, 0x98(%rsp)
cmpq $0x0, %rax
jne 0x2a58f
leaq 0x20d99(%rip), %rdi     # 0x4b323
callq 0x2a8b0
jmp 0x2a591
leaq 0x20(%rsp), %rdi
movq 0x98(%rsp), %rdx
movl $0x78, %esi
callq 0x23170
cmpq $0x0, %rax
je 0x2a5cd
leaq 0x20(%rsp), %rdi
leaq 0x20d86(%rip), %rsi     # 0x4b340
movl $0x8, %edx
callq 0x224a0
cmpl $0x0, %eax
jne 0x2a5cb
jmp 0x2a5cd
jmp 0x2a591
leaq 0x20(%rsp), %rdi
leaq 0x20d67(%rip), %rsi     # 0x4b340
movl $0x8, %edx
callq 0x224a0
cmpl $0x0, %eax
je 0x2a5f4
leaq 0x20d5a(%rip), %rdi     # 0x4b349
callq 0x2a8b0
movq 0x31945(%rip), %rax     # 0x5bf40
cmpl $0x0, (%rax)
je 0x2a613
leaq 0x20(%rsp), %rsi
leaq 0x20d50(%rip), %rdi     # 0x4b35c
movb $0x0, %al
callq 0x22060
movq 0x98(%rsp), %rdi
callq 0x220b0
leaq 0x20(%rsp), %rdi
movq 0x98(%rsp), %rdx
movl $0x78, %esi
callq 0x23170
cmpq $0x0, %rax
je 0x2a65c
leaq 0x20(%rsp), %rdi
leaq 0x20d2f(%rip), %rsi     # 0x4b378
movl $0x8, %edx
callq 0x224a0
cmpl $0x0, %eax
jne 0x2a65a
jmp 0x2a65c
jmp 0x2a620
movq 0x98(%rsp), %rdi
callq 0x22ab0
leaq 0x20(%rsp), %rdi
leaq 0x20d03(%rip), %rsi     # 0x4b378
movl $0x8, %edx
callq 0x224a0
cmpl $0x0, %eax
je 0x2a690
leaq 0x20cf6(%rip), %rdi     # 0x4b381
callq 0x2a8b0
leaq 0x20(%rsp), %rdi
callq 0x22420
movq %rax, %rcx
subq $0x1, %rcx
leaq 0x20(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0xa, %ecx
movb %al, 0x17(%rsp)
je 0x2a6d3
movq 0x18(%rsp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
sete %al
movb %al, 0x17(%rsp)
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x2a6dd
jmp 0x2a6f5
movq 0x18(%rsp), %rax
movb $0x0, (%rax)
movq 0x18(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x2a6ae
movq 0x31844(%rip), %rax     # 0x5bf40
cmpl $0x0, (%rax)
je 0x2a714
leaq 0x20(%rsp), %rsi
leaq 0x20c87(%rip), %rdi     # 0x4b394
movb $0x0, %al
callq 0x22060
leaq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax)
je 0x2a749
movq 0x18(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
jne 0x2a737
jmp 0x2a749
jmp 0x2a739
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x2a71e
movq 0x18(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
je 0x2a762
leaq 0x20c5a(%rip), %rdi     # 0x4b3b7
callq 0x2a8b0
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
callq 0x22bc0
movq (%rax), %rax
movq 0x18(%rsp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x2a7a1
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x2a770
callq 0x22bc0
movq (%rax), %rax
movq 0x18(%rsp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x1000, %eax           # imm = 0x1000
cmpl $0x0, %eax
jne 0x2a7ce
leaq 0x20c0f(%rip), %rdi     # 0x4b3d8
callq 0x2a8b0
movq 0x18(%rsp), %rdi
callq 0x22420
cmpq $0x4, %rax
jae 0x2a7ea
leaq 0x20c2d(%rip), %rdi     # 0x4b412
callq 0x2a8b0
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rdi
callq 0x22420
movq %rax, %rcx
movq 0x8(%rsp), %rax
addq %rcx, %rax
addq $-0x4, %rax
movq %rax, 0x18(%rsp)
movq 0x31727(%rip), %rax     # 0x5bf40
cmpl $0x0, (%rax)
je 0x2a831
movq 0x18(%rsp), %rsi
leaq 0x20c08(%rip), %rdi     # 0x4b432
movb $0x0, %al
callq 0x22060
movq 0x18(%rsp), %rdi
leaq 0x20c19(%rip), %rsi     # 0x4b456
callq 0x22f40
cmpl $0x0, %eax
je 0x2a85d
movq 0x18(%rsp), %rdi
leaq 0x20c08(%rip), %rsi     # 0x4b45b
callq 0x22f40
cmpl $0x0, %eax
jne 0x2a869
movl $0x1, 0x32469(%rip)     # 0x5ccd0
jmp 0x2a873
movl $0x2, 0x3245d(%rip)     # 0x5ccd0
movq 0x316c6(%rip), %rax     # 0x5bf40
cmpl $0x0, (%rax)
je 0x2a893
movl 0x3244b(%rip), %esi     # 0x5ccd0
leaq 0x20bd4(%rip), %rdi     # 0x4b460
movb $0x0, %al
callq 0x22060
movl 0x32437(%rip), %eax     # 0x5ccd0
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nop