#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*/ *);
int strlen(char *);
char *m_fgets(char *buffer, int size, FILE *fd) {
  int len;
  buffer[0] = '\0';
  fgets(buffer, size, fd);
  if ((len = strlen(buffer)) == 0)
    return NULL;
  if (buffer[len - 1] == '\n')
    buffer[len - 1] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<m_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jne    116c <m_fgets+0x4c>
movq   $0x0,-0x8(%rbp)
jmp    119f <m_fgets+0x7f>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1197 <m_fgets+0x77>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_fgets>:
push   %rbx
mov    %rdi,%rbx
movb   $0x0,(%rdi)
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     1148 <m_fgets+0x28>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    114a <m_fgets+0x2a>
movb   $0x0,-0x1(%rbx,%rax,1)
jmp    114a <m_fgets+0x2a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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

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