#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 */
char getc();
int /*<<< orphan*/ putc(char);
int rgets(char buf[]) {
  int i = 0;
  char c = getc();
  while (c != '\r') {
    buf[i] = c;
    putc(c);
    c = getc();
    i++;
  }
  buf[i] = 0;
}


x86 assembly (clang, -O0):
<rgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getc@plt>
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
je     1179 <rgets+0x59>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x15(%rbp),%edi
call   1030 <putc@plt>
mov    $0x0,%al
call   1040 <getc@plt>
mov    %al,-0x15(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113d <rgets+0x1d>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rgets>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getc@plt>
cmp    $0xd,%al
je     115d <rgets+0x3d>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %al,(%r14,%rbx,1)
movsbl %al,%edi
call   1030 <putc@plt>
xor    %eax,%eax
call   1040 <getc@plt>
add    $0x1,%rbx
cmp    $0xd,%al
jne    1140 <rgets+0x20>
mov    %ebx,%ebx
movb   $0x0,(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @rgets(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %3
store i32 0, i32* %4
%6 = call signext i8 (...) @getc()
store i8 %6, i8* %5
br label %7
7: ; preds = %11, %1
%8 = load i8, i8* %5
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 13
br i1 %10, label %11, label %22
11: ; preds = %7
%12 = load i8, i8* %5
%13 = load i8*, i8** %3
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 %12, i8* %16
%17 = load i8, i8* %5
%18 = call i32 @putc(i8 signext %17)
%19 = call signext i8 (...) @getc()
store i8 %19, i8* %5
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
br label %7
22: ; preds = %7
%23 = load i8*, i8** %3
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
define i32 @rgets(i8* nocapture writeonly %0) {
%2 = tail call signext i8 (...) @getc()
%3 = icmp eq i8 %2, 13
br i1 %3, label %14, label %4
4: ; preds = %1, %4
%5 = phi i64 [ %10, %4 ], [ 0, %1 ]
%6 = phi i8 [ %9, %4 ], [ %2, %1 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 %6, i8* %7
%8 = tail call i32 @putc(i8 signext %6)
%9 = tail call signext i8 (...) @getc()
%10 = add nuw i64 %5, 1
%11 = icmp eq i8 %9, 13
br i1 %11, label %12, label %4
12: ; preds = %4
%13 = and i64 %10, 4294967295
br label %14
14: ; preds = %12, %1
%15 = phi i64 [ 0, %1 ], [ %13, %12 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %16
ret i32 undef
}