#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int obd_read(int, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *strstr(char *, char *);
long strtol(char *, char **, int);
long int *obd_get_bytes(int fd, size_t numbytes) {
  int charsread;
  char buf[50];
  char *pEnd;
  long int *byteptr;
  size_t expected = 6 + (numbytes * 3 - 1) + 1;
  charsread = obd_read(fd, buf);
  if (charsread == expected) {
    byteptr = (long int *)malloc(8 * (numbytes + 2));
    byteptr[0] = strtol(buf, &pEnd, 16);
    for (short int i = 1; i < numbytes + 2; i++) {
      byteptr[i] = strtol(pEnd, &pEnd, 16);
    }
  } else if (strstr(buf, "STOPPED") != NULL) {
    fprintf(stderr, "OBD responded \"STOPPED\"\n");
    byteptr = NULL;
  } else if (strstr(buf, "UNABLE TO CONNECT") != NULL) {
    fprintf(stderr, "OBD responded \"UNABLE TO CONNECT\", exiting.\n");
    exit(EXIT_FAILURE);
  } else {
    byteptr = NULL;
  }
  return byteptr;
}


x86 assembly (clang, -O0):
<obd_get_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
imul   $0x3,-0x10(%rbp),%rax
sub    $0x1,%rax
add    $0x6,%rax
add    $0x1,%rax
mov    %rax,-0x68(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1060 <obd_read@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x68(%rbp),%rax
jne    1228 <obd_get_bytes+0xc8>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
lea    -0x58(%rbp),%rsi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
mov    %rcx,(%rax)
movw   $0x1,-0x6a(%rbp)
movswq -0x6a(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0x2,%rcx
cmp    %rcx,%rax
jae    1223 <obd_get_bytes+0xc3>
mov    -0x58(%rbp),%rdi
lea    -0x58(%rbp),%rsi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %rax,%rdx
mov    -0x60(%rbp),%rax
movswq -0x6a(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x6a(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x6a(%rbp)
jmp    11da <obd_get_bytes+0x7a>
jmp    12b3 <obd_get_bytes+0x153>
lea    -0x50(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     1264 <obd_get_bytes+0x104>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x60(%rbp)
jmp    12ae <obd_get_bytes+0x14e>
lea    -0x50(%rbp),%rdi
lea    0xdb2(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     12a1 <obd_get_bytes+0x141>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movq   $0x0,-0x60(%rbp)
jmp    12ae <obd_get_bytes+0x14e>
jmp    12b3 <obd_get_bytes+0x153>
mov    -0x60(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<obd_get_bytes>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
lea    (%rsi,%rsi,2),%rbx
add    $0x6,%rbx
lea    0x10(%rsp),%rsi
call   1060 <obd_read@plt>
cltq
cmp    %rax,%rbx
jne    11e4 <obd_get_bytes+0x84>
lea    0x2(%r14),%rdi
shl    $0x3,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %rax,(%r12)
sub    $0xfffffffffffffffe,%r14
jae    122a <obd_get_bytes+0xca>
mov    $0x1,%ebx
lea    0x8(%rsp),%r15
xchg   %ax,%ax
mov    0x8(%rsp),%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %rax,(%r12,%rbx,8)
add    $0x1,%ebx
movswq %bx,%rbx
cmp    %rbx,%r14
ja     11c0 <obd_get_bytes+0x60>
jmp    122a <obd_get_bytes+0xca>
lea    0xe15(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     1211 <obd_get_bytes+0xb1>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1030 <fprintf@plt>
jmp    1227 <obd_get_bytes+0xc7>
lea    0xe09(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <strstr@plt>
test   %rax,%rax
jne    1239 <obd_get_bytes+0xd9>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"STOPPED\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"OBD responded \22STOPPED\22\0A\00"
@.str.2 = [18 x i8] c"UNABLE TO CONNECT\00"
@.str.3 = [45 x i8] c"OBD responded \22UNABLE TO CONNECT\22, exiting.\0A\00"
@EXIT_FAILURE = global i32 0
define i64* @obd_get_bytes(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca [50 x i8]
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i16
store i32 %0, i32* %3
store i64 %1, i64* %4
%11 = load i64, i64* %4
%12 = mul i64 %11, 3
%13 = sub i64 %12, 1
%14 = add i64 6, %13
%15 = add i64 %14, 1
store i64 %15, i64* %9
%16 = load i32, i32* %3
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%18 = call i32 @obd_read(i32 %16, i8* %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = load i64, i64* %9
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %51
23: ; preds = %2
%24 = load i64, i64* %4
%25 = add i64 %24, 2
%26 = mul i64 8, %25
%27 = trunc i64 %26 to i32
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i64*
store i64* %29, i64** %8
%30 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%31 = call i64 @strtol(i8* %30, i8** %7, i32 16)
%32 = load i64*, i64** %8
%33 = getelementptr inbounds i64, i64* %32, i64 0
store i64 %31, i64* %33
store i16 1, i16* %10
br label %34
34: ; preds = %47, %23
%35 = load i16, i16* %10
%36 = sext i16 %35 to i64
%37 = load i64, i64* %4
%38 = add i64 %37, 2
%39 = icmp ult i64 %36, %38
br i1 %39, label %40, label %50
40: ; preds = %34
%41 = load i8*, i8** %7
%42 = call i64 @strtol(i8* %41, i8** %7, i32 16)
%43 = load i64*, i64** %8
%44 = load i16, i16* %10
%45 = sext i16 %44 to i64
%46 = getelementptr inbounds i64, i64* %43, i64 %45
store i64 %42, i64* %46
br label %47
47: ; preds = %40
%48 = load i16, i16* %10
%49 = add i16 %48, 1
store i16 %49, i16* %10
br label %34
50: ; preds = %34
br label %70
51: ; preds = %2
%52 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%53 = call i32* @strstr(i8* %52, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i64* null, i64** %8
br label %69
58: ; preds = %51
%59 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%60 = call i32* @strstr(i8* %59, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %58
store i64* null, i64** %8
br label %68
68: ; preds = %67
br label %69
69: ; preds = %68, %55
br label %70
70: ; preds = %69, %50
%71 = load i64*, i64** %8
ret i64* %71
}

LLVM IR (-O2):
@.str = [8 x i8] c"STOPPED\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"OBD responded \22STOPPED\22\0A\00"
@.str.2 = [18 x i8] c"UNABLE TO CONNECT\00"
@.str.3 = [45 x i8] c"OBD responded \22UNABLE TO CONNECT\22, exiting.\0A\00"
@EXIT_FAILURE = global i32 0
define i64* @obd_get_bytes(i32 %0, i64 %1) {
%3 = alloca [50 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%6 = bitcast i8** %4 to i8*
%7 = mul i64 %1, 3
%8 = add i64 %7, 6
%9 = call i32 @obd_read(i32 %0, i8* %5)
%10 = sext i32 %9 to i64
%11 = icmp eq i64 %8, %10
br i1 %11, label %12, label %29
12: ; preds = %2
%13 = add i64 %1, 2
%14 = trunc i64 %13 to i32
%15 = shl i32 %14, 3
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i64*
%18 = call i64 @strtol(i8* %5, i8** %4, i32 16)
store i64 %18, i64* %17
%19 = icmp ult i64 %1, -2
br i1 %19, label %20, label %43
20: ; preds = %12, %20
%21 = phi i64 [ %27, %20 ], [ 1, %12 ]
%22 = load i8*, i8** %4
%23 = call i64 @strtol(i8* %22, i8** %4, i32 16)
%24 = getelementptr inbounds i64, i64* %17, i64 %21
store i64 %23, i64* %24
%25 = shl nsw i64 %21, 48
%26 = add i64 %25, 281474976710656
%27 = ashr exact i64 %26, 48
%28 = icmp ugt i64 %13, %27
br i1 %28, label %20, label %43
29: ; preds = %2
%30 = call i32* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %43
35: ; preds = %29
%36 = call i32* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i32* %36, null
br i1 %37, label %43, label %38
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %20, %12, %35, %32
%44 = phi i64* [ null, %32 ], [ null, %35 ], [ %17, %12 ], [ %17, %20 ]
ret i64* %44
}