#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 */
struct _serial {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int serial_readln(struct _serial *, char *, int);
int lpc_read(struct _serial *serial, char *buffer, int len) {
  int count;
  count = serial_readln(serial, buffer, len);
  if (count == len) {
    printf("Error: read error(128)\n");
    return -1;
  }
  count -= 2;
  buffer[count] = 0;
  printf("buffer='%s'\n", buffer);
  return count;
}


x86 assembly (clang, -O0):
<lpc_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <serial_readln@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    116c <lpc_read+0x4c>
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1199 <lpc_read+0x79>
mov    -0x20(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rsi
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lpc_read>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
call   1050 <serial_readln@plt>
cmp    %ebx,%eax
jne    1155 <lpc_read+0x25>
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1173 <lpc_read+0x43>
movslq %eax,%rbx
movb   $0x0,-0x2(%r14,%rbx,1)
add    $0xfffffffffffffffe,%rbx
lea    0xe97(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct._serial = type { i32 }
@.str = [24 x i8] c"Error: read error(128)\0A\00"
@.str.1 = [13 x i8] c"buffer='%s'\0A\00"
define i32 @lpc_read(%struct._serial* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct._serial*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store %struct._serial* %0, %struct._serial** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load %struct._serial*, %struct._serial** %5
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @serial_readln(%struct._serial* %9, i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* %7
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %28
18: ; preds = %3
%19 = load i32, i32* %8
%20 = sub nsw i32 %19, 2
store i32 %20, i32* %8
%21 = load i8*, i8** %6
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 0, i8* %24
%25 = load i8*, i8** %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %8
store i32 %27, i32* %4
br label %28
28: ; preds = %18, %16
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
%struct._serial = type { i32 }
@.str.1 = [13 x i8] c"buffer='%s'\0A\00"
@str = [23 x i8] c"Error: read error(128)\00"
define i32 @lpc_read(%struct._serial* %0, i8* %1, i32 %2) {
%4 = tail call i32 @serial_readln(%struct._serial* %0, i8* %1, i32 %2)
%5 = icmp eq i32 %4, %2
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %13
8: ; preds = %3
%9 = add nsw i32 %4, -2
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %11
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %13
13: ; preds = %8, %6
%14 = phi i32 [ -1, %6 ], [ %9, %8 ]
ret i32 %14
}