#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 */
char *c_malloc(int);
int /*<<< orphan*/ clear_buffer();
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
char *s_resp(FILE *stream, int size) {
  char *result = c_malloc(size * sizeof(char));
  if (fgets(result, size + 1, stream) == NULL) {
    fprintf(stderr, "Error: fgets == NULL");
    clear_buffer();
    return 0;
  }
  long max = strlen(result) - 1;
  if (result[max] == '\n')
    result[max] = '\0';
  else
    clear_buffer();
  if (strlen(result) == 0) {
    free(result);
    return NULL;
  }
  return result;
}


x86 assembly (clang, -O0):
<s_resp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <c_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rdx
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    11c8 <s_resp+0x68>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1060 <clear_buffer@plt>
movq   $0x0,-0x8(%rbp)
jmp    1237 <s_resp+0xd7>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1200 <s_resp+0xa0>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1207 <s_resp+0xa7>
mov    $0x0,%al
call   1060 <clear_buffer@plt>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    122f <s_resp+0xcf>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1237 <s_resp+0xd7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s_resp>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
call   1050 <c_malloc@plt>
mov    %rax,%rbx
add    $0x1,%ebp
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     11a0 <s_resp+0x40>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11c0 <s_resp+0x60>
movb   $0x0,-0x1(%rbx,%rax,1)
jmp    11c7 <s_resp+0x67>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1080 <fprintf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <clear_buffer@plt>
jmp    11dd <s_resp+0x7d>
xor    %eax,%eax
call   1060 <clear_buffer@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jne    11dd <s_resp+0x7d>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Error: fgets == NULL\00"
define i8* @s_resp(i32* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i32* %0, i32** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 1
%11 = trunc i64 %10 to i32
%12 = call i8* @c_malloc(i32 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
%16 = load i32*, i32** %4
%17 = call i32* @fgets(i8* %13, i32 %15, i32* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%22 = call i32 (...) @clear_buffer()
store i8* null, i8** %3
br label %49
23: ; preds = %2
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
store i64 %27, i64* %7
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %38
34: ; preds = %23
%35 = load i8*, i8** %6
%36 = load i64, i64* %7
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
br label %40
38: ; preds = %23
%39 = call i32 (...) @clear_buffer()
br label %40
40: ; preds = %38, %34
%41 = load i8*, i8** %6
%42 = call i32 @strlen(i8* %41)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i8*, i8** %6
%46 = call i32 @free(i8* %45)
store i8* null, i8** %3
br label %49
47: ; preds = %40
%48 = load i8*, i8** %6
store i8* %48, i8** %3
br label %49
49: ; preds = %47, %44, %19
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Error: fgets == NULL\00"
define i8* @s_resp(i32* nocapture %0, i32 %1) {
%3 = tail call i8* @c_malloc(i32 %1)
%4 = add nsw i32 %1, 1
%5 = tail call i32* @fgets(i8* %3, i32 %4, i32* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 (...) @clear_buffer()
br label %26
11: ; preds = %2
%12 = tail call i32 @strlen(i8* %3)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %3, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %19
18: ; preds = %11
store i8 0, i8* %15
br label %21
19: ; preds = %11
%20 = tail call i32 (...) @clear_buffer()
br label %21
21: ; preds = %19, %18
%22 = tail call i32 @strlen(i8* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @free(i8* %3)
br label %26
26: ; preds = %24, %21, %7
%27 = phi i8* [ null, %7 ], [ null, %24 ], [ %3, %21 ]
ret i8* %27
}