#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*/ perror(char *);
int strlen(char const *);
int write(int, char const *, int);
int serialport_printlf(int fd, const char *str) {
  const char eol = '\n';
  int len = strlen(str);
  int n = write(fd, str, len);
  n += write(fd, &eol, 1);
  if (n != len + 1) {
    perror("serialport_write: couldn't write whole string\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<serialport_printlf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0xa,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
je     11a1 <serialport_printlf+0x71>
lea    0xe70(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <serialport_printlf+0x78>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_printlf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
movb   $0xa,0xf(%rsp)
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%ecx
add    %ebx,%ecx
add    $0x1,%ebp
xor    %eax,%eax
cmp    %ebp,%ecx
jne    1184 <serialport_printlf+0x54>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe75(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    117b <serialport_printlf+0x4b>

LLVM IR (-O0):
@.str = [47 x i8] c"serialport_write: couldn't write whole string\0A\00"
define i32 @serialport_printlf(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8 10, i8* %6
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %7
%11 = load i32, i32* %4
%12 = load i8*, i8** %5
%13 = load i32, i32* %7
%14 = call i32 @write(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %4
%16 = call i32 @write(i32 %15, i8* %6, i32 1)
%17 = load i32, i32* %8
%18 = add nsw i32 %17, %16
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 1
%22 = icmp ne i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %26
25: ; preds = %2
store i32 0, i32* %3
br label %26
26: ; preds = %25, %23
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str = [47 x i8] c"serialport_write: couldn't write whole string\0A\00"
define i32 @serialport_printlf(i32 %0, i8* %1) {
%3 = alloca i8
store i8 10, i8* %3
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32 @write(i32 %0, i8* %1, i32 %4)
%6 = call i32 @write(i32 %0, i8* %3, i32 1)
%7 = add nsw i32 %6, %5
%8 = add nsw i32 %4, 1
%9 = icmp eq i32 %7, %8
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %2, %10
%13 = phi i32 [ -1, %10 ], [ 0, %2 ]
ret i32 %13
}