#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 */
unsigned char *fgets(unsigned char *, int, int /*<<< orphan*/ *);
int strlen(unsigned char *);
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/ *);
unsigned char *fgets_unicode(unsigned char *line, int n, FILE *fp) {
  unsigned char *result = fgets(line, n, fp);
  int nbytes, ch;
  if (result != NULL) {
    nbytes = strlen(result);
    ch = result[nbytes - 1];
    if (ch & 0x80) {
      result[nbytes - 1] = 0;
      ungetc(ch, fp);
      if (nbytes == 1)
        result = NULL;
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<fgets_unicode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
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     11cc <fgets_unicode+0x9c>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11c7 <fgets_unicode+0x97>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <ungetc@plt>
cmpl   $0x1,-0x24(%rbp)
jne    11c2 <fgets_unicode+0x92>
movq   $0x0,-0x20(%rbp)
jmp    11c7 <fgets_unicode+0x97>
jmp    11cc <fgets_unicode+0x9c>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets_unicode>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
call   1040 <fgets@plt>
test   %rax,%rax
je     1176 <fgets_unicode+0x46>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <strlen@plt>
movslq %eax,%r15
movzbl -0x1(%r15,%rbx,1),%edi
test   $0x80,%dil
je     1178 <fgets_unicode+0x48>
movb   $0x0,-0x1(%rbx,%r15,1)
mov    %r14,%rsi
call   1050 <ungetc@plt>
xor    %eax,%eax
cmp    $0x1,%r15d
cmove  %rax,%rbx
jmp    1178 <fgets_unicode+0x48>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @fgets_unicode(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = load i32*, i32** %6
%13 = call i8* @fgets(i8* %10, i32 %11, i32* %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %43
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %8
%19 = load i8*, i8** %7
%20 = load i32, i32* %8
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = and i32 %26, 128
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %16
%30 = load i8*, i8** %7
%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
%35 = load i32, i32* %9
%36 = load i32*, i32** %6
%37 = call i32 @ungetc(i32 %35, i32* %36)
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %29
store i8* null, i8** %7
br label %41
41: ; preds = %40, %29
br label %42
42: ; preds = %41, %16
br label %43
43: ; preds = %42, %3
%44 = load i8*, i8** %7
ret i8* %44
}

LLVM IR (-O2):
define i8* @fgets_unicode(i8* %0, i32 %1, i32* nocapture %2) {
%4 = tail call i8* @fgets(i8* %0, i32 %1, i32* %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %19, 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 = zext i8 %11 to i32
%13 = and i32 %12, 128
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %6
store i8 0, i8* %10
%16 = tail call i32 @ungetc(i32 %12, i32* %2)
%17 = icmp eq i32 %7, 1
%18 = select i1 %17, i8* null, i8* %4
br label %19
19: ; preds = %15, %6, %3
%20 = phi i8* [ %4, %6 ], [ null, %3 ], [ %18, %15 ]
ret i8* %20
}