#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 */
char *fgets(char *, unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
unsigned int strlen(char *);
int stdin_read_line(char *str, unsigned int max_bytes) {
  char *result = fgets(str, max_bytes, stdin);
  if (result != str) {
    return 1;
  }
  unsigned int bytes_read = strlen(str);
  if (bytes_read != max_bytes) {
    str[bytes_read - 1] = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<stdin_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2eab(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1162 <stdin_read_line+0x42>
movl   $0x1,-0x4(%rbp)
jmp    1191 <stdin_read_line+0x71>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     118a <stdin_read_line+0x6a>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdin_read_line>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2eb9(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,%rcx
mov    $0x1,%eax
cmp    %rbx,%rcx
jne    115a <stdin_read_line+0x3a>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
je     115a <stdin_read_line+0x3a>
add    $0xffffffff,%ecx
movb   $0x0,(%rbx,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
define i32 @stdin_read_line(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = load i32, i32* @stdin
%11 = call i8* @fgets(i8* %8, i32 %9, i32 %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = load i8*, i8** %4
%14 = icmp ne i8* %12, %13
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 1, i32* %3
br label %29
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = load i32, i32* %5
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i32, i32* %7
%25 = sub i32 %24, 1
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %22, %16
store i32 0, i32* %3
br label %29
29: ; preds = %28, %15
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@stdin = global i32 0
define i32 @stdin_read_line(i8* %0, i32 %1) {
%3 = load i32, i32* @stdin
%4 = tail call i8* @fgets(i8* %0, i32 %1, i32 %3)
%5 = icmp eq i8* %4, %0
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp eq i32 %7, %1
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = add i32 %7, -1
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %6, %9, %2
%14 = phi i32 [ 1, %2 ], [ 0, %9 ], [ 0, %6 ]
ret i32 %14
}