#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 /*<<< orphan*/ exit(int);
char *fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
char *read_line(FILE *file, char *buff, size_t buff_size) {
  char *ret;
  size_t ret_len;
  ret = fgets(buff, buff_size, file);
  if (ret == NULL) {
    return ret;
  }
  ret_len = strlen(ret);
  if (ret_len == buff_size - 1) {
    fprintf(stderr, "line too long: %s\n", buff);
    exit(1);
  }
  if (ret_len > 0)
    buff[ret_len - 1] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <read_line+0x41>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e9 <read_line+0xa9>
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jne    11c6 <read_line+0x86>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jbe    11e1 <read_line+0xa1>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%rdx
mov    %rsi,%rdi
mov    %rbx,%rsi
call   1040 <fgets@plt>
mov    %rax,%r15
test   %rax,%rax
je     117d <read_line+0x3d>
mov    %r15,%rdi
call   1030 <strlen@plt>
add    $0xffffffffffffffff,%rbx
cmp    %rbx,%rax
je     1186 <read_line+0x46>
test   %rax,%rax
je     117d <read_line+0x3d>
movb   $0x0,-0x1(%rax,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"line too long: %s\0A\00"
define i8* @read_line(i32* nocapture %0, i8* %1, i64 %2) {
%4 = tail call i8* @fgets(i8* %1, i64 %2, i32* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %19, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* %4)
%8 = add i64 %2, -1
%9 = icmp eq i64 %7, %8
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 ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %1)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %6
%15 = icmp eq i64 %7, 0
br i1 %15, label %19, label %16
16: ; preds = %14
%17 = add i64 %7, -1
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %14, %16, %3
ret i8* %4
}