#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int write(int, char *, int);
int PMIU_writeline(int fd, char *buf) {
  int size, n;
  size = strlen(buf);
  if (size > 1024) {
    buf[1024 - 1] = '\0';
    printf("write_line: message string too big: :%s:\n", buf);
  } else if (buf[strlen(buf) - 1] != '\n')
    printf("write_line: message string doesn't end in newline: :%s:\n", buf);
  else {
    do {
      n = write(fd, buf, size);
    } while (n == -1 && errno == EINTR);
    if (n < 0) {
      printf("write_line error; fd=%d buf=:%s:\n", fd, buf);
      perror("system msg for write_line failure ");
      return (-1);
    }
    if (n < size)
      printf("write_line failed to write entire message\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<PMIU_writeline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x400,-0x14(%rbp)
jle    118a <PMIU_writeline+0x4a>
mov    -0x10(%rbp),%rax
movb   $0x0,0x3ff(%rax)
mov    -0x10(%rbp),%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1271 <PMIU_writeline+0x131>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11cb <PMIU_writeline+0x8b>
mov    -0x10(%rbp),%rsi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    126c <PMIU_writeline+0x12c>
jmp    11d0 <PMIU_writeline+0x90>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jne    120b <PMIU_writeline+0xcb>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11d0 <PMIU_writeline+0x90>
cmpl   $0x0,-0x18(%rbp)
jge    124d <PMIU_writeline+0x10d>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe49(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <PMIU_writeline+0x138>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1267 <PMIU_writeline+0x127>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    126c <PMIU_writeline+0x12c>
jmp    1271 <PMIU_writeline+0x131>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PMIU_writeline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1050 <strlen@plt>
cmp    $0x401,%eax
jl     117d <PMIU_writeline+0x2d>
movb   $0x0,0x3ff(%rbx)
lea    0xe85(%rip),%rdi
jmp    11db <PMIU_writeline+0x8b>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11d4 <PMIU_writeline+0x84>
mov    0x2e49(%rip),%r15
mov    0x2e3a(%rip),%r12
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    11bb <PMIU_writeline+0x6b>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     11a0 <PMIU_writeline+0x50>
test   %eax,%eax
js     11f4 <PMIU_writeline+0xa4>
xor    %r14d,%r14d
cmp    %ebp,%eax
jge    11e8 <PMIU_writeline+0x98>
lea    0xedb(%rip),%rdi
call   1030 <puts@plt>
jmp    11e8 <PMIU_writeline+0x98>
lea    0xe4f(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe68(%rip),%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11e8 <PMIU_writeline+0x98>

LLVM IR (-O0):
@.str = [42 x i8] c"write_line: message string too big: :%s:\0A\00"
@.str.1 = [57 x i8] c"write_line: message string doesn't end in newline: :%s:\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [34 x i8] c"write_line error; fd=%d buf=:%s:\0A\00"
@.str.3 = [35 x i8] c"system msg for write_line failure \00"
@.str.4 = [43 x i8] c"write_line failed to write entire message\0A\00"
define i32 @PMIU_writeline(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp sgt i32 %10, 1024
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 1023
store i8 0, i8* %14
%15 = load i8*, i8** %5
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %61
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %18, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 10
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = load i8*, i8** %5
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %60
30: ; preds = %17
br label %31
31: ; preds = %43, %30
%32 = load i32, i32* %4
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = call i32 @write(i32 %32, i8* %33, i32 %34)
store i32 %35, i32* %7
br label %36
36: ; preds = %31
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br label %43
43: ; preds = %39, %36
%44 = phi i1 [ false, %36 ], [ %42, %39 ]
br i1 %44, label %31, label %45
45: ; preds = %43
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = load i32, i32* %4
%50 = load i8*, i8** %5
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %49, i8* %50)
%52 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
53: ; preds = %45
%54 = load i32, i32* %7
%55 = load i32, i32* %6
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %57, %53
br label %60
60: ; preds = %59, %27
br label %61
61: ; preds = %60, %12
store i32 0, i32* %3
br label %62
62: ; preds = %61, %48
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [42 x i8] c"write_line: message string too big: :%s:\0A\00"
@.str.1 = [57 x i8] c"write_line: message string doesn't end in newline: :%s:\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [34 x i8] c"write_line error; fd=%d buf=:%s:\0A\00"
@.str.3 = [35 x i8] c"system msg for write_line failure \00"
@str = [42 x i8] c"write_line failed to write entire message\00"
define i32 @PMIU_writeline(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 1024
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %1, i64 1023
store i8 0, i8* %6
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %33
8: ; preds = %2
%9 = tail call i32 @strlen(i8* %1)
%10 = add nsw i32 %9, -1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 10
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %33
17: ; preds = %8, %17
%18 = tail call i32 @write(i32 %0, i8* %1, i32 %3)
%19 = icmp eq i32 %18, -1
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %17, label %24
24: ; preds = %17
%25 = icmp slt i32 %18, 0
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %0, i8* %1)
%28 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %33
29: ; preds = %24
%30 = icmp slt i32 %18, %3
br i1 %30, label %31, label %33
31: ; preds = %29
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %5, %29, %31, %15, %26
%34 = phi i32 [ -1, %26 ], [ 0, %15 ], [ 0, %31 ], [ 0, %29 ], [ 0, %5 ]
ret i32 %34
}