#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*/ printf(char *);
int read(int, char *, int);
int read_adc(int fd, int *data) {
  char buf[2] = {0};
  if (read(fd, buf, 2) != 2) {
    printf("Adafruit_ADS1015: Failed to read data");
    return -1;
  }
  *data = ((int)buf[0]) << 4 | ((int)buf[1]) >> 4;
  if ((*data & 0x800) != 0)
    *data = (*data || 0x7FF);
  return 0;
}


x86 assembly (clang, -O0):
<read_adc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x12(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
je     1181 <read_adc+0x51>
lea    0xe90(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <read_adc+0xac>
movsbl -0x12(%rbp),%ecx
shl    $0x4,%ecx
movsbl -0x11(%rbp),%eax
sar    $0x4,%eax
or     %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
and    $0x800,%eax
cmp    $0x0,%eax
je     11d5 <read_adc+0xa5>
mov    -0x10(%rbp),%rcx
mov    $0x1,%al
cmpl   $0x0,(%rcx)
mov    %al,-0x13(%rbp)
jne    11c7 <read_adc+0x97>
mov    $0x1,%al
mov    %al,-0x13(%rbp)
jmp    11c7 <read_adc+0x97>
mov    -0x13(%rbp),%al
and    $0x1,%al
movzbl %al,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_adc>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movw   $0x0,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
call   1040 <read@plt>
cmp    $0x2,%eax
jne    116b <read_adc+0x4b>
movsbl 0xe(%rsp),%eax
shl    $0x4,%eax
mov    0xf(%rsp),%cl
sar    $0x4,%cl
movsbl %cl,%ecx
or     %eax,%ecx
test   $0x800,%ecx
mov    $0x1,%eax
cmove  %ecx,%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    117c <read_adc+0x5c>
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [38 x i8] c"Adafruit_ADS1015: Failed to read data\00"
define i32 @read_adc(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [2 x i8]
store i32 %0, i32* %4
store i32* %1, i32** %5
%7 = bitcast [2 x i8]* %6 to i8*
%8 = load i32, i32* %4
%9 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 2)
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
14: ; preds = %2
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = shl i32 %17, 4
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 1
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = ashr i32 %21, 4
%23 = or i32 %18, %22
%24 = load i32*, i32** %5
store i32 %23, i32* %24
%25 = load i32*, i32** %5
%26 = load i32, i32* %25
%27 = and i32 %26, 2048
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %14
%30 = load i32*, i32** %5
%31 = load i32, i32* %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %29
br label %34
34: ; preds = %33, %29
%35 = phi i1 [ true, %29 ], [ true, %33 ]
%36 = zext i1 %35 to i32
%37 = load i32*, i32** %5
store i32 %36, i32* %37
br label %38
38: ; preds = %34, %14
store i32 0, i32* %3
br label %39
39: ; preds = %38, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [38 x i8] c"Adafruit_ADS1015: Failed to read data\00"
define i32 @read_adc(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
store i16 0, i16* %3
%5 = call i32 @read(i32 %0, i8* %4, i32 2)
%6 = icmp eq i32 %5, 2
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %22
9: ; preds = %2
%10 = bitcast i16* %3 to [2 x i8]*
%11 = load i8, i8* %4
%12 = sext i8 %11 to i32
%13 = shl nsw i32 %12, 4
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
%15 = load i8, i8* %14
%16 = ashr i8 %15, 4
%17 = sext i8 %16 to i32
%18 = or i32 %13, %17
%19 = and i32 %18, 2048
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32 %18, i32 1
store i32 %21, i32* %1
br label %22
22: ; preds = %9, %7
%23 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %23
}