#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 i2c_smbus_read_word_data(int, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(int /*<<< orphan*/, char *, int *);
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 = 0x09;
  if (ioctl(fd, I2C_SLAVE, addr) < 0) {
    printf("ioctl error\n");
    exit(1);
  }
  int i;
  sscanf(getenv("QUERY_STRING"), "%d", &i);
  printf("Content-type: text/plain\n\n");
  printf("%d", i2c_smbus_read_word_data(fd, i));
  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     11d0 <main+0x50>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x9,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    120d <main+0x8d>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe15(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
lea    0xe14(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <sscanf@plt>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1080 <i2c_smbus_read_word_data@plt>
mov    %eax,%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
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   %r14
push   %rbx
push   %rax
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   10a0 <open@plt>
cmp    $0x1,%eax
jle    1220 <main+0x90>
mov    %eax,%ebx
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x9,%edx
call   1060 <ioctl@plt>
test   %rax,%rax
js     1229 <main+0x99>
lea    0xe3a(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe3b(%rip),%r14
lea    0x4(%rsp),%rdx
mov    %eax,%edi
mov    %r14,%rsi
call   1080 <sscanf@plt>
lea    0xe28(%rip),%rdi
call   1040 <puts@plt>
mov    0x4(%rsp),%esi
mov    %ebx,%edi
call   1090 <i2c_smbus_read_word_data@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1a(%rip),%rdi
jmp    1230 <main+0xa0>
lea    0xe05(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <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 i32
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 (i8*, ...) @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 9, 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 (i8*, ...) @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 = call i32 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @sscanf(i32 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %8)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%29 = load i32, i32* %6
%30 = load i32, i32* %8
%31 = call i32 @i2c_smbus_read_word_data(i32 %29, i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
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 i32
%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 9)
%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 = bitcast i32* %3 to i8*
%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), i32* %3)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%22 = load i32, i32* %3
%23 = call i32 @i2c_smbus_read_word_data(i32 %5, i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = call i32 @close(i32 %5)
ret i32 0
}