#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 /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
char *gets(char *s) {
  char *p;
  p = s;
  for (;;) {
    if (!(read(0, p, 1))) {
      if (p == s)
        return 0;
      else
        break;
    } else if (*p == '\n')
      break;
    p++;
  }
  *p = 0;
  return s;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%eax
jne    115d <gets+0x4d>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1158 <gets+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1197 <gets+0x87>
jmp    1188 <gets+0x78>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1172 <gets+0x62>
jmp    1188 <gets+0x78>
jmp    1177 <gets+0x67>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1124 <gets+0x14>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
je     116a <gets+0x5a>
xor    %r15d,%r15d
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0xa,(%rbx)
je     1165 <gets+0x55>
add    $0x1,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
add    $0xffffffffffffffff,%r15
test   %eax,%eax
jne    1140 <gets+0x30>
test   %r15,%r15
je     116a <gets+0x5a>
movb   $0x0,(%rbx)
jmp    116d <gets+0x5d>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @gets(i8* %0) {
%2 = tail call i32 @read(i32 0, i8* %0, i32 1)
%3 = icmp eq i32 %2, 0
br i1 %3, label %16, label %6
4: ; preds = %10
%5 = icmp eq i8* %11, %0
br i1 %5, label %16, label %14
6: ; preds = %1, %10
%7 = phi i8* [ %11, %10 ], [ %0, %1 ]
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 10
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = tail call i32 @read(i32 0, i8* %11, i32 1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %4, label %6
14: ; preds = %6, %4
%15 = phi i8* [ %11, %4 ], [ %7, %6 ]
store i8 0, i8* %15
br label %16
16: ; preds = %1, %4, %14
%17 = phi i8* [ %0, %14 ], [ null, %4 ], [ null, %1 ]
ret i8* %17
}