#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int serial_open(char *);
scalar_t__ serial_recv(int);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int fd = serial_open("/dev/tty.usbmodem12341");
  if (fd < 0) {
    printf("Couldn't open serial\n");
    return 1;
  }
  for (;;) {
    printf("%c", (char)serial_recv(fd));
    fflush(stdout);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
call   1050 <serial_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1188 <main+0x48>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    118d <main+0x4d>
mov    -0x14(%rbp),%edi
call   1030 <serial_recv@plt>
movsbl %al,%esi
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    118d <main+0x4d>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1060 <serial_open@plt>
test   %eax,%eax
js     1189 <main+0x39>
mov    %eax,%ebx
mov    0x2e73(%rip),%r14
nopl   (%rax)
mov    %ebx,%edi
call   1040 <serial_recv@plt>
movsbl %al,%edi
call   1030 <putchar@plt>
mov    (%r14),%edi
call   1070 <fflush@plt>
jmp    1170 <main+0x20>
lea    0xe87(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/dev/tty.usbmodem12341\00"
@.str.1 = [22 x i8] c"Couldn't open serial\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 @serial_open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
12: ; preds = %2
br label %13
13: ; preds = %13, %12
%14 = load i32, i32* %6
%15 = call i64 @serial_recv(i32 %14)
%16 = trunc i64 %15 to i8
%17 = sext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @stdout
%20 = call i32 @fflush(i32 %19)
br label %13
}

LLVM IR (-O2):
@.str = [23 x i8] c"/dev/tty.usbmodem12341\00"
@stdout = global i32 0
@str = [21 x i8] c"Couldn't open serial\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @serial_open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
ret i32 1
7: ; preds = %2, %7
%8 = tail call i64 @serial_recv(i32 %3)
%9 = trunc i64 %8 to i32
%10 = shl i32 %9, 24
%11 = ashr exact i32 %10, 24
%12 = tail call i32 @putchar(i32 %11)
%13 = load i32, i32* @stdout
%14 = tail call i32 @fflush(i32 %13)
br label %7
}