#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 */
char EOF;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
char getchar();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stdin;
int strcspn(char *, char *);
int strlen(char *);
ssize_t pgets(char *str, ssize_t siz) {
  char buf_ch;
  int ch_i;
  memset(str, 0, siz);
  fgets(str, siz, stdin);
  if (strlen(str) == strcspn(str, "\r\n"))
    while ((buf_ch = getchar()) != '\n' && buf_ch != EOF)
      ;
  ch_i = strcspn(str, "\r\n");
  str[ch_i] = 0;
  return (ch_i);
}


x86 assembly (clang, -O0):
<pgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1050 <strcspn@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jne    11fb <pgets+0xab>
jmp    11b0 <pgets+0x60>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    %al,-0xd(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x19(%rbp)
je     11e1 <pgets+0x91>
movsbl -0xd(%rbp),%eax
mov    0x2e0a(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11f1 <pgets+0xa1>
jmp    11f6 <pgets+0xa6>
jmp    11b0 <pgets+0x60>
jmp    11fb <pgets+0xab>
mov    -0x8(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1050 <strcspn@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgets>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
lea    0xe7b(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcspn@plt>
cmp    %eax,%ebp
jne    11b0 <pgets+0x60>
mov    0x2e48(%rip),%rbp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <getchar@plt>
cmp    $0xa,%al
je     11b0 <pgets+0x60>
cmp    0x0(%rbp),%al
jne    11a0 <pgets+0x50>
lea    0xe49(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcspn@plt>
cltq
movb   $0x0,(%rbx,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [3 x i8] c"\0D\0A\00"
@EOF = global i8 0
define i32 @pgets(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* %4
%9 = call i32 @memset(i8* %7, i32 0, i32 %8)
%10 = load i8*, i8** %3
%11 = load i32, i32* %4
%12 = load i32, i32* @stdin
%13 = call i32 @fgets(i8* %10, i32 %11, i32 %12)
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = load i8*, i8** %3
%17 = call i32 @strcspn(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i32 %15, %17
br i1 %18, label %19, label %34
19: ; preds = %2
br label %20
20: ; preds = %32, %19
%21 = call signext i8 (...) @getchar()
store i8 %21, i8* %5
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i8, i8* %5
%26 = sext i8 %25 to i32
%27 = load i8, i8* @EOF
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %26, %28
br label %30
30: ; preds = %24, %20
%31 = phi i1 [ false, %20 ], [ %29, %24 ]
br i1 %31, label %32, label %33
32: ; preds = %30
br label %20
33: ; preds = %30
br label %34
34: ; preds = %33, %2
%35 = load i8*, i8** %3
%36 = call i32 @strcspn(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %36, i32* %6
%37 = load i8*, i8** %3
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %6
ret i32 %41
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [3 x i8] c"\0D\0A\00"
@EOF = global i8 0
define i32 @pgets(i8* %0, i32 %1) {
%3 = tail call i32 @memset(i8* %0, i32 0, i32 %1)
%4 = load i32, i32* @stdin
%5 = tail call i32 @fgets(i8* %0, i32 %1, i32 %4)
%6 = tail call i32 @strlen(i8* %0)
%7 = tail call i32 @strcspn(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %15
9: ; preds = %2, %9
%10 = tail call signext i8 (...) @getchar()
%11 = icmp ne i8 %10, 10
%12 = load i8, i8* @EOF
%13 = icmp ne i8 %10, %12
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %9, label %15
15: ; preds = %9, %2
%16 = tail call i32 @strcspn(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
store i8 0, i8* %18
ret i32 %16
}