#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*/ *fgets(char *, int, int /*<<< orphan*/);
scalar_t__ isempty(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdin;
int strnlen(char *, int);
int read_cmd(char *buf, int bufsize) {
  int buflen;
  *buf = '\0';
  if (NULL == fgets(buf, bufsize, stdin)) {
    perror("fgets() failed while reading stdin");
    return -1;
  }
  if (isempty(buf)) {
    return 0;
  }
  buflen = strnlen(buf, bufsize);
  if (buflen == bufsize) {
    buf[--buflen] = '\0';
  }
  buf[buflen] = '\0';
  return buflen;
}


x86 assembly (clang, -O0):
<read_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e84(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1191 <read_cmd+0x51>
lea    0xe80(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <read_cmd+0xb1>
mov    -0x10(%rbp),%rdi
call   1060 <isempty@plt>
cmp    $0x0,%rax
je     11b0 <read_cmd+0x70>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <read_cmd+0xb1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <strnlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11df <read_cmd+0x9f>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_cmd>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
movb   $0x0,(%rdi)
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
test   %rax,%rax
je     119c <read_cmd+0x5c>
mov    %rbx,%rdi
call   1060 <isempty@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1195 <read_cmd+0x55>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <strnlen@plt>
cmp    %ebp,%eax
jne    118e <read_cmd+0x4e>
movslq %ebp,%rcx
movb   $0x0,-0x1(%rbx,%rcx,1)
add    $0xffffffffffffffff,%rcx
mov    %ecx,%eax
jmp    1191 <read_cmd+0x51>
movslq %eax,%rcx
movb   $0x0,(%rbx,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    1195 <read_cmd+0x55>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [35 x i8] c"fgets() failed while reading stdin\00"
define i32 @read_cmd(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
store i8 0, i8* %7
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = load i32, i32* @stdin
%11 = call i32* @fgets(i8* %8, i32 %9, i32 %10)
%12 = icmp eq i32* null, %11
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i64 @isempty(i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 0, i32* %3
br label %39
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = call i32 @strnlen(i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = load i8*, i8** %4
%29 = load i32, i32* %6
%30 = add nsw i32 %29, -1
store i32 %30, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %27, %20
%34 = load i8*, i8** %4
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
%38 = load i32, i32* %6
store i32 %38, i32* %3
br label %39
39: ; preds = %33, %19, %13
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [35 x i8] c"fgets() failed while reading stdin\00"
define i32 @read_cmd(i8* %0, i32 %1) {
store i8 0, i8* %0
%3 = load i32, i32* @stdin
%4 = tail call i32* @fgets(i8* %0, i32 %1, i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %24
8: ; preds = %2
%9 = tail call i64 @isempty(i8* %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %8
%12 = tail call i32 @strnlen(i8* %0, i32 %1)
%13 = icmp eq i32 %12, %1
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = sext i32 %12 to i64
br label %20
16: ; preds = %11
%17 = add nsw i32 %1, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %14, %16
%21 = phi i64 [ %15, %14 ], [ %18, %16 ]
%22 = phi i32 [ %12, %14 ], [ %17, %16 ]
%23 = getelementptr inbounds i8, i8* %0, i64 %21
store i8 0, i8* %23
br label %24
24: ; preds = %8, %20, %6
%25 = phi i32 [ -1, %6 ], [ %22, %20 ], [ 0, %8 ]
ret i32 %25
}