#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 ssize_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ barcode;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ PIPE_BUF;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ send_barcode(int /*<<< orphan*/ *, char *, int);
char *strerror(int /*<<< orphan*/);
void read_barcode(Display *display, int serial_fd, int pipe_to_parent) {
  char buf[128];
  char barcode[PIPE_BUF + 1];
  int codesize = 0;
  while (true) {
    ssize_t n;
    unsigned int i;
    n = read(serial_fd, buf, sizeof(buf));
    if (n <= 0)
      fatal("Error reading serial device: %s (%d)\n",
            n ? strerror(errno) : "(no data)", errno);
    for (i = 0; i < n; i++) {
      if (buf[i] == '\n') {
        continue;
      } else if (buf[i] == '\r') {
        barcode[codesize] = '\0';
        send_barcode(display, barcode, pipe_to_parent);
        codesize = 0;
      } else {
        if (codesize == sizeof(barcode) - 1)
          codesize = 0;
        barcode[codesize++] = buf[i];
      }
    }
  }
}


x86 assembly (clang, -O0):
<read_barcode>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0xc0(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x98(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xb8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0xa0(%rbp)
movl   $0x0,-0xa4(%rbp)
jmp    11a3 <read_barcode+0x63>
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,-0xa8(%rbp)
cmpl   $0x0,-0xa8(%rbp)
jg     1220 <read_barcode+0xe0>
cmpl   $0x0,-0xa8(%rbp)
je     11f1 <read_barcode+0xb1>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,-0xc8(%rbp)
jmp    1204 <read_barcode+0xc4>
lea    0xe2e(%rip),%rax
mov    %rax,-0xc8(%rbp)
jmp    1204 <read_barcode+0xc4>
mov    -0xc8(%rbp),%rsi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
lea    0xde5(%rip),%rdi
call   1060 <fatal@plt>
movl   $0x0,-0xac(%rbp)
mov    -0xac(%rbp),%eax
cmp    -0xa8(%rbp),%eax
jae    130a <read_barcode+0x1ca>
mov    -0xac(%rbp),%eax
movsbl -0x90(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1258 <read_barcode+0x118>
jmp    12f6 <read_barcode+0x1b6>
mov    -0xac(%rbp),%eax
movsbl -0x90(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
jne    129c <read_barcode+0x15c>
mov    -0xb8(%rbp),%rsi
movslq -0xa4(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
call   1030 <send_barcode@plt>
movl   $0x0,-0xa4(%rbp)
jmp    12ec <read_barcode+0x1ac>
mov    -0xc0(%rbp),%rcx
movslq -0xa4(%rbp),%rax
sub    $0x1,%rcx
cmp    %rcx,%rax
jne    12c1 <read_barcode+0x181>
movl   $0x0,-0xa4(%rbp)
mov    -0xb8(%rbp),%rax
mov    -0xac(%rbp),%ecx
mov    -0x90(%rbp,%rcx,1),%dl
mov    -0xa4(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0xa4(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    12f1 <read_barcode+0x1b1>
jmp    12f6 <read_barcode+0x1b6>
mov    -0xac(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xac(%rbp)
jmp    122a <read_barcode+0xea>
jmp    119e <read_barcode+0x5e>

x86 assembly (clang, -O2):
<read_barcode>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %edx,-0x2c(%rbp)
mov    %esi,%r12d
mov    %rdi,-0x40(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %rsp,%r14
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x38(%rbp)
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
lea    -0xc0(%rbp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jg     11ff <read_barcode+0xbf>
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
jne    11e0 <read_barcode+0xa0>
lea    0xe37(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1060 <fatal@plt>
jmp    11a0 <read_barcode+0x60>
nopw   0x0(%rax,%rax,1)
mov    %edx,%edi
call   1050 <strerror@plt>
mov    0x2df2(%rip),%rcx
mov    (%rcx),%edx
lea    0xe09(%rip),%rdi
mov    %rax,%rsi
call   1060 <fatal@plt>
mov    %r13d,%r13d
xor    %ebx,%ebx
jmp    1235 <read_barcode+0xf5>
cs nopw 0x0(%rax,%rax,1)
cmp    %r15,-0x38(%rbp)
mov    $0x0,%ecx
cmove  %ecx,%r15d
movslq %r15d,%rcx
add    $0x1,%r15d
mov    %al,(%r14,%rcx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
je     1190 <read_barcode+0x50>
movzbl -0xc0(%rbp,%rbx,1),%eax
ff
cmp    $0xa,%al
je     1228 <read_barcode+0xe8>
movslq %r15d,%r15
cmp    $0xd,%al
jne    1210 <read_barcode+0xd0>
movb   $0x0,(%r14,%r15,1)
mov    -0x40(%rbp),%rdi
mov    %r14,%rsi
mov    -0x2c(%rbp),%edx
call   1030 <send_barcode@plt>
xor    %r15d,%r15d
jmp    1228 <read_barcode+0xe8>

LLVM IR (-O0):
@PIPE_BUF = global i32 0
@.str = [38 x i8] c"Error reading serial device: %s (%d)\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"(no data)\00"
define void @read_barcode(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%13 = load i32, i32* @PIPE_BUF
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
store i32 0, i32* %10
br label %18
18: ; preds = %83, %3
br label %19
19: ; preds = %18
%20 = load i32, i32* %5
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 128)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %19
%26 = load i32, i32* %11
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
br label %32
31: ; preds = %25
br label %32
32: ; preds = %31, %28
%33 = phi i8* [ %30, %28 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %31 ]
%34 = load i32, i32* @errno
%35 = call i32 @fatal(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %33, i32 %34)
br label %36
36: ; preds = %32, %19
store i32 0, i32* %12
br label %37
37: ; preds = %80, %36
%38 = load i32, i32* %12
%39 = load i32, i32* %11
%40 = icmp ult i32 %38, %39
br i1 %40, label %41, label %83
41: ; preds = %37
%42 = load i32, i32* %12
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 10
br i1 %47, label %48, label %49
48: ; preds = %41
br label %80
49: ; preds = %41
%50 = load i32, i32* %12
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 13
br i1 %55, label %56, label %63
56: ; preds = %49
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %17, i64 %58
store i8 0, i8* %59
%60 = load i32*, i32** %4
%61 = load i32, i32* %6
%62 = call i32 @send_barcode(i32* %60, i8* %17, i32 %61)
store i32 0, i32* %10
br label %78
63: ; preds = %49
%64 = load i32, i32* %10
%65 = sext i32 %64 to i64
%66 = sub i64 %15, 1
%67 = icmp eq i64 %65, %66
br i1 %67, label %68, label %69
68: ; preds = %63
store i32 0, i32* %10
br label %69
69: ; preds = %68, %63
%70 = load i32, i32* %12
%71 = zext i32 %70 to i64
%72 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %71
%73 = load i8, i8* %72
%74 = load i32, i32* %10
%75 = add nsw i32 %74, 1
store i32 %75, i32* %10
%76 = sext i32 %74 to i64
%77 = getelementptr inbounds i8, i8* %17, i64 %76
store i8 %73, i8* %77
br label %78
78: ; preds = %69, %56
br label %79
79: ; preds = %78
br label %80
80: ; preds = %79, %48
%81 = load i32, i32* %12
%82 = add i32 %81, 1
store i32 %82, i32* %12
br label %37
83: ; preds = %37
br label %18
}

LLVM IR (-O2):
@PIPE_BUF = global i32 0
@.str = [38 x i8] c"Error reading serial device: %s (%d)\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"(no data)\00"
define void @read_barcode(i32* %0, i32 %1, i32 %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @PIPE_BUF
%7 = add nsw i32 %6, 1
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = add nsw i64 %8, -1
br label %11
11: ; preds = %43, %3
%12 = phi i32 [ 0, %3 ], [ %44, %43 ]
br label %13
13: ; preds = %11, %19
%14 = call i32 @read(i32 %1, i8* %5, i32 128)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %25
16: ; preds = %13
%17 = icmp eq i32 %14, 0
%18 = load i32, i32* @errno
br i1 %17, label %19, label %21
19: ; preds = %16
%20 = call i32 @fatal(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %13
21: ; preds = %16
%22 = call i8* @strerror(i32 %18)
%23 = load i32, i32* @errno
%24 = call i32 @fatal(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %23)
br label %25
25: ; preds = %13, %21
%26 = zext i32 %14 to i64
br label %27
27: ; preds = %25, %43
%28 = phi i64 [ 0, %25 ], [ %45, %43 ]
%29 = phi i32 [ %12, %25 ], [ %44, %43 ]
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %28
%31 = load i8, i8* %30
switch i8 %31, label %36 [
i8 10, label %43
i8 13, label %32
]
32: ; preds = %27
%33 = sext i32 %29 to i64
%34 = getelementptr inbounds i8, i8* %9, i64 %33
store i8 0, i8* %34
%35 = call i32 @send_barcode(i32* %0, i8* %9, i32 %2)
br label %43
36: ; preds = %27
%37 = sext i32 %29 to i64
%38 = icmp eq i64 %10, %37
%39 = select i1 %38, i32 0, i32 %29
%40 = add nsw i32 %39, 1
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %9, i64 %41
store i8 %31, i8* %42
br label %43
43: ; preds = %27, %36, %32
%44 = phi i32 [ %29, %27 ], [ 0, %32 ], [ %40, %36 ]
%45 = add nuw nsw i64 %28, 1
%46 = icmp eq i64 %45, %26
br i1 %46, label %11, label %27
}