#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*/ szx;

/* Variables and functions */
int errno;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
char *strstr(char *, char *);
int get_input(int x, char *szbuf, int len) {
  if (NULL == szbuf || len <= 0) {
    return -1;
  }
  int pos = 0;
  char szx[1024] = {0};
  while (1) {
    if (NULL == fgets(szx, sizeof(szx), stdin)) {
      do {
        printf("[%2d]--", x);
        printf("fgets() error : [%d]", errno);
        printf("\n");
      } while (0);
      return -1;
    }
    if (strstr(szx, "quit") == szx) {
      break;
    }
    if (strlen(szx) + strlen(szbuf) >= len) {
      break;
    }
    pos += snprintf(szbuf + pos, len - pos, "%s", szx);
  }
  return strlen(szbuf);
}


x86 assembly (clang, -O0):
<get_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     118b <get_input+0x2b>
cmpl   $0x0,-0x14(%rbp)
jg     1197 <get_input+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <get_input+0x15d>
movl   $0x0,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x420(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1220 <get_input+0xc0>
jmp    11de <get_input+0x7e>
mov    -0x8(%rbp),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <get_input+0x15d>
lea    -0x420(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1080 <strstr@plt>
lea    -0x420(%rbp),%rcx
cmp    %rcx,%rax
jne    1248 <get_input+0xe8>
jmp    12b1 <get_input+0x151>
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x424(%rbp),%eax
add    %ecx,%eax
cmp    -0x14(%rbp),%eax
jl     127b <get_input+0x11b>
jmp    12b1 <get_input+0x151>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x18(%rbp),%esi
lea    -0x420(%rbp),%rcx
lea    0xd8a(%rip),%rdx
call   1050 <snprintf@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    11b1 <get_input+0x51>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_input>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    $0xffffffff,%eax
test   %rsi,%rsi
je     129d <get_input+0x12d>
mov    %edx,%r15d
test   %edx,%edx
jle    129d <get_input+0x12d>
mov    %rsi,%r13
mov    %edi,0xc(%rsp)
lea    0x10(%rsp),%rbx
xor    %ebp,%ebp
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     125b <get_input+0xeb>
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
lea    0xe36(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <strncmp@plt>
test   %eax,%eax
je     1295 <get_input+0x125>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %r13,%rdi
call   1050 <strlen@plt>
add    %r14d,%eax
cmp    %r15d,%eax
jge    1295 <get_input+0x125>
movslq %ebp,%rbx
lea    (%rbx,%r13,1),%rdi
mov    %r15d,%esi
sub    %ebx,%esi
lea    0xdef(%rip),%rdx
mov    %r12,%rcx
call   1070 <snprintf@plt>
mov    %rax,%rbp
add    %ebx,%ebp
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
jne    11e0 <get_input+0x70>
lea    0xd9e(%rip),%rdi
mov    0xc(%rsp),%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
lea    0xd8b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%eax
jmp    129d <get_input+0x12d>
mov    %r13,%rdi
call   1050 <strlen@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [8 x i8] c"[%2d]--\00"
@.str.1 = [21 x i8] c"fgets() error : [%d]\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [5 x i8] c"quit\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @get_input(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = icmp eq i8* null, %10
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32, i32* %7
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12, %3
store i32 -1, i32* %4
br label %63
16: ; preds = %12
store i32 0, i32* %8
%17 = bitcast [1024 x i8]* %9 to i8*
br label %18
18: ; preds = %16, %46
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%20 = load i32, i32* @stdin
%21 = call i32* @fgets(i8* %19, i32 1024, i32 %20)
%22 = icmp eq i32* null, %21
br i1 %22, label %23, label %31
23: ; preds = %18
br label %24
24: ; preds = %23
%25 = load i32, i32* %5
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @errno
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %24
store i32 -1, i32* %4
br label %63
31: ; preds = %18
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%33 = call i8* @strstr(i8* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %37
36: ; preds = %31
br label %60
37: ; preds = %31
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = load i8*, i8** %6
%41 = call i32 @strlen(i8* %40)
%42 = add nsw i32 %39, %41
%43 = load i32, i32* %7
%44 = icmp sge i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %37
br label %60
46: ; preds = %37
%47 = load i8*, i8** %6
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i32, i32* %7
%52 = load i32, i32* %8
%53 = sub nsw i32 %51, %52
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%55 = call i64 @snprintf(i8* %50, i32 %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = add nsw i64 %57, %55
%59 = trunc i64 %58 to i32
store i32 %59, i32* %8
br label %18
60: ; preds = %45, %36
%61 = load i8*, i8** %6
%62 = call i32 @strlen(i8* %61)
store i32 %62, i32* %4
br label %63
63: ; preds = %60, %30, %15
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [8 x i8] c"[%2d]--\00"
@.str.1 = [21 x i8] c"fgets() error : [%d]\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"quit\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @get_input(i32 %0, i8* %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = icmp eq i8* %1, null
%6 = icmp slt i32 %2, 1
%7 = or i1 %5, %6
br i1 %7, label %42, label %8
8: ; preds = %3
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @stdin
%11 = call i32* @fgets(i8* %9, i32 1024, i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %18
13: ; preds = %28, %8
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %0)
%15 = load i32, i32* @errno
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 @putchar(i32 10)
br label %40
18: ; preds = %8, %28
%19 = phi i32 [ %34, %28 ], [ 0, %8 ]
%20 = call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @strncmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %38, label %23
23: ; preds = %18
%24 = call i32 @strlen(i8* %9)
%25 = call i32 @strlen(i8* %1)
%26 = add nsw i32 %25, %24
%27 = icmp slt i32 %26, %2
br i1 %27, label %28, label %38
28: ; preds = %23
%29 = sext i32 %19 to i64
%30 = getelementptr inbounds i8, i8* %1, i64 %29
%31 = sub nsw i32 %2, %19
%32 = call i64 @snprintf(i8* %30, i32 %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%33 = trunc i64 %32 to i32
%34 = add i32 %19, %33
%35 = load i32, i32* @stdin
%36 = call i32* @fgets(i8* %9, i32 1024, i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %13, label %18
38: ; preds = %23, %18
%39 = call i32 @strlen(i8* %1)
br label %40
40: ; preds = %38, %13
%41 = phi i32 [ -1, %13 ], [ %39, %38 ]
br label %42
42: ; preds = %3, %40
%43 = phi i32 [ %41, %40 ], [ -1, %3 ]
ret i32 %43
}