#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 EOF;
int fgetc(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int strlen(char *);
char *fgets_truncate_buffer(char *buffer, int size, FILE *stream, int *res) {
  char *s;
  int a, i, len;
  s = fgets(buffer, size, stream);
  *res = 0;
  if (s == NULL || s[0] == '\0')
    return s;
  i = 0;
  len = strlen(s) - 1;
  if (s[len] != '\n') {
    while ((a = fgetc(stream)) != EOF) {
      i++;
      if (a == '\n')
        break;
    }
  } else
    s[len] = '\0';
  *res = i;
  return s;
}


x86 assembly (clang, -O0):
<fgets_truncate_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x30(%rbp)
je     1180 <fgets_truncate_buffer+0x50>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    118d <fgets_truncate_buffer+0x5d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1217 <fgets_truncate_buffer+0xe7>
movl   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11fa <fgets_truncate_buffer+0xca>
jmp    11bd <fgets_truncate_buffer+0x8d>
mov    -0x20(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x34(%rbp)
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%eax
je     11f5 <fgets_truncate_buffer+0xc5>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0xa,-0x34(%rbp)
jne    11f0 <fgets_truncate_buffer+0xc0>
jmp    11f5 <fgets_truncate_buffer+0xc5>
jmp    11bd <fgets_truncate_buffer+0x8d>
jmp    1206 <fgets_truncate_buffer+0xd6>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets_truncate_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rbx
call   1050 <fgets@plt>
mov    %rax,%r15
movl   $0x0,(%r14)
test   %rax,%rax
je     1199 <fgets_truncate_buffer+0x69>
cmpb   $0x0,(%r15)
je     1199 <fgets_truncate_buffer+0x69>
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r15,1)
jne    1174 <fgets_truncate_buffer+0x44>
movb   $0x0,-0x1(%r15,%rax,1)
xor    %ebp,%ebp
jmp    1196 <fgets_truncate_buffer+0x66>
xor    %ebp,%ebp
mov    0x2e6b(%rip),%r12
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    (%r12),%eax
je     1196 <fgets_truncate_buffer+0x66>
add    $0x1,%ebp
cmp    $0xa,%eax
jne    1180 <fgets_truncate_buffer+0x50>
mov    %ebp,(%r14)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define i8* @fgets_truncate_buffer(i8* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = load i32*, i32** %8
%17 = call i8* @fgets(i8* %14, i32 %15, i32* %16)
store i8* %17, i8** %10
%18 = load i32*, i32** %9
store i32 0, i32* %18
%19 = load i8*, i8** %10
%20 = icmp eq i8* %19, null
br i1 %20, label %27, label %21
21: ; preds = %4
%22 = load i8*, i8** %10
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21, %4
%28 = load i8*, i8** %10
store i8* %28, i8** %5
br label %63
29: ; preds = %21
store i32 0, i32* %12
%30 = load i8*, i8** %10
%31 = call i32 @strlen(i8* %30)
%32 = sub nsw i32 %31, 1
store i32 %32, i32* %13
%33 = load i8*, i8** %10
%34 = load i32, i32* %13
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 10
br i1 %39, label %40, label %54
40: ; preds = %29
br label %41
41: ; preds = %52, %40
%42 = load i32*, i32** %8
%43 = call i32 @fgetc(i32* %42)
store i32 %43, i32* %11
%44 = load i32, i32* @EOF
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %53
46: ; preds = %41
%47 = load i32, i32* %12
%48 = add nsw i32 %47, 1
store i32 %48, i32* %12
%49 = load i32, i32* %11
%50 = icmp eq i32 %49, 10
br i1 %50, label %51, label %52
51: ; preds = %46
br label %53
52: ; preds = %46
br label %41
53: ; preds = %51, %41
br label %59
54: ; preds = %29
%55 = load i8*, i8** %10
%56 = load i32, i32* %13
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
br label %59
59: ; preds = %54, %53
%60 = load i32, i32* %12
%61 = load i32*, i32** %9
store i32 %60, i32* %61
%62 = load i8*, i8** %10
store i8* %62, i8** %5
br label %63
63: ; preds = %59, %27
%64 = load i8*, i8** %5
ret i8* %64
}

LLVM IR (-O2):
@EOF = global i32 0
define i8* @fgets_truncate_buffer(i8* %0, i32 %1, i32* nocapture %2, i32* nocapture writeonly %3) {
%5 = tail call i8* @fgets(i8* %0, i32 %1, i32* %2)
store i32 0, i32* %3
%6 = icmp eq i8* %5, null
br i1 %6, label %28, label %7
7: ; preds = %4
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 0
br i1 %9, label %28, label %10
10: ; preds = %7
%11 = tail call i32 @strlen(i8* %5)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %5, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %25, label %17
17: ; preds = %10, %22
%18 = phi i32 [ %23, %22 ], [ 0, %10 ]
%19 = tail call i32 @fgetc(i32* %2)
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %19, %20
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = add nuw nsw i32 %18, 1
%24 = icmp eq i32 %19, 10
br i1 %24, label %26, label %17
25: ; preds = %10
store i8 0, i8* %14
br label %26
26: ; preds = %17, %22, %25
%27 = phi i32 [ 0, %25 ], [ %18, %17 ], [ %23, %22 ]
store i32 %27, i32* %3
br label %28
28: ; preds = %4, %7, %26
ret i8* %5
}