#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 uint8_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char read_byte_data(int);
int /*<<< orphan*/ stderr;
char updatebyte(char, int, int);
int /*<<< orphan*/ write_byte_data(int, char);
char set_channel_state(uint8_t address, uint8_t channel, uint8_t state) {
  if (address < 0x70 || address > 0x77) {
    fprintf(stderr, "I2C address out of range. 0x70 - 0x77\n");
    return (-1);
  }
  if (channel < 1 || channel > 4) {
    fprintf(stderr, "channel out of range. 1 - 4\n");
    return (-1);
  }
  if (state > 1) {
    fprintf(stderr, "state out of range. 0 or 1\n");
    return (-1);
  }
  char cval = read_byte_data(address);
  cval = updatebyte(cval, channel - 1, state);
  write_byte_data(address, cval);
  return (0);
}


x86 assembly (clang, -O0):
<set_channel_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x70,-0x8(%rbp)
jl     1165 <set_channel_state+0x25>
cmpl   $0x77,-0x8(%rbp)
jle    1183 <set_channel_state+0x43>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
call   1040 <fprintf@plt>
movb   $0xff,-0x1(%rbp)
jmp    120f <set_channel_state+0xcf>
cmpl   $0x1,-0xc(%rbp)
jl     1197 <set_channel_state+0x57>
cmpl   $0x4,-0xc(%rbp)
jle    11b5 <set_channel_state+0x75>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1040 <fprintf@plt>
movb   $0xff,-0x1(%rbp)
jmp    120f <set_channel_state+0xcf>
cmpl   $0x1,-0x10(%rbp)
jle    11dd <set_channel_state+0x9d>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1040 <fprintf@plt>
movb   $0xff,-0x1(%rbp)
jmp    120f <set_channel_state+0xcf>
mov    -0x8(%rbp),%edi
call   1030 <read_byte_data@plt>
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%edx
movsbl %al,%edi
call   1060 <updatebyte@plt>
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%edi
movsbl -0x11(%rbp),%esi
call   1050 <write_byte_data@plt>
movb   $0x0,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_channel_state>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
lea    -0x78(%rbx),%eax
cmp    $0xfffffff7,%eax
ja     1160 <set_channel_state+0x20>
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
lea    0xea2(%rip),%rsi
jmp    1194 <set_channel_state+0x54>
mov    %esi,%ebp
lea    -0x5(%rbp),%eax
cmp    $0xfffffffb,%eax
ja     117c <set_channel_state+0x3c>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xead(%rip),%rsi
jmp    1194 <set_channel_state+0x54>
mov    %edx,%r14d
cmp    $0x2,%edx
jl     11a0 <set_channel_state+0x60>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xeb0(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xff,%al
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebx,%edi
call   1030 <read_byte_data@plt>
add    $0xffffffff,%ebp
movsbl %al,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1060 <updatebyte@plt>
movsbl %al,%esi
mov    %ebx,%edi
call   1050 <write_byte_data@plt>
xor    %eax,%eax
jmp    119b <set_channel_state+0x5b>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"I2C address out of range. 0x70 - 0x77\0A\00"
@.str.1 = [29 x i8] c"channel out of range. 1 - 4\0A\00"
@.str.2 = [28 x i8] c"state out of range. 0 or 1\0A\00"
define signext i8 @set_channel_state(i32 %0, i32 %1, i32 %2) {
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 112
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 119
br i1 %13, label %14, label %17
14: ; preds = %11, %3
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i8 -1, i8* %4
br label %43
17: ; preds = %11
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 1
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 4
br i1 %22, label %23, label %26
23: ; preds = %20, %17
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i8 -1, i8* %4
br label %43
26: ; preds = %20
%27 = load i32, i32* %7
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i8 -1, i8* %4
br label %43
32: ; preds = %26
%33 = load i32, i32* %5
%34 = call signext i8 @read_byte_data(i32 %33)
store i8 %34, i8* %8
%35 = load i8, i8* %8
%36 = load i32, i32* %6
%37 = sub nsw i32 %36, 1
%38 = load i32, i32* %7
%39 = call signext i8 @updatebyte(i8 signext %35, i32 %37, i32 %38)
store i8 %39, i8* %8
%40 = load i32, i32* %5
%41 = load i8, i8* %8
%42 = call i32 @write_byte_data(i32 %40, i8 signext %41)
store i8 0, i8* %4
br label %43
43: ; preds = %32, %29, %23, %14
%44 = load i8, i8* %4
ret i8 %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"I2C address out of range. 0x70 - 0x77\0A\00"
@.str.1 = [29 x i8] c"channel out of range. 1 - 4\0A\00"
@.str.2 = [28 x i8] c"state out of range. 0 or 1\0A\00"
define signext i8 @set_channel_state(i32 %0, i32 %1, i32 %2) {
%4 = add i32 %0, -120
%5 = icmp ult i32 %4, -8
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %25
9: ; preds = %3
%10 = add i32 %1, -5
%11 = icmp ult i32 %10, -4
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %25
15: ; preds = %9
%16 = icmp sgt i32 %2, 1
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %25
20: ; preds = %15
%21 = tail call signext i8 @read_byte_data(i32 %0)
%22 = add nsw i32 %1, -1
%23 = tail call signext i8 @updatebyte(i8 signext %21, i32 %22, i32 %2)
%24 = tail call i32 @write_byte_data(i32 %0, i8 signext %23)
br label %25
25: ; preds = %20, %17, %12, %6
%26 = phi i8 [ -1, %6 ], [ -1, %12 ], [ -1, %17 ], [ 0, %20 ]
ret i8 %26
}