#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 getch();
int /*<<< orphan*/ putch(int);
void cgets(char *s, int bufsize) {
  char *p;
  int c;
  int i;
  for (i = 0; i < bufsize; i++) {
    *(s + i) = 0;
  }
  p = s;
  for (p = s; p < s + bufsize - 1;) {
    c = getch();
    switch (c) {
    case '\r':
    case '\n':
      putch('\r');
      putch('\n');
      *p = '\n';
      return;
    case '\b':
      if (p > s) {
        *p-- = 0;
        putch('\b');
        putch(' ');
        putch('\b');
      }
      break;
    default:
      putch(c);
      *p++ = c;
      break;
    }
  }
  return;
}


x86 assembly (clang, -O0):
<cgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    115c <cgets+0x3c>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1136 <cgets+0x16>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0xc(%rbp),%rdx
add    %rdx,%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jae    124e <cgets+0x12e>
xor    %eax,%eax
call   1030 <getch@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x8,%eax
je     11e8 <cgets+0xc8>
jmp    11a6 <cgets+0x86>
mov    -0x24(%rbp),%eax
sub    $0xa,%eax
je     11c8 <cgets+0xa8>
jmp    11b7 <cgets+0x97>
mov    -0x24(%rbp),%eax
sub    $0xd,%eax
jne    122b <cgets+0x10b>
jmp    11c8 <cgets+0xa8>
mov    $0xd,%edi
call   1040 <putch@plt>
mov    $0xa,%edi
call   1040 <putch@plt>
mov    -0x18(%rbp),%rax
movb   $0xa,(%rax)
jmp    1253 <cgets+0x133>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    1226 <cgets+0x106>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
mov    $0x8,%edi
call   1040 <putch@plt>
mov    $0x20,%edi
call   1040 <putch@plt>
mov    $0x8,%edi
call   1040 <putch@plt>
jmp    1249 <cgets+0x129>
mov    -0x1c(%rbp),%edi
call   1040 <putch@plt>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    116c <cgets+0x4c>
jmp    1253 <cgets+0x133>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
test   %esi,%esi
jle    114c <cgets+0x1c>
mov    %ebp,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
movslq %ebp,%rax
lea    (%r14,%rax,1),%r15
add    $0xffffffffffffffff,%r15
cmp    %r14,%r15
jbe    11e3 <cgets+0xb3>
mov    %r14,%rbx
jmp    1175 <cgets+0x45>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %r15,%rbx
jae    11e3 <cgets+0xb3>
xor    %eax,%eax
call   1040 <getch@plt>
cmp    $0x8,%eax
je     11a0 <cgets+0x70>
mov    %eax,%ebp
cmp    $0xd,%eax
je     11cc <cgets+0x9c>
cmp    $0xa,%ebp
je     11cc <cgets+0x9c>
mov    %ebp,%edi
call   1050 <putch@plt>
mov    %bpl,(%rbx)
add    $0x1,%rbx
jmp    1170 <cgets+0x40>
nopl   (%rax)
cmp    %r14,%rbx
jbe    1170 <cgets+0x40>
movb   $0x0,(%rbx)
add    $0xffffffffffffffff,%rbx
mov    $0x8,%edi
call   1050 <putch@plt>
mov    $0x20,%edi
call   1050 <putch@plt>
mov    $0x8,%edi
call   1050 <putch@plt>
jmp    1170 <cgets+0x40>
mov    $0xd,%edi
call   1050 <putch@plt>
mov    $0xa,%edi
call   1050 <putch@plt>
movb   $0xa,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @cgets(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %7
br label %8
8: ; preds = %17, %2
%9 = load i32, i32* %7
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %12
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
store i32 %19, i32* %7
br label %8
20: ; preds = %8
%21 = load i8*, i8** %3
store i8* %21, i8** %5
%22 = load i8*, i8** %3
store i8* %22, i8** %5
br label %23
23: ; preds = %56, %20
%24 = load i8*, i8** %5
%25 = load i8*, i8** %3
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -1
%30 = icmp ult i8* %24, %29
br i1 %30, label %31, label %57
31: ; preds = %23
%32 = call i32 (...) @getch()
store i32 %32, i32* %6
%33 = load i32, i32* %6
switch i32 %33, label %49 [
i32 13, label %34
i32 10, label %34
i32 8, label %38
]
34: ; preds = %31, %31
%35 = call i32 @putch(i32 13)
%36 = call i32 @putch(i32 10)
%37 = load i8*, i8** %5
store i8 10, i8* %37
br label %58
38: ; preds = %31
%39 = load i8*, i8** %5
%40 = load i8*, i8** %3
%41 = icmp ugt i8* %39, %40
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i32 -1
store i8* %44, i8** %5
store i8 0, i8* %43
%45 = call i32 @putch(i32 8)
%46 = call i32 @putch(i32 32)
%47 = call i32 @putch(i32 8)
br label %48
48: ; preds = %42, %38
br label %56
49: ; preds = %31
%50 = load i32, i32* %6
%51 = call i32 @putch(i32 %50)
%52 = load i32, i32* %6
%53 = trunc i32 %52 to i8
%54 = load i8*, i8** %5
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %5
store i8 %53, i8* %54
br label %56
56: ; preds = %49, %48
br label %23
57: ; preds = %23
br label %58
58: ; preds = %57, %34
ret void
}

LLVM IR (-O2):
define void @cgets(i8* writeonly %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %2
%7 = sext i32 %1 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 -1
%10 = icmp ugt i8* %9, %0
br i1 %10, label %11, label %31
11: ; preds = %6, %28
%12 = phi i8* [ %29, %28 ], [ %0, %6 ]
%13 = tail call i32 (...) @getch()
switch i32 %13, label %24 [
i32 13, label %14
i32 10, label %14
i32 8, label %17
]
14: ; preds = %11, %11
%15 = tail call i32 @putch(i32 13)
%16 = tail call i32 @putch(i32 10)
store i8 10, i8* %12
br label %31
17: ; preds = %11
%18 = icmp ugt i8* %12, %0
br i1 %18, label %19, label %28
19: ; preds = %17
%20 = getelementptr inbounds i8, i8* %12, i64 -1
store i8 0, i8* %12
%21 = tail call i32 @putch(i32 8)
%22 = tail call i32 @putch(i32 32)
%23 = tail call i32 @putch(i32 8)
br label %28
24: ; preds = %11
%25 = tail call i32 @putch(i32 %13)
%26 = trunc i32 %13 to i8
%27 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %26, i8* %12
br label %28
28: ; preds = %17, %19, %24
%29 = phi i8* [ %27, %24 ], [ %20, %19 ], [ %12, %17 ]
%30 = icmp ult i8* %29, %9
br i1 %30, label %11, label %31
31: ; preds = %28, %6, %14
ret void
}