#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 *, int, int /*<<< orphan*/ *);
int strlen(char *);
char *stb_fgets(char *buffer, int buflen, FILE *f) {
  char *p;
  buffer[0] = 0;
  p = fgets(buffer, buflen, f);
  if (p) {
    int 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    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119d <stb_fgets+0x7d>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1198 <stb_fgets+0x78>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1193 <stb_fgets+0x73>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1198 <stb_fgets+0x78>
jmp    119d <stb_fgets+0x7d>
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     114a <stb_fgets+0x2a>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    114a <stb_fgets+0x2a>
add    $0xffffffff,%eax
cmpb   $0xa,(%rbx,%rax,1)
jne    114a <stb_fgets+0x2a>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @stb_fgets(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %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 i32, i32* %5
%13 = load i32*, i32** %6
%14 = call i8* @fgets(i8* %11, i32 %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 %38
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 1
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %17
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%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 eq i32 %29, 10
br i1 %30, label %31, label %36
31: ; preds = %23
%32 = load i8*, i8** %7
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %31, %23
br label %37
37: ; preds = %36, %17
br label %38
38: ; preds = %37, %3
%39 = load i8*, i8** %7
ret i8* %39
}

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