#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*/ exit(int);
int /*<<< orphan*/ fatal(int, char *);
int read(int /*<<< orphan*/, char *, int);
void getstr(char *buf, int cnt, char *errmsg) {
  char c;
  do {
    if (read(0, &c, 1) != 1)
      exit(1);
    if (--cnt < 0)
      fatal(1, errmsg);
    *buf++ = c;
  } while (c != 0);
}


x86 assembly (clang, -O0):
<getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %edi,%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1166 <getstr+0x36>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1186 <getstr+0x56>
mov    -0x18(%rbp),%rsi
mov    $0x1,%edi
call   1050 <fatal@plt>
mov    -0x19(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jne    1143 <getstr+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x7(%rsp),%r15
jmp    1162 <getstr+0x32>
cs nopw 0x0(%rax,%rax,1)
movzbl 0x7(%rsp),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
test   %al,%al
je     1189 <getstr+0x59>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1194 <getstr+0x64>
test   %ebp,%ebp
jg     1150 <getstr+0x20>
mov    $0x1,%edi
mov    %r14,%rsi
call   1050 <fatal@plt>
jmp    1150 <getstr+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
define void @getstr(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
br label %8
8: ; preds = %24, %3
%9 = call i32 @read(i32 0, i8* %7, i32 1)
%10 = icmp ne i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %8
%14 = load i32, i32* %5
%15 = add nsw i32 %14, -1
store i32 %15, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i8*, i8** %6
%19 = call i32 @fatal(i32 1, i8* %18)
br label %20
20: ; preds = %17, %13
%21 = load i8, i8* %7
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
store i8 %21, i8* %22
br label %24
24: ; preds = %20
%25 = load i8, i8* %7
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br i1 %27, label %8, label %28
28: ; preds = %24
ret void
}

LLVM IR (-O2):
define void @getstr(i8* nocapture writeonly %0, i32 %1, i8* %2) {
%4 = alloca i8
br label %5
5: ; preds = %17, %3
%6 = phi i32 [ %1, %3 ], [ %13, %17 ]
%7 = phi i8* [ %0, %3 ], [ %19, %17 ]
%8 = call i32 @read(i32 0, i8* %4, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %5
%13 = add nsw i32 %6, -1
%14 = icmp slt i32 %6, 1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @fatal(i32 1, i8* %2)
br label %17
17: ; preds = %15, %12
%18 = load i8, i8* %4
%19 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %18, i8* %7
%20 = icmp eq i8 %18, 0
br i1 %20, label %21, label %5
21: ; preds = %17
ret void
}