#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 */
typedef int uint32_t;
typedef int int32_t;

/* Variables and functions */
int bcm2835_read(int);
int /*<<< orphan*/ bcm2835_write(int, int);
int /*<<< orphan*/ printk(char *, int);
int32_t gpio_function_select(int which_one, int function) {
  uint32_t old;
  uint32_t addr_offset;
  uint32_t bit;
  if (which_one > 54) {
    printk("Invalid GPIO%d\n", which_one);
    return -22;
  }
  addr_offset = (which_one / 10) * 4;
  bit = (which_one % 10) * 3;
  old = bcm2835_read(((0 + 0x200000) + 0x00) + addr_offset);
  old &= ~(7 << bit);
  old |= (function << bit);
  bcm2835_write(((0 + 0x200000) + 0x00) + addr_offset, old);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_function_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x36,-0x8(%rbp)
jle    1163 <gpio_function_select+0x33>
mov    -0x8(%rbp),%esi
lea    0xeae(%rip),%rdi
call   1030 <printk@plt>
movl   $0xffffffea,-0x4(%rbp)
jmp    11cf <gpio_function_select+0x9f>
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
shl    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
imul   $0x3,%edx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
add    $0x200000,%edi
call   1040 <bcm2835_read@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    $0x7,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%ecx
shl    %cl,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
add    $0x200000,%edi
mov    -0x10(%rbp),%esi
call   1050 <bcm2835_write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_function_select>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
cmp    $0x37,%edi
jl     1150 <gpio_function_select+0x20>
lea    0xebe(%rip),%rdi
mov    %ebx,%esi
call   1030 <printk@plt>
mov    $0xffffffea,%eax
jmp    119f <gpio_function_select+0x6f>
mov    %esi,%ebp
movslq %ebx,%rax
imul   $0x66666667,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
lea    (%rax,%rax,1),%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%ebx
lea    0x200000(,%rax,4),%r14d
mov    %r14d,%edi
call   1040 <bcm2835_read@plt>
lea    (%rbx,%rbx,2),%ecx
mov    $0x7,%edx
shl    %cl,%edx
not    %edx
and    %eax,%edx
shl    %cl,%ebp
or     %edx,%ebp
mov    %r14d,%edi
mov    %ebp,%esi
call   1050 <bcm2835_write@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Invalid GPIO%d\0A\00"
define i32 @gpio_function_select(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 54
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* %4
%13 = call i32 @printk(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 -22, i32* %3
br label %38
14: ; preds = %2
%15 = load i32, i32* %4
%16 = sdiv i32 %15, 10
%17 = mul nsw i32 %16, 4
store i32 %17, i32* %7
%18 = load i32, i32* %4
%19 = srem i32 %18, 10
%20 = mul nsw i32 %19, 3
store i32 %20, i32* %8
%21 = load i32, i32* %7
%22 = add nsw i32 2097152, %21
%23 = call i32 @bcm2835_read(i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %8
%25 = shl i32 7, %24
%26 = xor i32 %25, -1
%27 = load i32, i32* %6
%28 = and i32 %27, %26
store i32 %28, i32* %6
%29 = load i32, i32* %5
%30 = load i32, i32* %8
%31 = shl i32 %29, %30
%32 = load i32, i32* %6
%33 = or i32 %32, %31
store i32 %33, i32* %6
%34 = load i32, i32* %7
%35 = add nsw i32 2097152, %34
%36 = load i32, i32* %6
%37 = call i32 @bcm2835_write(i32 %35, i32 %36)
store i32 0, i32* %3
br label %38
38: ; preds = %14, %11
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [16 x i8] c"Invalid GPIO%d\0A\00"
define i32 @gpio_function_select(i32 %0, i32 %1) {
%3 = icmp sgt i32 %0, 54
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @printk(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %19
6: ; preds = %2
%7 = sdiv i32 %0, 10
%8 = shl nsw i32 %7, 2
%9 = srem i32 %0, 10
%10 = mul nsw i32 %9, 3
%11 = add nsw i32 %8, 2097152
%12 = tail call i32 @bcm2835_read(i32 %11)
%13 = shl nuw nsw i32 7, %10
%14 = xor i32 %13, -1
%15 = and i32 %12, %14
%16 = shl i32 %1, %10
%17 = or i32 %15, %16
%18 = tail call i32 @bcm2835_write(i32 %11, i32 %17)
br label %19
19: ; preds = %6, %4
%20 = phi i32 [ -22, %4 ], [ 0, %6 ]
ret i32 %20
}