#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ isspace(char);
int /*<<< orphan*/ memmove(char *, char *, scalar_t__);
char *strchr(char *, char);
scalar_t__ strlen(char *);
char *readline(FILE *fp) {
  static char buf[1024];
  char *cp;
  if (fgets(buf, 1024 - 1, fp) == NULL)
    return (NULL);
  buf[1024 - 1] = '\0';
  if ((cp = strchr(buf, '\n')) != NULL)
    *cp = '\0';
  for (cp = buf; *cp && isspace(*cp); cp++)
    ;
  if (cp != buf && *cp)
    memmove(buf, cp, strlen(cp) + 1);
  return (buf);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdx
lea    0x2ef9(%rip),%rdi
mov    $0x3ff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    1188 <readline+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1265 <readline+0x115>
movb   $0x0,0x32d0(%rip)
lea    0x2eca(%rip),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b5 <readline+0x65>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2ea4(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     11eb <readline+0x9b>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11fb <readline+0xab>
jmp    1211 <readline+0xc1>
jmp    1200 <readline+0xb0>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11c0 <readline+0x70>
lea    0x2e48(%rip),%rax
cmp    %rax,-0x18(%rbp)
je     125a <readline+0x10a>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     125a <readline+0x10a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
lea    0x2e0b(%rip),%rdi
call   1070 <memmove@plt>
lea    0x2dff(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rdx
lea    0x2f02(%rip),%rdi
mov    $0x3ff,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11fa <readline+0xaa>
movb   $0x0,0x32e7(%rip)
lea    0x2ee1(%rip),%r14
mov    %r14,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1194 <readline+0x44>
movb   $0x0,(%rax)
mov    0x2ec6(%rip),%al
test   %al,%al
je     11fd <readline+0xad>
lea    0x2ebb(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movsbl %al,%edi
call   1050 <isspace@plt>
test   %rax,%rax
je     11c9 <readline+0x79>
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11b0 <readline+0x60>
lea    0x2e90(%rip),%r14
cmp    %r14,%rbx
je     11fd <readline+0xad>
cmpb   $0x0,(%rbx)
je     11fd <readline+0xad>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
lea    0x2e73(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <memmove@plt>
jmp    11fd <readline+0xad>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@readline.buf = internal global [1024 x i8] zeroinitializer
define i8* @readline(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = call i32* @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), i32 1023, i32* %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %46
9: ; preds = %1
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 1023)
%10 = call i8* @strchr(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), i8 signext 10)
store i8* %10, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i8*, i8** %4
store i8 0, i8* %13
br label %14
14: ; preds = %12, %9
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), i8** %4
br label %15
15: ; preds = %28, %14
%16 = load i8*, i8** %4
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = call i64 @isspace(i8 signext %22)
%24 = icmp ne i64 %23, 0
br label %25
25: ; preds = %20, %15
%26 = phi i1 [ false, %15 ], [ %24, %20 ]
br i1 %26, label %27, label %31
27: ; preds = %25
br label %28
28: ; preds = %27
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %4
br label %15
31: ; preds = %25
%32 = load i8*, i8** %4
%33 = icmp ne i8* %32, getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0)
br i1 %33, label %34, label %45
34: ; preds = %31
%35 = load i8*, i8** %4
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %34
%40 = load i8*, i8** %4
%41 = load i8*, i8** %4
%42 = call i64 @strlen(i8* %41)
%43 = add nsw i64 %42, 1
%44 = call i32 @memmove(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), i8* %40, i64 %43)
br label %45
45: ; preds = %39, %34, %31
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), i8** %2
br label %46
46: ; preds = %45, %8
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@readline.buf = internal global [1024 x i8] zeroinitializer
define i8* @readline(i32* nocapture %0) {
%2 = tail call i32* @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), i32 1023, i32* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %30, label %4
4: ; preds = %1
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 1023)
%5 = tail call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), i8 signext 10)
%6 = icmp eq i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %4
store i8 0, i8* %5
br label %8
8: ; preds = %7, %4
%9 = load i8, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0)
%10 = icmp eq i8 %9, 0
br i1 %10, label %30, label %11
11: ; preds = %8, %16
%12 = phi i8 [ %18, %16 ], [ %9, %8 ]
%13 = phi i8* [ %17, %16 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), %8 ]
%14 = tail call i64 @isspace(i8 signext %12)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = getelementptr inbounds i8, i8* %13, i64 1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %11
20: ; preds = %11, %16
%21 = phi i8* [ %13, %11 ], [ %17, %16 ]
%22 = icmp eq i8* %21, getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0)
br i1 %22, label %30, label %23
23: ; preds = %20
%24 = load i8, i8* %21
%25 = icmp eq i8 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = tail call i64 @strlen(i8* %21)
%28 = add nsw i64 %27, 1
%29 = tail call i32 @memmove(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), i8* %21, i64 %28)
br label %30
30: ; preds = %8, %20, %23, %26, %1
%31 = phi i8* [ null, %1 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), %26 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), %23 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), %20 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @readline.buf, i64 0, i64 0), %8 ]
ret i8* %31
}