#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 */
char *fgets(char *, size_t, int /*<<< orphan*/ *);
int strlen(char *);
char *stb_fgets(char *buffer, size_t buflen, FILE *f) {
  char *p;
  buffer[0] = 0;
  p = fgets(buffer, buflen, f);
  if (p) {
    size_t n = strlen(p) - 1;
    if (n >= 0)
      if (p[n] == '\n')
        p[n] = 0;
  }
  return p;
}


x86 assembly (clang, -O0):
<stb_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a3 <stb_fgets+0x83>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jb     119e <stb_fgets+0x7e>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1199 <stb_fgets+0x79>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    119e <stb_fgets+0x7e>
jmp    11a3 <stb_fgets+0x83>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stb_fgets>:
push   %rbx
movb   $0x0,(%rdi)
call   1040 <fgets@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1147 <stb_fgets+0x27>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1147 <stb_fgets+0x27>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @stb_fgets(i8* %0, i64 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%9 = load i8*, i8** %4
%10 = getelementptr inbounds i8, i8* %9, i64 0
store i8 0, i8* %10
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = load i32*, i32** %6
%14 = call i8* @fgets(i8* %11, i64 %12, i32* %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %37
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = icmp uge i64 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %17
%25 = load i8*, i8** %7
%26 = load i64, i64* %8
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i8*, i8** %7
%33 = load i64, i64* %8
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %31, %24
br label %36
36: ; preds = %35, %17
br label %37
37: ; preds = %36, %3
%38 = load i8*, i8** %7
ret i8* %38
}

LLVM IR (-O2):
define i8* @stb_fgets(i8* %0, i64 %1, i32* nocapture %2) {
store i8 0, i8* %0
%4 = tail call i8* @fgets(i8* %0, i64 %1, i32* %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %4)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %4, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %14
13: ; preds = %6
store i8 0, i8* %10
br label %14
14: ; preds = %13, %6, %3
ret i8* %4
}