#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 O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, unsigned char *, int);
int /*<<< orphan*/ writeResetToDefaults(int);
void talkToGPS() {
  printf("Let's talk to GPS!\n");
  const char *device = "/dev/ttyUSB0";
  int fd = open(device, O_RDWR | O_NOCTTY);
  if (fd == -1) {
    printf("error opening\n");
    perror(device);
    close(fd);
    return;
  }
  printf("opened /dev/ttyUSB0\n");
  writeResetToDefaults(fd);
  int count = 0;
  while (1) {
    count++;
    unsigned char response[1024];
    size_t bytes_read = read(fd, response, 1024);
    if (bytes_read == -1) {
      printf("error reading (error %d)\n", errno);
      perror("perror reading");
      break;
    }
    response[bytes_read] = '\0';
    printf("%s\n", response);
    if (count == 1) {
      writeResetToDefaults(fd);
    }
    if (count == 10) {
    }
  }
  close(fd);
}


x86 assembly (clang, -O0):
<talkToGPS>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe94(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11d0 <talkToGPS+0x70>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    1299 <talkToGPS+0x139>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1030 <writeResetToDefaults@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0xffffffffffffffff,-0x418(%rbp)
ff
jne    1247 <talkToGPS+0xe7>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe22(%rip),%rdi
call   1080 <perror@plt>
jmp    1291 <talkToGPS+0x131>
mov    -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x10(%rbp)
jne    127d <talkToGPS+0x11d>
mov    -0xc(%rbp),%edi
call   1030 <writeResetToDefaults@plt>
cmpl   $0xa,-0x10(%rbp)
jne    128c <talkToGPS+0x12c>
jmp    128c <talkToGPS+0x12c>
jmp    11ed <talkToGPS+0x8d>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<talkToGPS>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e5c(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea5(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    11ca <talkToGPS+0x5a>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
lea    0xe88(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1274 <talkToGPS+0x104>
mov    %eax,%ebp
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1040 <writeResetToDefaults@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffffffffffff,%rax
je     1251 <talkToGPS+0xe1>
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1040 <writeResetToDefaults@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffffffffffff,%rax
je     1251 <talkToGPS+0xe1>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x0,(%rsp,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffffffffffff,%rax
jne    1230 <talkToGPS+0xc0>
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
lea    0xd9f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdab(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
add    $0x408,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <close@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Let's talk to GPS!\0A\00"
@.str.1 = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.2 = [15 x i8] c"error opening\0A\00"
@.str.3 = [21 x i8] c"opened /dev/ttyUSB0\0A\00"
@.str.4 = [26 x i8] c"error reading (error %d)\0A\00"
@errno = global i32 0
@.str.5 = [15 x i8] c"perror reading\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define void @talkToGPS() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i64
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %1
%7 = load i8*, i8** %1
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%16 = load i8*, i8** %1
%17 = call i32 @perror(i8* %16)
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
br label %54
20: ; preds = %0
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* %2
%23 = call i32 @writeResetToDefaults(i32 %22)
store i32 0, i32* %3
br label %24
24: ; preds = %20, %50
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
%27 = load i32, i32* %2
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = call i64 @read(i32 %27, i8* %28, i32 1024)
store i64 %29, i64* %5
%30 = load i64, i64* %5
%31 = icmp eq i64 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %24
%33 = load i32, i32* @errno
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %51
36: ; preds = %24
%37 = load i64, i64* %5
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %3
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = load i32, i32* %2
%45 = call i32 @writeResetToDefaults(i32 %44)
br label %46
46: ; preds = %43, %36
%47 = load i32, i32* %3
%48 = icmp eq i32 %47, 10
br i1 %48, label %49, label %50
49: ; preds = %46
br label %50
50: ; preds = %49, %46
br label %24
51: ; preds = %32
%52 = load i32, i32* %2
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51, %14
ret void
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.4 = [26 x i8] c"error reading (error %d)\0A\00"
@errno = global i32 0
@.str.5 = [15 x i8] c"perror reading\00"
@str = [19 x i8] c"Let's talk to GPS!\00"
@str.7 = [20 x i8] c"opened /dev/ttyUSB0\00"
@str.8 = [14 x i8] c"error opening\00"
define void @talkToGPS() {
%1 = alloca [1024 x i8]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%10 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %33
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
%13 = tail call i32 @writeResetToDefaults(i32 %6)
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%15 = call i64 @read(i32 %6, i8* %14, i32 1024)
%16 = icmp eq i64 %15, -1
br i1 %16, label %29, label %17
17: ; preds = %11
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %15
store i8 0, i8* %18
%19 = call i32 @puts(i8* %14)
%20 = tail call i32 @writeResetToDefaults(i32 %6)
%21 = call i64 @read(i32 %6, i8* %14, i32 1024)
%22 = icmp eq i64 %21, -1
br i1 %22, label %29, label %23
23: ; preds = %17, %23
%24 = phi i64 [ %27, %23 ], [ %21, %17 ]
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 @puts(i8* %14)
%27 = call i64 @read(i32 %6, i8* %14, i32 1024)
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %23
29: ; preds = %23, %17, %11
%30 = load i32, i32* @errno
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %29, %8
%34 = phi i32 [ %6, %29 ], [ -1, %8 ]
%35 = tail call i32 @close(i32 %34)
ret void
}