#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 *fgetstr(char *str, int n, FILE *fptr) {
  int str_length;
  if (fgets(str, n, fptr) == NULL)
    return (NULL);
  str_length = strlen(str);
  if (str_length > 0) {
    if (str[str_length - 1] == '\n') {
      str[str_length - 1] = '\0';
    }
  }
  return (str);
}


x86 assembly (clang, -O0):
<fgetstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    115a <fgetstr+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    11a8 <fgetstr+0x88>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11a0 <fgetstr+0x80>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    119b <fgetstr+0x7b>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a0 <fgetstr+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetstr>:
push   %rbx
mov    %rdi,%rbx
call   1040 <fgets@plt>
test   %rax,%rax
je     1149 <fgetstr+0x29>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    114b <fgetstr+0x2b>
add    $0xffffffff,%eax
cmpb   $0xa,(%rbx,%rax,1)
jne    114b <fgetstr+0x2b>
movb   $0x0,(%rbx,%rax,1)
jmp    114b <fgetstr+0x2b>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @fgetstr(i8* %0, i32 %1, i32* nocapture %2) {
%4 = tail call i32* @fgets(i8* %0, i32 %1, i32* %2)
%5 = icmp eq i32* %4, null
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%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* %0, 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
%17 = phi i8* [ null, %3 ], [ %0, %9 ], [ %0, %15 ], [ %0, %6 ]
ret i8* %17
}