#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 */
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, unsigned int *, int);
int /*<<< orphan*/ write(int, unsigned int *, int);
int gpio_output(unsigned char io, unsigned char val) {
  int fd;
  unsigned int reg_val[2];
  if ((io > 31) || (val > 1)) {
    printf("param is wrong!\n");
    return -1;
  }
  fd = open("/dev/regopt", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd < 0) {
    printf("can't open!\n");
    return -1;
  }
  if (val == 0) {
    reg_val[0] = 0X10000620;
    read(fd, reg_val, 8);
    reg_val[1] &= ~(1 << io);
    write(fd, reg_val, 8);
  } else if (val == 1) {
    reg_val[0] = 0X10000620;
    read(fd, reg_val, 8);
    reg_val[1] |= 1 << io;
    write(fd, reg_val, 8);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
movzbl -0x5(%rbp),%eax
cmp    $0x1f,%eax
jg     117e <gpio_output+0x2e>
movzbl -0x6(%rbp),%eax
cmp    $0x1,%eax
jle    1196 <gpio_output+0x46>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <gpio_output+0x13c>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe59(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e2 <gpio_output+0x92>
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <gpio_output+0x13c>
movzbl -0x6(%rbp),%eax
cmp    $0x0,%eax
jne    1231 <gpio_output+0xe1>
movl   $0x10000620,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
movzbl -0x5(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
jmp    127d <gpio_output+0x12d>
movzbl -0x6(%rbp),%eax
cmp    $0x1,%eax
jne    1278 <gpio_output+0x128>
movl   $0x10000620,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
movzbl -0x5(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
jmp    127d <gpio_output+0x12d>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_output>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
cmp    $0x1f,%bpl
ja     11c3 <gpio_output+0x73>
mov    %esi,%r14d
cmp    $0x2,%r14b
jae    11c3 <gpio_output+0x73>
mov    0x2e78(%rip),%rax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11cc <gpio_output+0x7c>
mov    %eax,%ebx
movl   $0x10000620,0x8(%rsp)
10
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <read@plt>
mov    $0x1,%eax
mov    %ebp,%ecx
shl    %cl,%eax
test   %r14b,%r14b
je     11df <gpio_output+0x8f>
or     %eax,0xc(%rsp)
jmp    11e5 <gpio_output+0x95>
lea    0xe4e(%rip),%rdi
jmp    11d3 <gpio_output+0x83>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11ff <gpio_output+0xaf>
not    %eax
and    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"param is wrong!\0A\00"
@.str.1 = [12 x i8] c"/dev/regopt\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [13 x i8] c"can't open!\0A\00"
define i32 @gpio_output(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca i32
%7 = alloca [2 x i32]
store i8 %0, i8* %4
store i8 %1, i8* %5
%8 = load i8, i8* %4
%9 = zext i8 %8 to i32
%10 = icmp sgt i32 %9, 31
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = load i8, i8* %5
%13 = zext i8 %12 to i32
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %11, %2
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %69
17: ; preds = %11
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_NOCTTY
%20 = or i32 %18, %19
%21 = load i32, i32* @O_NDELAY
%22 = or i32 %20, %21
%23 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %69
28: ; preds = %17
%29 = load i8, i8* %5
%30 = zext i8 %29 to i32
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %47
32: ; preds = %28
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
store i32 268437024, i32* %33
%34 = load i32, i32* %6
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%36 = call i32 @read(i32 %34, i32* %35, i32 8)
%37 = load i8, i8* %4
%38 = zext i8 %37 to i32
%39 = shl i32 1, %38
%40 = xor i32 %39, -1
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%42 = load i32, i32* %41
%43 = and i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* %6
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%46 = call i32 @write(i32 %44, i32* %45, i32 8)
br label %66
47: ; preds = %28
%48 = load i8, i8* %5
%49 = zext i8 %48 to i32
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %65
51: ; preds = %47
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
store i32 268437024, i32* %52
%53 = load i32, i32* %6
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%55 = call i32 @read(i32 %53, i32* %54, i32 8)
%56 = load i8, i8* %4
%57 = zext i8 %56 to i32
%58 = shl i32 1, %57
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%60 = load i32, i32* %59
%61 = or i32 %60, %58
store i32 %61, i32* %59
%62 = load i32, i32* %6
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%64 = call i32 @write(i32 %62, i32* %63, i32 8)
br label %65
65: ; preds = %51, %47
br label %66
66: ; preds = %65, %32
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
store i32 0, i32* %3
br label %69
69: ; preds = %66, %26, %15
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"/dev/regopt\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@str = [12 x i8] c"can't open!\00"
@str.3 = [16 x i8] c"param is wrong!\00"
define i32 @gpio_output(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = zext i8 %0 to i32
%6 = icmp ugt i8 %0, 31
%7 = icmp ugt i8 %1, 1
%8 = or i1 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.3, i64 0, i64 0))
br label %38
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_NOCTTY
%14 = or i32 %13, %12
%15 = load i32, i32* @O_NDELAY
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %38
21: ; preds = %11
%22 = icmp eq i8 %1, 0
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
store i32 268437024, i32* %23
%24 = call i32 @read(i32 %17, i32* %23, i32 8)
%25 = shl nuw i32 1, %5
br i1 %22, label %26, label %31
26: ; preds = %21
%27 = xor i32 %25, -1
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
br label %35
31: ; preds = %21
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = or i32 %33, %25
store i32 %34, i32* %32
br label %35
35: ; preds = %31, %26
%36 = call i32 @write(i32 %17, i32* %23, i32 8)
%37 = tail call i32 @close(i32 %17)
br label %38
38: ; preds = %35, %19, %9
%39 = phi i32 [ -1, %9 ], [ -1, %19 ], [ 0, %35 ]
ret i32 %39
}