#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 */
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int keyboard_read(char *message, char *buffer, int to_read) {
  int read = 0;
  if (message && buffer && (to_read > 0)) {
    printf(message);
    if (fgets(buffer, to_read, stdin)) {
      read = to_read;
    }
  }
  return read;
}


x86 assembly (clang, -O0):
<keyboard_read>:
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,-0x8(%rbp)
je     118d <keyboard_read+0x6d>
cmpq   $0x0,-0x10(%rbp)
je     118d <keyboard_read+0x6d>
cmpl   $0x0,-0x14(%rbp)
jle    118d <keyboard_read+0x6d>
mov    -0x8(%rbp),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1188 <keyboard_read+0x68>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    118d <keyboard_read+0x6d>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keyboard_read>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
test   %rdi,%rdi
je     115b <keyboard_read+0x3b>
mov    %rsi,%rbx
test   %rsi,%rsi
je     115b <keyboard_read+0x3b>
mov    %edx,%ebp
test   %edx,%edx
jle    115b <keyboard_read+0x3b>
call   1030 <printf@plt>
mov    0x2ea4(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
cmovne %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
define i32 @keyboard_read(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 = icmp ne i8* %8, null
br i1 %9, label %10, label %27
10: ; preds = %3
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = call i32 @printf(i8* %17)
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = load i32, i32* @stdin
%22 = call i64 @fgets(i8* %19, i32 %20, i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = load i32, i32* %6
store i32 %25, i32* %7
br label %26
26: ; preds = %24, %16
br label %27
27: ; preds = %26, %13, %10, %3
%28 = load i32, i32* %7
ret i32 %28
}

LLVM IR (-O2):
@stdin = global i32 0
define i32 @keyboard_read(i8* %0, i8* %1, i32 %2) {
%4 = icmp ne i8* %0, null
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
%7 = icmp sgt i32 %2, 0
%8 = and i1 %6, %7
br i1 %8, label %9, label %15
9: ; preds = %3
%10 = tail call i32 @printf(i8* %0)
%11 = load i32, i32* @stdin
%12 = tail call i64 @fgets(i8* %1, i32 %2, i32 %11)
%13 = icmp eq i64 %12, 0
%14 = select i1 %13, i32 0, i32 %2
br label %15
15: ; preds = %9, %3
%16 = phi i32 [ 0, %3 ], [ %14, %9 ]
ret i32 %16
}