#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*/ ioctl(int, int /*<<< orphan*/, char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ write(int, char *, int);
int ADS1015_open(int channel, float gain) {
  char *bus = "/dev/i2c-1";
  int fd = open(bus, O_RDWR);
  if (fd < 0) {
    printf("Adafruit_ADS1015: Failed to open bus");
    return -1;
  }
  ioctl(fd, I2C_SLAVE, (char)0x49);
  char config[3];
  config[0] = (char)0x01;
  config[1] = (char)0x00;
  switch (channel) {
  case 0:
    config[1] = config[1] | (char)0x40;
    break;
  case 1:
    config[1] = config[1] | (char)0x50;
    break;
  case 2:
    config[1] = config[1] | (char)0x60;
    break;
  case 3:
    config[1] = config[1] | (char)0x70;
    break;
  default:
    printf("Adafruit_ADS1015: Invalid channel number");
    return -1;
  }
  if (gain == 2.0f / 3.0f)
    config[1] = config[1] | (char)0x00;
  else if (gain == 1.0f)
    config[1] = config[1] | (char)0x02;
  else if (gain == 2.0f)
    config[1] = config[1] | (char)0x04;
  else if (gain == 4.0f)
    config[1] = config[1] | (char)0x06;
  else if (gain == 8.0f)
    config[1] = config[1] | (char)0x08;
  else if (gain == 16.0f)
    config[1] = config[1] | (char)0x0A;
  else {
    printf("Adafruit_ADS1015: Invalid gain");
    return -1;
  }
  config[2] = (char)0x00 | (char)0x03 | (char)0x80;
  write(fd, config, 3);
  return fd;
}


