#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, x;
  sscanf(getenv("QUERY_STRING"), "%d", &x);
  printf("Content-type: text/plain\n\n");
  printf("[");
  for (i = x; i < x + 100; i++)
    printf("%d, ", i2c_smbus_read_word_data(fd, i));
  printf("%d", i2c_smbus_read_word_data(fd, x + 100));
  printf("]");
  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    -0x20(%rbp),%rdx
call   1070 <sscanf@plt>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
add    $0x64,%ecx
cmp    %ecx,%eax
jge    1287 <main+0x107>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1080 <i2c_smbus_read_word_data@plt>
mov    %eax,%esi
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    124d <main+0xcd>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%esi
add    $0x64,%esi
call   1080 <i2c_smbus_read_word_data@plt>
mov    %eax,%esi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdaf(%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   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   10b0 <open@plt>
cmp    $0x1,%eax
jle    1289 <main+0xe9>
mov    %eax,%r14d
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    $0x9,%edx
call   1070 <ioctl@plt>
test   %rax,%rax
js     1292 <main+0xf2>
lea    0xe1c(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe1d(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %eax,%edi
call   1090 <sscanf@plt>
lea    0xe12(%rip),%rdi
call   1050 <puts@plt>
mov    $0x5b,%edi
call   1040 <putchar@plt>
mov    0xc(%rsp),%ebp
lea    0xdf3(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %ebp,%esi
call   10a0 <i2c_smbus_read_word_data@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%ebp
mov    0xc(%rsp),%esi
add    $0x64,%esi
cmp    %esi,%ebp
jl     1230 <main+0x90>
mov    %r14d,%edi
call   10a0 <i2c_smbus_read_word_data@plt>
lea    0xdb5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x5d,%edi
call   1040 <putchar@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb6(%rip),%rdi
jmp    1299 <main+0xf9>
lea    0xda1(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10c0 <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"
@.str.6 = [2 x i8] c"[\00"
@.str.7 = [5 x i8] c"%d, \00"
@.str.8 = [2 x i8] c"]\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
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp sle i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
store i32 9, i32* %7
%18 = load i32, i32* %6
%19 = load i32, i32* @I2C_SLAVE
%20 = load i32, i32* %7
%21 = call i64 @ioctl(i32 %18, i32 %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = call i32 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @sscanf(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %9)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%31 = load i32, i32* %9
store i32 %31, i32* %8
br label %32
32: ; preds = %42, %26
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 100
%36 = icmp slt i32 %33, %35
br i1 %36, label %37, label %45
37: ; preds = %32
%38 = load i32, i32* %6
%39 = load i32, i32* %8
%40 = call i32 @i2c_smbus_read_word_data(i32 %38, i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %37
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %32
45: ; preds = %32
%46 = load i32, i32* %6
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 100
%49 = call i32 @i2c_smbus_read_word_data(i32 %46, i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
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.7 = [5 x i8] c"%d, \00"
@str = [26 x i8] c"Content-type: text/plain\0A\00"
@str.9 = [12 x i8] c"ioctl error\00"
@str.10 = [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.10, 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.9, 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 = call i32 @putchar(i32 91)
%23 = load i32, i32* %3
br label %24
24: ; preds = %17, %24
%25 = phi i32 [ %28, %24 ], [ %23, %17 ]
%26 = call i32 @i2c_smbus_read_word_data(i32 %5, i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %26)
%28 = add nsw i32 %25, 1
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 100
%31 = icmp slt i32 %28, %30
br i1 %31, label %24, label %32
32: ; preds = %24
%33 = call i32 @i2c_smbus_read_word_data(i32 %5, i32 %30)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = call i32 @putchar(i32 93)
%36 = call i32 @close(i32 %5)
ret i32 0
}