#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
int mb_gets(char *buf, int s) {
  int result = 0;
  if (buf && s) {
    if (fgets(buf, s, stdin) == 0) {
      fprintf(stderr, "Error reading.\n");
      exit(1);
    }
    result = (int)strlen(buf);
    if (buf[result - 1] == '\n') {
      buf[result - 1] = '\0';
      result--;
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<mb_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ee <mb_gets+0xae>
cmpl   $0x0,-0xc(%rbp)
je     11ee <mb_gets+0xae>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11a9 <mb_gets+0x69>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11e9 <mb_gets+0xa9>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11ee <mb_gets+0xae>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mb_gets>:
push   %rbx
xor    %eax,%eax
test   %rdi,%rdi
je     117c <mb_gets+0x3c>
test   %esi,%esi
je     117c <mb_gets+0x3c>
mov    %rdi,%rbx
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
test   %rax,%rax
je     117e <mb_gets+0x3e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    -0x1(%rax),%ecx
movslq %ecx,%rdx
cmpb   $0xa,(%rbx,%rdx,1)
jne    117c <mb_gets+0x3c>
movb   $0x0,(%rbx,%rdx,1)
mov    %ecx,%eax
pop    %rbx
ret
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"Error reading.\0A\00"
define i32 @mb_gets(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %42
8: ; preds = %2
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %42
11: ; preds = %8
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = load i32, i32* @stdin
%15 = call i64 @fgets(i8* %12, i32 %13, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i8*, i8** %3
%23 = call i64 @strlen(i8* %22)
%24 = trunc i64 %23 to i32
store i32 %24, i32* %5
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %41
33: ; preds = %21
%34 = load i8*, i8** %3
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %5
%40 = add nsw i32 %39, -1
store i32 %40, i32* %5
br label %41
41: ; preds = %33, %21
br label %42
42: ; preds = %41, %8, %2
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@stdin = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"Error reading.\0A\00"
define i32 @mb_gets(i8* %0, i32 %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i32 %1, 0
%5 = and i1 %3, %4
br i1 %5, label %6, label %25
6: ; preds = %2
%7 = load i32, i32* @stdin
%8 = tail call i64 @fgets(i8* %0, i32 %1, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %6
%15 = tail call i64 @strlen(i8* %0)
%16 = trunc i64 %15 to i32
%17 = shl i64 %15, 32
%18 = add i64 %17, -4294967296
%19 = ashr exact i64 %18, 32
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 10
br i1 %22, label %23, label %25
23: ; preds = %14
store i8 0, i8* %20
%24 = add nsw i32 %16, -1
br label %25
25: ; preds = %14, %23, %2
%26 = phi i32 [ %24, %23 ], [ %16, %14 ], [ 0, %2 ]
ret i32 %26
}