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

/* Variables and functions */
int check_arg_count(int, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ inchnstr(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ report_count(int);
int /*<<< orphan*/ report_error(char *);
int /*<<< orphan*/ report_nstr(int /*<<< orphan*/ *);
int /*<<< orphan*/ report_return(int /*<<< orphan*/);
scalar_t__ sscanf(char *, char *, int *);
void cmd_inchnstr(int nargs, char **args) {
  int limit;
  chtype *string;
  if (check_arg_count(nargs, 1) == 1)
    return;
  if (sscanf(args[0], "%d", &limit) == 0) {
    report_count(1);
    report_error("BAD ARGUMENT");
    return;
  }
  if ((string = malloc((limit + 1) * sizeof(chtype))) == NULL) {
    report_count(1);
    report_error("MALLOC_FAILED");
    return;
  }
  report_count(2);
  report_return(inchnstr(string, limit));
  report_nstr(string);
  free(string);
}


x86 assembly (clang, -O0):
<cmd_inchnstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1070 <check_arg_count@plt>
cmp    $0x1,%eax
jne    11ba <cmd_inchnstr+0x2a>
jmp    1261 <cmd_inchnstr+0xd1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe38(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   10a0 <sscanf@plt>
cmp    $0x0,%rax
jne    11f6 <cmd_inchnstr+0x66>
mov    $0x1,%edi
call   1090 <report_count@plt>
lea    0xe17(%rip),%rdi
call   10b0 <report_error@plt>
jmp    1261 <cmd_inchnstr+0xd1>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1232 <cmd_inchnstr+0xa2>
mov    $0x1,%edi
call   1090 <report_count@plt>
lea    0xde8(%rip),%rdi
call   10b0 <report_error@plt>
jmp    1261 <cmd_inchnstr+0xd1>
mov    $0x2,%edi
call   1090 <report_count@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <inchnstr@plt>
mov    %eax,%edi
call   1050 <report_return@plt>
mov    -0x20(%rbp),%rdi
call   1040 <report_nstr@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_inchnstr>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    $0x1,%esi
call   1070 <check_arg_count@plt>
cmp    $0x1,%eax
je     1234 <cmd_inchnstr+0xa4>
mov    (%rbx),%rdi
lea    0xe4b(%rip),%rsi
lea    0xc(%rsp),%rdx
call   10a0 <sscanf@plt>
test   %rax,%rax
je     120b <cmd_inchnstr+0x7b>
mov    0xc(%rsp),%eax
lea    0x4(,%rax,4),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     121e <cmd_inchnstr+0x8e>
mov    %rax,%rbx
mov    $0x2,%edi
call   1090 <report_count@plt>
mov    0xc(%rsp),%esi
mov    %rbx,%rdi
call   1060 <inchnstr@plt>
mov    %eax,%edi
call   1050 <report_return@plt>
mov    %rbx,%rdi
call   1040 <report_nstr@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1234 <cmd_inchnstr+0xa4>
mov    $0x1,%edi
call   1090 <report_count@plt>
lea    0xde7(%rip),%rdi
jmp    122f <cmd_inchnstr+0x9f>
mov    $0x1,%edi
call   1090 <report_count@plt>
lea    0xde1(%rip),%rdi
call   10b0 <report_error@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [13 x i8] c"BAD ARGUMENT\00"
@.str.2 = [14 x i8] c"MALLOC_FAILED\00"
define void @cmd_inchnstr(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i32, i32* %3
%8 = call i32 @check_arg_count(i32 %7, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %2
br label %41
11: ; preds = %2
%12 = load i8**, i8*** %4
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i64 @sscanf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @report_count(i32 1)
%19 = call i32 @report_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %41
20: ; preds = %11
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = mul i64 %23, 4
%25 = trunc i64 %24 to i32
%26 = call i32* @malloc(i32 %25)
store i32* %26, i32** %6
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = call i32 @report_count(i32 1)
%30 = call i32 @report_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %41
31: ; preds = %20
%32 = call i32 @report_count(i32 2)
%33 = load i32*, i32** %6
%34 = load i32, i32* %5
%35 = call i32 @inchnstr(i32* %33, i32 %34)
%36 = call i32 @report_return(i32 %35)
%37 = load i32*, i32** %6
%38 = call i32 @report_nstr(i32* %37)
%39 = load i32*, i32** %6
%40 = call i32 @free(i32* %39)
br label %41
41: ; preds = %31, %28, %17, %10
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [13 x i8] c"BAD ARGUMENT\00"
@.str.2 = [14 x i8] c"MALLOC_FAILED\00"
define void @cmd_inchnstr(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @check_arg_count(i32 %0, i32 1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %30, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = call i64 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = call i32 @report_count(i32 1)
%13 = call i32 @report_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %30
14: ; preds = %7
%15 = load i32, i32* %3
%16 = shl i32 %15, 2
%17 = add i32 %16, 4
%18 = call i32* @malloc(i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @report_count(i32 1)
%22 = call i32 @report_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %30
23: ; preds = %14
%24 = call i32 @report_count(i32 2)
%25 = load i32, i32* %3
%26 = call i32 @inchnstr(i32* %18, i32 %25)
%27 = call i32 @report_return(i32 %26)
%28 = call i32 @report_nstr(i32* %18)
%29 = call i32 @free(i32* %18)
br label %30
30: ; preds = %2, %23, %20, %11
ret void
}