#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 *fgets(char *, int, int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stdin;
char *s_gets(char *st, int n) {
  char *ret_val;
  int i = 0;
  int ch;
  ret_val = fgets(st, n, stdin);
  if (ret_val) {
    while (st[i] != '\n' && st[i] != '\0') {
      i++;
    }
    if (st[i] == '\n') {
      st[i] = '\0';
    } else {
      while ((ch = getchar()) != '\n') {
        printf("正在丢弃多余的字符串%c\n", ch);
        continue;
      }
    }
  }
  return ret_val;
}


x86 assembly (clang, -O0):
<s_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e94(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1218 <s_gets+0xe8>
jmp    116f <s_gets+0x3f>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x21(%rbp)
je     119e <s_gets+0x6e>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11ae <s_gets+0x7e>
jmp    11bc <s_gets+0x8c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116f <s_gets+0x3f>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11e2 <s_gets+0xb2>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1213 <s_gets+0xe3>
jmp    11e7 <s_gets+0xb7>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xa,%eax
je     120e <s_gets+0xde>
mov    -0x20(%rbp),%esi
lea    0xdfc(%rip),%rdi
call   1030 <printf@plt>
jmp    11e7 <s_gets+0xb7>
jmp    1213 <s_gets+0xe3>
jmp    1218 <s_gets+0xe8>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s_gets>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eaa(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,%r14
test   %rax,%rax
je     119e <s_gets+0x6e>
xor    %eax,%eax
nop
movzbl (%rbx,%rax,1),%ecx
cmp    $0xa,%cl
je     1198 <s_gets+0x68>
test   %cl,%cl
je     1163 <s_gets+0x33>
add    $0x1,%rax
jmp    1150 <s_gets+0x20>
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     119e <s_gets+0x6e>
lea    0xe8a(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
jne    1180 <s_gets+0x50>
jmp    119e <s_gets+0x6e>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [34 x i8] c"\E6\AD\A3\E5\9C\A8\E4\B8\A2\E5\BC\83\E5\A4\9A\E4\BD\99\E7\9A\84\E5\AD\97\E7\AC\A6\E4\B8\B2%c\0A\00"
define i8* @s_gets(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* %6
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = load i32, i32* @stdin
%11 = call i8* @fgets(i8* %8, i32 %9, i32 %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %58
14: ; preds = %2
br label %15
15: ; preds = %33, %14
%16 = load i8*, i8** %3
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 10
br i1 %22, label %23, label %31
23: ; preds = %15
%24 = load i8*, i8** %3
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br label %31
31: ; preds = %23, %15
%32 = phi i1 [ false, %15 ], [ %30, %23 ]
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %15
36: ; preds = %31
%37 = load i8*, i8** %3
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 10
br i1 %43, label %44, label %49
44: ; preds = %36
%45 = load i8*, i8** %3
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
br label %57
49: ; preds = %36
br label %50
50: ; preds = %53, %49
%51 = call i32 (...) @getchar()
store i32 %51, i32* %7
%52 = icmp ne i32 %51, 10
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* %7
%55 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %54)
br label %50
56: ; preds = %50
br label %57
57: ; preds = %56, %44
br label %58
58: ; preds = %57, %2
%59 = load i8*, i8** %5
ret i8* %59
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [34 x i8] c"\E6\AD\A3\E5\9C\A8\E4\B8\A2\E5\BC\83\E5\A4\9A\E4\BD\99\E7\9A\84\E5\AD\97\E7\AC\A6\E4\B8\B2%c\0A\00"
define i8* @s_gets(i8* %0, i32 %1) {
%3 = load i32, i32* @stdin
%4 = tail call i8* @fgets(i8* %0, i32 %1, i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %23, label %6
6: ; preds = %2, %13
%7 = phi i64 [ %14, %13 ], [ 0, %2 ]
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
switch i8 %9, label %13 [
i8 10, label %15
i8 0, label %10
]
10: ; preds = %6
%11 = tail call i32 (...) @getchar()
%12 = icmp eq i32 %11, 10
br i1 %12, label %23, label %18
13: ; preds = %6
%14 = add nuw i64 %7, 1
br label %6
15: ; preds = %6
%16 = and i64 %7, 4294967295
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
br label %23
18: ; preds = %10, %18
%19 = phi i32 [ %21, %18 ], [ %11, %10 ]
%20 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = tail call i32 (...) @getchar()
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %18
23: ; preds = %18, %10, %15, %2
ret i8* %4
}