#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 /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getenv(char *);
int /*<<< orphan*/ i2c_smbus_write_byte_data(int, int, unsigned char);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sscanf(int /*<<< orphan*/, char *, unsigned char *);
int main(int argc, char *argv[]) {
  int fd;
  fd = open("/dev/i2c-1", O_RDWR);
  if (fd <= 1) {
    printf("error on device\n");
    exit(1);
  }
  int addr = 0x18;
  if (ioctl(fd, I2C_SLAVE, addr) < 0) {
    printf("ioctl error\n");
    exit(1);
  }
  i2c_smbus_write_byte_data(fd, 0x03, 0x3F);
  unsigned char cmd;
  sscanf(getenv("QUERY_STRING"), "%d", &cmd);
  i2c_smbus_write_byte_data(fd, 0x01, cmd);
  printf("Content-type: text/plain\n\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jg     11ce <main+0x4e>
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x18,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1209 <main+0x89>
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
mov    $0x3f,%edx
call   1070 <i2c_smbus_write_byte_data@plt>
lea    0xe07(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
lea    0xe06(%rip),%rsi
lea    -0x19(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
movzbl -0x19(%rbp),%edx
call   1070 <i2c_smbus_write_byte_data@plt>
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1090 <open@plt>
cmp    $0x1,%eax
jle    1217 <main+0x97>
mov    %eax,%ebx
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x18,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1220 <main+0xa0>
mov    %ebx,%edi
mov    $0x3,%esi
mov    $0x3f,%edx
call   1070 <i2c_smbus_write_byte_data@plt>
lea    0xe38(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe39(%rip),%rsi
lea    0xf(%rsp),%rdx
mov    %eax,%edi
call   1080 <sscanf@plt>
movzbl 0xf(%rsp),%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <i2c_smbus_write_byte_data@plt>
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe23(%rip),%rdi
jmp    1227 <main+0xa7>
lea    0xe0e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"error on device\0A\00"
@I2C_SLAVE = global i32 0
@.str.2 = [13 x i8] c"ioctl error\0A\00"
@.str.3 = [13 x i8] c"QUERY_STRING\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [27 x i8] c"Content-type: text/plain\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp sle i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
store i32 24, i32* %7
%17 = load i32, i32* %6
%18 = load i32, i32* @I2C_SLAVE
%19 = load i32, i32* %7
%20 = call i64 @ioctl(i32 %17, i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* %6
%27 = call i32 @i2c_smbus_write_byte_data(i32 %26, i32 3, i8 zeroext 63)
%28 = call i32 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @sscanf(i32 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%30 = load i32, i32* %6
%31 = load i8, i8* %8
%32 = call i32 @i2c_smbus_write_byte_data(i32 %30, i32 1, i8 zeroext %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@.str.3 = [13 x i8] c"QUERY_STRING\00"
@.str.4 = [3 x i8] c"%d\00"
@str = [26 x i8] c"Content-type: text/plain\0A\00"
@str.6 = [12 x i8] c"ioctl error\00"
@str.7 = [16 x i8] c"error on device\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 2
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @I2C_SLAVE
%12 = tail call i64 @ioctl(i32 %5, i32 %11, i32 24)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = tail call i32 @i2c_smbus_write_byte_data(i32 %5, i32 3, i8 zeroext 63)
%19 = tail call i32 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @sscanf(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%21 = load i8, i8* %3
%22 = call i32 @i2c_smbus_write_byte_data(i32 %5, i32 1, i8 zeroext %21)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%24 = call i32 @close(i32 %5)
ret i32 0
}