#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 read2(int /*<<< orphan*/, char *, int, int);
char *gets2(char *buf, int max) {
  int i, cc;
  char c;
  int ispwd = 1;
  for (i = 0; i + 1 < max;) {
    cc = read2(0, &c, 1, ispwd);
    if (cc < 1)
      break;
    buf[i++] = c;
    if (c == '\n' || c == '\r')
      break;
  }
  buf[i] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<gets2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jge    119d <gets2+0x8d>
mov    -0x1c(%rbp),%ecx
xor    %edi,%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read2@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    1161 <gets2+0x51>
jmp    119d <gets2+0x8d>
mov    -0x15(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
je     1193 <gets2+0x83>
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
jne    1198 <gets2+0x88>
jmp    119d <gets2+0x8d>
jmp    112d <gets2+0x1d>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets2>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
cmp    $0x2,%esi
mov    $0x1,%eax
cmovl  %eax,%r12d
mov    %rdi,%r14
add    $0xffffffff,%r12d
xor    %ebx,%ebx
lea    0x7(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %rbx,%r12
je     1175 <gets2+0x65>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1030 <read2@plt>
test   %eax,%eax
jle    1172 <gets2+0x62>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xd,%al
je     1172 <gets2+0x62>
cmp    $0xa,%al
jne    1140 <gets2+0x30>
mov    %ebx,%r12d
movslq %r12d,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @gets2(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 1, i32* %8
store i32 0, i32* %5
br label %9
9: ; preds = %35, %2
%10 = load i32, i32* %5
%11 = add nsw i32 %10, 1
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %36
14: ; preds = %9
%15 = load i32, i32* %8
%16 = call i32 @read2(i32 0, i8* %7, i32 1, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %14
br label %36
20: ; preds = %14
%21 = load i8, i8* %7
%22 = load i8*, i8** %3
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 %21, i8* %26
%27 = load i8, i8* %7
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %34, label %30
30: ; preds = %20
%31 = load i8, i8* %7
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 13
br i1 %33, label %34, label %35
34: ; preds = %30, %20
br label %36
35: ; preds = %30
br label %9
36: ; preds = %34, %19, %9
%37 = load i8*, i8** %3
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
define i8* @gets2(i8* returned writeonly %0, i32 %1) {
%3 = alloca i8
%4 = call i32 @llvm.smax.i32(i32 %1, i32 1)
%5 = add nsw i32 %4, -1
%6 = zext i32 %5 to i64
br label %7
7: ; preds = %14, %2
%8 = phi i64 [ %9, %14 ], [ 0, %2 ]
%9 = add nuw nsw i64 %8, 1
%10 = icmp eq i64 %8, %6
br i1 %10, label %21, label %11
11: ; preds = %7
%12 = call i32 @read2(i32 0, i8* %3, i32 1, i32 1)
%13 = icmp slt i32 %12, 1
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = load i8, i8* %3
%16 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %15, i8* %16
switch i8 %15, label %7 [
i8 13, label %17
i8 10, label %17
]
17: ; preds = %14, %14
%18 = trunc i64 %9 to i32
br label %21
19: ; preds = %11
%20 = trunc i64 %8 to i32
br label %21
21: ; preds = %7, %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ], [ %5, %7 ]
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
ret i8* %0
}