#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ writeToFile(int);
char *getDataLinux(char **mydata) {
  char *cat_command = "sudo cat /dev/ttyUSB0";
  FILE *usbptr = popen(cat_command, "r");
  if (usbptr == NULL) {
    printf("USB is NULL\n");
  }
  int ctr = 0;
  int numSamp = 100;
  printf("reading\n");
  int indata = fgetc(usbptr);
  printf("done reading\n");
  while (indata != EOF) {
    if (feof(usbptr)) {
      printf("At EOF\n");
      break;
    }
    writeToFile(indata);
    if (indata == 'z') {
      break;
    } else {
      indata = fgetc(usbptr);
      ctr++;
      if (ctr > numSamp) {
        break;
      }
    }
  }
  int closeflag = pclose(usbptr);
  if (closeflag < 0) {
    printf("error on close\n");
  }
  return "z";
}


x86 assembly (clang, -O0):
<getDataLinux>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a2 <getDataLinux+0x42>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x64,-0x20(%rbp)
lea    0xe73(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x24(%rbp)
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%eax
je     1251 <getDataLinux+0xf1>
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     120a <getDataLinux+0xaa>
lea    0xe33(%rip),%rdi
call   1040 <printf@plt>
jmp    1251 <getDataLinux+0xf1>
mov    -0x24(%rbp),%edi
call   1030 <writeToFile@plt>
cmpl   $0x7a,-0x24(%rbp)
jne    1221 <getDataLinux+0xc1>
jmp    1251 <getDataLinux+0xf1>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    1247 <getDataLinux+0xe7>
jmp    1251 <getDataLinux+0xf1>
jmp    124c <getDataLinux+0xec>
jmp    11d4 <getDataLinux+0x74>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1273 <getDataLinux+0x113>
lea    0xdcd(%rip),%rdi
call   1040 <printf@plt>
lea    0xdd1(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getDataLinux>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe92(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    118e <getDataLinux+0x2e>
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebp
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x65,%ebx
mov    0x2e2c(%rip),%r15
nopl   0x0(%rax)
cmp    (%r15),%ebp
je     11fb <getDataLinux+0x9b>
mov    %r14,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    11ef <getDataLinux+0x8f>
mov    %ebp,%edi
call   1040 <writeToFile@plt>
cmp    $0x7a,%ebp
je     11fb <getDataLinux+0x9b>
mov    %r14,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebp
add    $0xffffffff,%ebx
jne    11c0 <getDataLinux+0x60>
jmp    11fb <getDataLinux+0x9b>
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1050 <pclose@plt>
test   %eax,%eax
jns    1213 <getDataLinux+0xb3>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
lea    0xdfe(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"sudo cat /dev/ttyUSB0\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"USB is NULL\0A\00"
@.str.3 = [9 x i8] c"reading\0A\00"
@.str.4 = [14 x i8] c"done reading\0A\00"
@EOF = global i32 0
@.str.5 = [8 x i8] c"At EOF\0A\00"
@.str.6 = [16 x i8] c"error on close\0A\00"
@.str.7 = [2 x i8] c"z\00"
define i8* @getDataLinux(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8** %0, i8*** %2
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %1
store i32 0, i32* %5
store i32 100, i32* %6
%16 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32*, i32** %4
%18 = call i32 @fgetc(i32* %17)
store i32 %18, i32* %7
%19 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %46, %15
%21 = load i32, i32* %7
%22 = load i32, i32* @EOF
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %47
24: ; preds = %20
%25 = load i32*, i32** %4
%26 = call i64 @feof(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %47
30: ; preds = %24
%31 = load i32, i32* %7
%32 = call i32 @writeToFile(i32 %31)
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, 122
br i1 %34, label %35, label %36
35: ; preds = %30
br label %47
36: ; preds = %30
%37 = load i32*, i32** %4
%38 = call i32 @fgetc(i32* %37)
store i32 %38, i32* %7
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = load i32, i32* %6
%43 = icmp sgt i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %36
br label %47
45: ; preds = %36
br label %46
46: ; preds = %45
br label %20
47: ; preds = %44, %35, %28, %20
%48 = load i32*, i32** %4
%49 = call i32 @pclose(i32* %48)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %54
54: ; preds = %52, %47
ret i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0)
}

LLVM IR (-O2):
@.str = [22 x i8] c"sudo cat /dev/ttyUSB0\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.7 = [2 x i8] c"z\00"
@str = [8 x i8] c"reading\00"
@str.8 = [13 x i8] c"done reading\00"
@str.9 = [15 x i8] c"error on close\00"
@str.10 = [7 x i8] c"At EOF\00"
@str.11 = [12 x i8] c"USB is NULL\00"
define i8* @getDataLinux(i8** nocapture readnone %0) {
%2 = tail call i32* @popen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.11, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @fgetc(i32* %2)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
br label %10
10: ; preds = %23, %6
%11 = phi i32 [ %8, %6 ], [ %24, %23 ]
%12 = phi i32 [ 0, %6 ], [ %25, %23 ]
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %11, %13
br i1 %14, label %27, label %15
15: ; preds = %10
%16 = tail call i64 @feof(i32* %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.10, i64 0, i64 0))
br label %27
20: ; preds = %15
%21 = tail call i32 @writeToFile(i32 %11)
%22 = icmp eq i32 %11, 122
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = tail call i32 @fgetc(i32* %2)
%25 = add nuw nsw i32 %12, 1
%26 = icmp eq i32 %25, 101
br i1 %26, label %27, label %10
27: ; preds = %23, %20, %10, %18
%28 = tail call i32 @pclose(i32* %2)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
ret i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0)
}