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


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jge    1193 <gets+0x83>
xor    %edi,%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    1157 <gets+0x47>
jmp    1193 <gets+0x83>
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     1189 <gets+0x79>
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
jne    118e <gets+0x7e>
jmp    1193 <gets+0x83>
jmp    1126 <gets+0x16>
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):
<gets>:
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     1170 <gets+0x60>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    116d <gets+0x5d>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xd,%al
je     116d <gets+0x5d>
cmp    $0xa,%al
jne    1140 <gets+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* @gets(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %8
8: ; preds = %33, %2
%9 = load i32, i32* %5
%10 = add nsw i32 %9, 1
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %34
13: ; preds = %8
%14 = call i32 @read(i32 0, i8* %7, i32 1)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %13
br label %34
18: ; preds = %13
%19 = load i8, i8* %7
%20 = load i8*, i8** %3
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
store i32 %22, i32* %5
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 %19, i8* %24
%25 = load i8, i8* %7
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br i1 %27, label %32, label %28
28: ; preds = %18
%29 = load i8, i8* %7
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 13
br i1 %31, label %32, label %33
32: ; preds = %28, %18
br label %34
33: ; preds = %28
br label %8
34: ; preds = %32, %17, %8
%35 = load i8*, i8** %3
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
define i8* @gets(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 @read(i32 0, i8* %3, 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
}