#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(int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int serial_init();
int main() {
  int sfd, n;
  char buf[128];
  sfd = serial_init();
  while (1) {
    n = read(sfd, buf, 128);
    if (n < 0) {
      exit(2);
    }
    buf[n] = 0;
    printf("%s", buf);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <serial_init@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1187 <main+0x47>
mov    $0x2,%edi
call   1050 <exit@plt>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rsi
lea    0xe5f(%rip),%rdi
call   1030 <printf@plt>
jmp    115c <main+0x1c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
xor    %eax,%eax
call   1060 <serial_init@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x80,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11a4 <main+0x64>
lea    0xe92(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    1180 <main+0x40>
mov    $0x2,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [128 x i8]
store i32 0, i32* %1
%5 = call i32 (...) @serial_init()
store i32 %5, i32* %2
br label %6
6: ; preds = %0, %14
%7 = load i32, i32* %2
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 128)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = call i32 @exit(i32 2)
unreachable
14: ; preds = %6
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %16
store i8 0, i8* %17
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%19 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %6
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @serial_init()
%4 = call i32 @read(i32 %3, i8* %2, i32 128)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %8, %0
%7 = call i32 @exit(i32 2)
unreachable
8: ; preds = %0, %8
%9 = phi i32 [ %13, %8 ], [ %4, %0 ]
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %2)
%13 = call i32 @read(i32 %3, i8* %2, i32 128)
%14 = icmp slt i32 %13, 0
br i1 %14, label %6, label %8
}