#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;

/* Variables and functions */
int /*<<< orphan*/ logMessage(int, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
void printAndLog(int fd, char *buffer, int size, int fd_logfile,
                 ssize_t *msg_counter) {
  ssize_t readAmnt = read(fd, buffer, size);
  if (readAmnt == -1) {
    printf("error while reading serial\n");
    return;
  }
  *msg_counter += readAmnt;
  printf("Read %d bytes: %s           #total until now: %d\n", readAmnt, buffer,
         *msg_counter);
  printf("First char: %c   -   Last char: %c\n\n", buffer[0],
         buffer[readAmnt - 1]);
  if (fd_logfile < 0)
    return;
  logMessage(fd_logfile, buffer, readAmnt);
}


x86 assembly (clang, -O0):
<printAndLog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1178 <printAndLog+0x48>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e2 <printAndLog+0xb2>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jge    11d3 <printAndLog+0xa3>
jmp    11e2 <printAndLog+0xb2>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <logMessage@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printAndLog>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%r15
mov    %ecx,%r14d
mov    %rsi,%rbx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     11ac <printAndLog+0x6c>
mov    %eax,%ebp
mov    (%r15),%ecx
add    %eax,%ecx
mov    %ecx,(%r15)
lea    0xe95(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
movsbl (%rbx),%esi
movslq %ebp,%rax
movsbl -0x1(%rax,%rbx,1),%edx
lea    0xea9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14d,%r14d
js     11c2 <printAndLog+0x82>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <logMessage@plt>
lea    0xea4(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"error while reading serial\0A\00"
@.str.1 = [50 x i8] c"Read %d bytes: %s #total until now: %d\0A\00"
@.str.2 = [37 x i8] c"First char: %c - Last char: %c\0A\0A\00"
define void @printAndLog(i32 %0, i8* %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32* %4, i32** %10
%12 = load i32, i32* %6
%13 = load i8*, i8** %7
%14 = load i32, i32* %8
%15 = call i32 @read(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %50
20: ; preds = %5
%21 = load i32, i32* %11
%22 = load i32*, i32** %10
%23 = load i32, i32* %22
%24 = add nsw i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* %11
%26 = load i8*, i8** %7
%27 = load i32*, i32** %10
%28 = load i32, i32* %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %25, i8* %26, i32 %28)
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = load i8*, i8** %7
%35 = load i32, i32* %11
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %40)
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %20
br label %50
45: ; preds = %20
%46 = load i32, i32* %9
%47 = load i8*, i8** %7
%48 = load i32, i32* %11
%49 = call i32 @logMessage(i32 %46, i8* %47, i32 %48)
br label %50
50: ; preds = %45, %44, %18
ret void
}

LLVM IR (-O2):
@.str.1 = [50 x i8] c"Read %d bytes: %s #total until now: %d\0A\00"
@.str.2 = [37 x i8] c"First char: %c - Last char: %c\0A\0A\00"
@str = [27 x i8] c"error while reading serial\00"
define void @printAndLog(i32 %0, i8* %1, i32 %2, i32 %3, i32* nocapture %4) {
%6 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %25
10: ; preds = %5
%11 = load i32, i32* %4
%12 = add nsw i32 %11, %6
store i32 %12, i32* %4
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %6, i8* %1, i32 %12)
%14 = load i8, i8* %1
%15 = sext i8 %14 to i32
%16 = add nsw i32 %6, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %20)
%22 = icmp slt i32 %3, 0
br i1 %22, label %25, label %23
23: ; preds = %10
%24 = tail call i32 @logMessage(i32 %3, i8* %1, i32 %6)
br label %25
25: ; preds = %10, %23, %8
ret void
}