#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*/ die(int, char *);
char *fgets(char *, int, int /*<<< orphan*/ *);
size_t strlen(char *);
void get_line(char *s, int size, FILE *stream) {
  char *p = fgets(s, size, stream);
  size_t len;
  if (!p)
    die(1, "unexpected end of file");
  len = strlen(p);
  if (s[len - 1] != '\n')
    die(1, "buffer overflow");
  s[len - 1] = 0;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1173 <get_line+0x43>
mov    $0x1,%edi
lea    0xe92(%rip),%rsi
call   1050 <die@plt>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11aa <get_line+0x7a>
mov    $0x1,%edi
lea    0xe72(%rip),%rsi
call   1050 <die@plt>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <fgets@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1155 <get_line+0x25>
lea    0xeb5(%rip),%rsi
mov    $0x1,%edi
call   1050 <die@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
cmpb   $0xa,-0x1(%rax,%r14,1)
je     1179 <get_line+0x49>
lea    0xea8(%rip),%rsi
mov    $0x1,%edi
call   1050 <die@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"unexpected end of file\00"
@.str.1 = [16 x i8] c"buffer overflow\00"
define void @get_line(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = load i32*, i32** %6
%12 = call i8* @fgets(i8* %9, i32 %10, i32* %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = call i32 @die(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %3
%18 = load i8*, i8** %7
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %8
%20 = load i8*, i8** %4
%21 = load i64, i64* %8
%22 = sub i64 %21, 1
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 10
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @die(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %17
%30 = load i8*, i8** %4
%31 = load i64, i64* %8
%32 = sub i64 %31, 1
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"unexpected end of file\00"
@.str.1 = [16 x i8] c"buffer overflow\00"
define void @get_line(i8* %0, i32 %1, i32* nocapture %2) {
%4 = tail call i8* @fgets(i8* %0, i32 %1, i32* %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @die(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%10 = add i64 %9, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @die(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %8
store i8 0, i8* %11
ret void
}