#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*/ STDIN_FILENO;
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int read(int /*<<< orphan*/, char *, size_t);
int strlen(char const *);
int /*<<< orphan*/ warn(char *);
ssize_t getinput(char *buf, size_t len, const char *eol) {
  ssize_t count;
  memset(buf, 0, len);
  count = read(STDIN_FILENO, buf, len);
  if (count == -1) {
    warn("read failed");
    return -1;
  }
  memcpy(buf + count - 1, eol, strlen(eol));
  return (count - 1 + strlen(eol));
}


x86 assembly (clang, -O0):
<getinput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ae <getinput+0x5e>
lea    0xe63(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <getinput+0xad>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getinput>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %esi,%esi
mov    %r15,%rdx
call   1050 <memset@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     11b2 <getinput+0x62>
mov    %eax,%r15d
cltq
add    %rax,%rbx
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %r15d,%eax
add    $0xffffffff,%eax
jmp    11c3 <getinput+0x73>
lea    0xe47(%rip),%rdi
call   1030 <warn@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [12 x i8] c"read failed\00"
define i32 @getinput(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = call i32 @memset(i8* %9, i32 0, i64 %10)
%12 = load i32, i32* @STDIN_FILENO
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i32 @read(i32 %12, i8* %13, i64 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %35
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = getelementptr inbounds i8, i8* %24, i64 -1
%26 = load i8*, i8** %7
%27 = load i8*, i8** %7
%28 = call i32 @strlen(i8* %27)
%29 = call i32 @memcpy(i8* %25, i8* %26, i32 %28)
%30 = load i32, i32* %8
%31 = sub nsw i32 %30, 1
%32 = load i8*, i8** %7
%33 = call i32 @strlen(i8* %32)
%34 = add nsw i32 %31, %33
store i32 %34, i32* %4
br label %35
35: ; preds = %20, %18
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [12 x i8] c"read failed\00"
define i32 @getinput(i8* %0, i64 %1, i8* %2) {
%4 = tail call i32 @memset(i8* %0, i32 0, i64 %1)
%5 = load i32, i32* @STDIN_FILENO
%6 = tail call i32 @read(i32 %5, i8* %0, i64 %1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %19
10: ; preds = %3
%11 = sext i32 %6 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = getelementptr inbounds i8, i8* %12, i64 -1
%14 = tail call i32 @strlen(i8* %2)
%15 = tail call i32 @memcpy(i8* %13, i8* %2, i32 %14)
%16 = add nsw i32 %6, -1
%17 = tail call i32 @strlen(i8* %2)
%18 = add nsw i32 %16, %17
br label %19
19: ; preds = %10, %8
%20 = phi i32 [ -1, %8 ], [ %18, %10 ]
ret i32 %20
}