#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 */

/* Variables and functions */
scalar_t__ _rpi_nano_exit_AT(int);
int _rpi_nano_get_reg(int, char *, int *);
scalar_t__ _rpi_nano_switch_to_AT(int);
int rpi_nano_get_channel(int fd) {
  if (_rpi_nano_switch_to_AT(fd))
    return -1;
  int S002Val = 0;
  int S003Val = 0;
  if (_rpi_nano_get_reg(fd, "S002", &S002Val) == -1) {
    if (_rpi_nano_exit_AT(fd))
      return -1;
    return -1;
  }
  if (_rpi_nano_get_reg(fd, "S003", &S003Val) == -1) {
    if (_rpi_nano_exit_AT(fd))
      return -1;
    return -1;
  }
  if (_rpi_nano_exit_AT(fd))
    return -1;
  return ((S003Val << 8) & 0xff00) | (S002Val & 0xff);
}


x86 assembly (clang, -O0):
<rpi_nano_get_channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <_rpi_nano_switch_to_AT@plt>
cmp    $0x0,%rax
je     1159 <rpi_nano_get_channel+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <rpi_nano_get_channel+0xfa>
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe8f(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1040 <_rpi_nano_get_reg@plt>
cmp    $0xffffffff,%eax
jne    11ad <rpi_nano_get_channel+0x7d>
mov    -0x8(%rbp),%edi
call   1050 <_rpi_nano_exit_AT@plt>
cmp    $0x0,%rax
je     11a1 <rpi_nano_get_channel+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <rpi_nano_get_channel+0xfa>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <rpi_nano_get_channel+0xfa>
mov    -0x8(%rbp),%edi
lea    0xe4e(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1040 <_rpi_nano_get_reg@plt>
cmp    $0xffffffff,%eax
jne    11f3 <rpi_nano_get_channel+0xc3>
mov    -0x8(%rbp),%edi
call   1050 <_rpi_nano_exit_AT@plt>
cmp    $0x0,%rax
je     11e7 <rpi_nano_get_channel+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <rpi_nano_get_channel+0xfa>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <rpi_nano_get_channel+0xfa>
mov    -0x8(%rbp),%edi
call   1050 <_rpi_nano_exit_AT@plt>
cmp    $0x0,%rax
je     1211 <rpi_nano_get_channel+0xe1>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <rpi_nano_get_channel+0xfa>
mov    -0x10(%rbp),%eax
shl    $0x8,%eax
and    $0xff00,%eax
mov    -0xc(%rbp),%ecx
and    $0xff,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpi_nano_get_channel>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
call   1030 <_rpi_nano_switch_to_AT@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     114e <rpi_nano_get_channel+0x1e>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
lea    0xe9c(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1040 <_rpi_nano_get_reg@plt>
cmp    $0xffffffff,%eax
je     11b3 <rpi_nano_get_channel+0x83>
lea    0xe89(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <_rpi_nano_get_reg@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <_rpi_nano_exit_AT@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ebp
je     1147 <rpi_nano_get_channel+0x17>
test   %rcx,%rcx
jne    1147 <rpi_nano_get_channel+0x17>
mov    (%rsp),%eax
shl    $0x8,%eax
movzwl %ax,%ecx
movzbl 0x4(%rsp),%eax
or     %ecx,%eax
jmp    1147 <rpi_nano_get_channel+0x17>
mov    %ebx,%edi
call   1050 <_rpi_nano_exit_AT@plt>
mov    $0xffffffff,%eax
jmp    1147 <rpi_nano_get_channel+0x17>

LLVM IR (-O0):
@.str = [5 x i8] c"S002\00"
@.str.1 = [5 x i8] c"S003\00"
define i32 @rpi_nano_get_channel(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i64 @_rpi_nano_switch_to_AT(i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %42
10: ; preds = %1
store i32 0, i32* %4
store i32 0, i32* %5
%11 = load i32, i32* %3
%12 = call i32 @_rpi_nano_get_reg(i32 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = load i32, i32* %3
%16 = call i64 @_rpi_nano_exit_AT(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 -1, i32* %2
br label %42
19: ; preds = %14
store i32 -1, i32* %2
br label %42
20: ; preds = %10
%21 = load i32, i32* %3
%22 = call i32 @_rpi_nano_get_reg(i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i32, i32* %3
%26 = call i64 @_rpi_nano_exit_AT(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %2
br label %42
29: ; preds = %24
store i32 -1, i32* %2
br label %42
30: ; preds = %20
%31 = load i32, i32* %3
%32 = call i64 @_rpi_nano_exit_AT(i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %2
br label %42
35: ; preds = %30
%36 = load i32, i32* %5
%37 = shl i32 %36, 8
%38 = and i32 %37, 65280
%39 = load i32, i32* %4
%40 = and i32 %39, 255
%41 = or i32 %38, %40
store i32 %41, i32* %2
br label %42
42: ; preds = %35, %34, %29, %28, %19, %18, %9
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [5 x i8] c"S002\00"
@.str.1 = [5 x i8] c"S003\00"
define i32 @rpi_nano_get_channel(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = tail call i64 @_rpi_nano_switch_to_AT(i32 %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %28
6: ; preds = %1
%7 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%8 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%9 = call i32 @_rpi_nano_get_reg(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %2)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = call i64 @_rpi_nano_exit_AT(i32 %0)
br label %26
13: ; preds = %6
%14 = call i32 @_rpi_nano_get_reg(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%15 = icmp ne i32 %14, -1
%16 = call i64 @_rpi_nano_exit_AT(i32 %0)
%17 = icmp eq i64 %16, 0
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %26
19: ; preds = %13
%20 = load i32, i32* %3
%21 = shl i32 %20, 8
%22 = and i32 %21, 65280
%23 = load i32, i32* %2
%24 = and i32 %23, 255
%25 = or i32 %22, %24
br label %26
26: ; preds = %13, %11, %19
%27 = phi i32 [ %25, %19 ], [ -1, %11 ], [ -1, %13 ]
br label %28
28: ; preds = %1, %26
%29 = phi i32 [ %27, %26 ], [ -1, %1 ]
ret i32 %29
}