#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*/ alarm(int);
int /*<<< orphan*/ getline_from_buffer(char *, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int test_getline_from_buffer(void) {
  char linebuf[256];
  char scratchbuf[256];
  alarm(100);
  strcpy(scratchbuf, "\r\n");
  getline_from_buffer(scratchbuf, linebuf, 256);
  if (strlen(scratchbuf) != 0) {
    printf("FAILURE: scratchbuf not snarfed\n");
    return (1);
  }
  if (strlen(linebuf) != 2) {
    printf("FAILURE: linebuf not populated\n");
    return (1);
  }
  strcpy(scratchbuf, "\n");
  getline_from_buffer(scratchbuf, linebuf, 256);
  if (strlen(scratchbuf) != 0) {
    printf("FAILURE: scratchbuf not snarfed\n");
    return (1);
  }
  if (strlen(linebuf) != 1) {
    printf("FAILURE: linebuf not populated\n");
    return (1);
  }
  alarm(0);
  return (0);
}


x86 assembly (clang, -O0):
<test_getline_from_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
mov    $0x64,%edi
call   1060 <alarm@plt>
lea    -0x210(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x210(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1070 <getline_from_buffer@plt>
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
je     11bd <test_getline_from_buffer+0x6d>
lea    0xe57(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127d <test_getline_from_buffer+0x12d>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
je     11ea <test_getline_from_buffer+0x9a>
lea    0xe4b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127d <test_getline_from_buffer+0x12d>
lea    -0x210(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x210(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1070 <getline_from_buffer@plt>
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
je     1242 <test_getline_from_buffer+0xf2>
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127d <test_getline_from_buffer+0x12d>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x1,%eax
je     126f <test_getline_from_buffer+0x11f>
lea    0xdc6(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127d <test_getline_from_buffer+0x12d>
xor    %edi,%edi
call   1060 <alarm@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_getline_from_buffer>:
push   %rbx
sub    $0x200,%rsp
mov    $0x64,%edi
call   1060 <alarm@plt>
lea    0xe97(%rip),%rsi
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x100,%edx
call   1070 <getline_from_buffer@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     119e <test_getline_from_buffer+0x4e>
lea    0xe86(%rip),%rdi
jmp    11fd <test_getline_from_buffer+0xad>
mov    %rsp,%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jne    11f6 <test_getline_from_buffer+0xa6>
lea    0xe4f(%rip),%rsi
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x100,%edx
call   1070 <getline_from_buffer@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jne    1195 <test_getline_from_buffer+0x45>
mov    %rsp,%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jne    11f6 <test_getline_from_buffer+0xa6>
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <alarm@plt>
jmp    1207 <test_getline_from_buffer+0xb7>
lea    0xe06(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [33 x i8] c"FAILURE: scratchbuf not snarfed\0A\00"
@.str.2 = [32 x i8] c"FAILURE: linebuf not populated\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @test_getline_from_buffer() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [256 x i8]
%4 = call i32 @alarm(i32 100)
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%9 = call i32 @getline_from_buffer(i8* %7, i8* %8, i32 256)
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %40
15: ; preds = %0
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %40
21: ; preds = %15
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%26 = call i32 @getline_from_buffer(i8* %24, i8* %25, i32 256)
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %40
32: ; preds = %21
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %40
38: ; preds = %32
%39 = call i32 @alarm(i32 0)
store i32 0, i32* %1
br label %40
40: ; preds = %38, %36, %30, %19, %13
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@str.5 = [31 x i8] c"FAILURE: linebuf not populated\00"
@str.6 = [32 x i8] c"FAILURE: scratchbuf not snarfed\00"
define i32 @test_getline_from_buffer() {
%1 = alloca [256 x i8]
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @alarm(i32 100)
%6 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @getline_from_buffer(i8* %4, i8* %3, i32 256)
%8 = call i32 @strlen(i8* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
br label %31
12: ; preds = %0
%13 = call i32 @strlen(i8* %3)
%14 = icmp eq i32 %13, 2
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
br label %31
17: ; preds = %12
%18 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @getline_from_buffer(i8* %4, i8* %3, i32 256)
%20 = call i32 @strlen(i8* %4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
br label %31
24: ; preds = %17
%25 = call i32 @strlen(i8* %3)
%26 = icmp eq i32 %25, 1
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
br label %31
29: ; preds = %24
%30 = call i32 @alarm(i32 0)
br label %31
31: ; preds = %29, %27, %22, %15, %10
%32 = phi i32 [ 1, %10 ], [ 1, %15 ], [ 1, %22 ], [ 1, %27 ], [ 0, %29 ]
ret i32 %32
}