x86 assembly (clang, -O0):
<ADS1015_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movss  %xmm0,-0xc(%rbp)
lea    0xed1(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1192 <ADS1015_open+0x52>
lea    0xeb2(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <ADS1015_open+0x237>
mov    -0x1c(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    $0x49,%edx
call   1050 <ioctl@plt>
movb   $0x1,-0x1f(%rbp)
movb   $0x0,-0x1e(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x28(%rbp)
sub    $0x3,%rax
ja     1211 <ADS1015_open+0xd1>
mov    -0x28(%rbp),%rax
lea    0xe4c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsbl -0x1e(%rbp),%eax
or     $0x40,%eax
mov    %al,-0x1e(%rbp)
jmp    1229 <ADS1015_open+0xe9>
movsbl -0x1e(%rbp),%eax
or     $0x50,%eax
mov    %al,-0x1e(%rbp)
jmp    1229 <ADS1015_open+0xe9>
movsbl -0x1e(%rbp),%eax
or     $0x60,%eax
mov    %al,-0x1e(%rbp)
jmp    1229 <ADS1015_open+0xe9>
movsbl -0x1e(%rbp),%eax
or     $0x70,%eax
mov    %al,-0x1e(%rbp)
jmp    1229 <ADS1015_open+0xe9>
lea    0xe40(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <ADS1015_open+0x237>
movss  -0xc(%rbp),%xmm0
movss  0xdca(%rip),%xmm1
ucomiss %xmm1,%xmm0
jne    1254 <ADS1015_open+0x114>
jp     1254 <ADS1015_open+0x114>
movsbl -0x1e(%rbp),%eax
or     $0x0,%eax
mov    %al,-0x1e(%rbp)
jmp    135c <ADS1015_open+0x21c>
movss  -0xc(%rbp),%xmm0
movss  0xda3(%rip),%xmm1
ucomiss %xmm1,%xmm0
jne    127f <ADS1015_open+0x13f>
jp     127f <ADS1015_open+0x13f>
movsbl -0x1e(%rbp),%eax
or     $0x2,%eax
mov    %al,-0x1e(%rbp)
jmp    1357 <ADS1015_open+0x217>
movss  -0xc(%rbp),%xmm0
movss  0xd7c(%rip),%xmm1
ucomiss %xmm1,%xmm0
jne    12aa <ADS1015_open+0x16a>
jp     12aa <ADS1015_open+0x16a>
movsbl -0x1e(%rbp),%eax
or     $0x4,%eax
mov    %al,-0x1e(%rbp)
jmp    1352 <ADS1015_open+0x212>
movss  -0xc(%rbp),%xmm0
movss  0xd55(%rip),%xmm1
ucomiss %xmm1,%xmm0
jne    12d5 <ADS1015_open+0x195>
jp     12d5 <ADS1015_open+0x195>
movsbl -0x1e(%rbp),%eax
or     $0x6,%eax
mov    %al,-0x1e(%rbp)
jmp    134d <ADS1015_open+0x20d>
movss  -0xc(%rbp),%xmm0
movss  0xd2e(%rip),%xmm1
ucomiss %xmm1,%xmm0
jne    1300 <ADS1015_open+0x1c0>
jp     1300 <ADS1015_open+0x1c0>
movsbl -0x1e(%rbp),%eax
or     $0x8,%eax
mov    %al,-0x1e(%rbp)
jmp    1348 <ADS1015_open+0x208>
movss  -0xc(%rbp),%xmm0
movss  0xd07(%rip),%xmm1
ucomiss %xmm1,%xmm0
jne    132b <ADS1015_open+0x1eb>
jp     132b <ADS1015_open+0x1eb>
movsbl -0x1e(%rbp),%eax
or     $0xa,%eax
mov    %al,-0x1e(%rbp)
jmp    1343 <ADS1015_open+0x203>
lea    0xd4f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <ADS1015_open+0x237>
jmp    1348 <ADS1015_open+0x208>
jmp    134d <ADS1015_open+0x20d>
jmp    1352 <ADS1015_open+0x212>
jmp    1357 <ADS1015_open+0x217>
jmp    135c <ADS1015_open+0x21c>
movb   $0x83,-0x1d(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x1f(%rbp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ADS1015_open>:
push   %rbp
push   %rbx
push   %rax
movss  %xmm0,0x4(%rsp)
mov    %edi,%ebp
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xebd(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     120c <ADS1015_open+0xcc>
mov    %eax,%ebx
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x49,%edx
call   1050 <ioctl@plt>
movb   $0x1,0x1(%rsp)
cmp    $0x4,%ebp
jae    1215 <ADS1015_open+0xd5>
shl    $0x4,%bpl
add    $0x40,%bpl
mov    %bpl,0x2(%rsp)
movss  0x4(%rsp),%xmm0
ucomiss 0xe59(%rip),%xmm0
jne    11ab <ADS1015_open+0x6b>
jnp    11f4 <ADS1015_open+0xb4>
mov    $0x2,%al
ucomiss 0xe50(%rip),%xmm0
jne    11b8 <ADS1015_open+0x78>
jnp    11ec <ADS1015_open+0xac>
mov    $0x4,%al
ucomiss 0xe47(%rip),%xmm0
jne    11c5 <ADS1015_open+0x85>
jnp    11ec <ADS1015_open+0xac>
mov    $0x6,%al
ucomiss 0xe3e(%rip),%xmm0
jne    11d2 <ADS1015_open+0x92>
jnp    11ec <ADS1015_open+0xac>
mov    $0x8,%al
ucomiss 0xe35(%rip),%xmm0
jne    11df <ADS1015_open+0x9f>
jnp    11ec <ADS1015_open+0xac>
mov    $0xa,%al
ucomiss 0xe2c(%rip),%xmm0
jne    122f <ADS1015_open+0xef>
jp     122f <ADS1015_open+0xef>
or     %al,%bpl
mov    %bpl,0x2(%rsp)
movb   $0x83,0x3(%rsp)
lea    0x1(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
jmp    1226 <ADS1015_open+0xe6>
lea    0xe10(%rip),%rdi
jmp    121c <ADS1015_open+0xdc>
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe3b(%rip),%rdi
jmp    121c <ADS1015_open+0xdc>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@.str.1 = [37 x i8] c"Adafruit_ADS1015: Failed to open bus\00"
@I2C_SLAVE = global i32 0
@.str.2 = [41 x i8] c"Adafruit_ADS1015: Invalid channel number\00"
@.str.3 = [31 x i8] c"Adafruit_ADS1015: Invalid gain\00"
define i32 @ADS1015_open(i32 %0, float %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca float
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [3 x i8]
store i32 %0, i32* %4
store float %1, float* %5
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %6
%9 = load i8*, i8** %6
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %126
16: ; preds = %2
%17 = load i32, i32* %7
%18 = load i32, i32* @I2C_SLAVE
%19 = call i32 @ioctl(i32 %17, i32 %18, i8 signext 73)
%20 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
store i8 1, i8* %20
%21 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 0, i8* %21
%22 = load i32, i32* %4
switch i32 %22, label %51 [
i32 0, label %23
i32 1, label %30
i32 2, label %37
i32 3, label %44
]
23: ; preds = %16
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = or i32 %26, 64
%28 = trunc i32 %27 to i8
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %28, i8* %29
br label %53
30: ; preds = %16
%31 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = or i32 %33, 80
%35 = trunc i32 %34 to i8
%36 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %35, i8* %36
br label %53
37: ; preds = %16
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = or i32 %40, 96
%42 = trunc i32 %41 to i8
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %42, i8* %43
br label %53
44: ; preds = %16
%45 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = or i32 %47, 112
%49 = trunc i32 %48 to i8
%50 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %49, i8* %50
br label %53
51: ; preds = %16
%52 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %126
53: ; preds = %44, %37, %30, %23
%54 = load float, float* %5
%55 = fcmp oeq float %54, 0x3FE5555560000000
br i1 %55, label %56, label %63
56: ; preds = %53
%57 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = or i32 %59, 0
%61 = trunc i32 %60 to i8
%62 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %61, i8* %62
br label %120
63: ; preds = %53
%64 = load float, float* %5
%65 = fcmp oeq float %64, 1.000000e+00
br i1 %65, label %66, label %73
66: ; preds = %63
%67 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = or i32 %69, 2
%71 = trunc i32 %70 to i8
%72 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %71, i8* %72
br label %119
73: ; preds = %63
%74 = load float, float* %5
%75 = fcmp oeq float %74, 2.000000e+00
br i1 %75, label %76, label %83
76: ; preds = %73
%77 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = or i32 %79, 4
%81 = trunc i32 %80 to i8
%82 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %81, i8* %82
br label %118
83: ; preds = %73
%84 = load float, float* %5
%85 = fcmp oeq float %84, 4.000000e+00
br i1 %85, label %86, label %93
86: ; preds = %83
%87 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%88 = load i8, i8* %87
%89 = sext i8 %88 to i32
%90 = or i32 %89, 6
%91 = trunc i32 %90 to i8
%92 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %91, i8* %92
br label %117
93: ; preds = %83
%94 = load float, float* %5
%95 = fcmp oeq float %94, 8.000000e+00
br i1 %95, label %96, label %103
96: ; preds = %93
%97 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%98 = load i8, i8* %97
%99 = sext i8 %98 to i32
%100 = or i32 %99, 8
%101 = trunc i32 %100 to i8
%102 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %101, i8* %102
br label %116
103: ; preds = %93
%104 = load float, float* %5
%105 = fcmp oeq float %104, 1.600000e+01
br i1 %105, label %106, label %113
106: ; preds = %103
%107 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%108 = load i8, i8* %107
%109 = sext i8 %108 to i32
%110 = or i32 %109, 10
%111 = trunc i32 %110 to i8
%112 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %111, i8* %112
br label %115
113: ; preds = %103
%114 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %126
115: ; preds = %106
br label %116
116: ; preds = %115, %96
br label %117
117: ; preds = %116, %86
br label %118
118: ; preds = %117, %76
br label %119
119: ; preds = %118, %66
br label %120
120: ; preds = %119, %56
%121 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
store i8 -125, i8* %121
%122 = load i32, i32* %7
%123 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%124 = call i32 @write(i32 %122, i8* %123, i32 3)
%125 = load i32, i32* %7
store i32 %125, i32* %3
br label %126
126: ; preds = %120, %113, %51, %14
%127 = load i32, i32* %3
ret i32 %127
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@.str.1 = [37 x i8] c"Adafruit_ADS1015: Failed to open bus\00"
@I2C_SLAVE = global i32 0
@.str.2 = [41 x i8] c"Adafruit_ADS1015: Invalid channel number\00"
@.str.3 = [31 x i8] c"Adafruit_ADS1015: Invalid gain\00"
define i32 @ADS1015_open(i32 %0, float %1) {
%3 = alloca [3 x 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, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %42
9: ; preds = %2
%10 = load i32, i32* @I2C_SLAVE
%11 = tail call i32 @ioctl(i32 %5, i32 %10, i8 signext 73)
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
store i8 1, i8* %12
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
%14 = icmp ult i32 %0, 4
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %40
17: ; preds = %9
%18 = trunc i32 %0 to i8
%19 = shl i8 %18, 4
%20 = add i8 %19, 64
store i8 %20, i8* %13
%21 = fcmp oeq float %1, 0x3FE5555560000000
br i1 %21, label %37, label %22
22: ; preds = %17
%23 = fcmp oeq float %1, 1.000000e+00
br i1 %23, label %34, label %24
24: ; preds = %22
%25 = fcmp oeq float %1, 2.000000e+00
br i1 %25, label %34, label %26
26: ; preds = %24
%27 = fcmp oeq float %1, 4.000000e+00
br i1 %27, label %34, label %28
28: ; preds = %26
%29 = fcmp oeq float %1, 8.000000e+00
br i1 %29, label %34, label %30
30: ; preds = %28
%31 = fcmp oeq float %1, 1.600000e+01
br i1 %31, label %34, label %32
32: ; preds = %30
%33 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %40
34: ; preds = %30, %28, %26, %24, %22
%35 = phi i8 [ 2, %22 ], [ 4, %24 ], [ 6, %26 ], [ 8, %28 ], [ 10, %30 ]
%36 = or i8 %20, %35
store i8 %36, i8* %13
br label %37
37: ; preds = %34, %17
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
store i8 -125, i8* %38
%39 = call i32 @write(i32 %5, i8* %12, i32 3)
br label %40
40: ; preds = %37, %32, %15
%41 = phi i32 [ -1, %15 ], [ %5, %37 ], [ -1, %32 ]
br label %42
42: ; preds = %40, %7
%43 = phi i32 [ -1, %7 ], [ %41, %40 ]
ret i32 %43
}