#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(const char *pmt, char *buf, int s) {
  int result = 0;
  ((void)(pmt));
  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    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f2 <mb_gets+0xb2>
cmpl   $0x0,-0x14(%rbp)
je     11f2 <mb_gets+0xb2>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11ad <mb_gets+0x6d>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11ed <mb_gets+0xad>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    11f2 <mb_gets+0xb2>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mb_gets>:
push   %rbx
xor    %eax,%eax
test   %rsi,%rsi
je     1183 <mb_gets+0x43>
test   %edx,%edx
je     1183 <mb_gets+0x43>
mov    %rsi,%rbx
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %rsi,%rdi
mov    %edx,%esi
mov    %eax,%edx
call   1040 <fgets@plt>
test   %rax,%rax
je     1185 <mb_gets+0x45>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    -0x1(%rax),%ecx
movslq %ecx,%rdx
cmpb   $0xa,(%rbx,%rdx,1)
jne    1183 <mb_gets+0x43>
movb   $0x0,(%rbx,%rdx,1)
mov    %ecx,%eax
pop    %rbx
ret
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%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, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %45
11: ; preds = %3
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %45
14: ; preds = %11
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = load i32, i32* @stdin
%18 = call i64 @fgets(i8* %15, i32 %16, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i8*, i8** %5
%26 = call i64 @strlen(i8* %25)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %7
%28 = load i8*, i8** %5
%29 = load i32, i32* %7
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 10
br i1 %35, label %36, label %44
36: ; preds = %24
%37 = load i8*, i8** %5
%38 = load i32, i32* %7
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* %7
%43 = add nsw i32 %42, -1
store i32 %43, i32* %7
br label %44
44: ; preds = %36, %24
br label %45
45: ; preds = %44, %11, %3
%46 = load i32, i32* %7
ret i32 %46
}

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* nocapture readnone %0, i8* %1, i32 %2) {
%4 = icmp ne i8* %1, null
%5 = icmp ne i32 %2, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %26
7: ; preds = %3
%8 = load i32, i32* @stdin
%9 = tail call i64 @fgets(i8* %1, i32 %2, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = tail call i64 @strlen(i8* %1)
%17 = trunc i64 %16 to i32
%18 = shl i64 %16, 32
%19 = add i64 %18, -4294967296
%20 = ashr exact i64 %19, 32
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %26
24: ; preds = %15
store i8 0, i8* %21
%25 = add nsw i32 %17, -1
br label %26
26: ; preds = %15, %24, %3
%27 = phi i32 [ %25, %24 ], [ %17, %15 ], [ 0, %3 ]
ret i32 %27
}