#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*/ error(char *, char *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int read(int /*<<< orphan*/, char *, int);
void getstr(char *buf, int cnt, char *err) {
  char c;
  do {
    if (read(0, &c, 1) != 1)
      exit(1);
    *buf++ = c;
    if (--cnt == 0) {
      error("%s too long\n", err, 0, 0);
      exit(1);
    }
  } 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   1050 <exit@plt>
mov    -0x19(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    11aa <getstr+0x7a>
mov    -0x18(%rbp),%rsi
lea    0xe69(%rip),%rdi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1040 <error@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11af <getstr+0x7f>
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jne    1143 <getstr+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r13d
mov    %rdi,%r15
add    $0xffffffff,%r13d
xor    %ebx,%ebx
lea    0xf(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1198 <getstr+0x68>
movzbl 0xf(%rsp),%eax
mov    %al,(%r15,%rbx,1)
cmp    %ebx,%r13d
je     11a2 <getstr+0x72>
add    $0x1,%rbx
test   %al,%al
jne    1160 <getstr+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xe57(%rip),%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <error@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"%s too long\0A\00"
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 = %25, %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 i8, i8* %7
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %4
store i8 %14, i8* %15
%17 = load i32, i32* %5
%18 = add nsw i32 %17, -1
store i32 %18, i32* %5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = load i8*, i8** %6
%22 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %21, i32 0, i32 0)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %13
br label %25
25: ; preds = %24
%26 = load i8, i8* %7
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %8, label %29
29: ; preds = %25
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"%s too long\0A\00"
define void @getstr(i8* nocapture writeonly %0, i32 %1, i8* %2) {
%4 = alloca i8
br label %5
5: ; preds = %19, %3
%6 = phi i32 [ %1, %3 ], [ %14, %19 ]
%7 = phi i8* [ %0, %3 ], [ %20, %19 ]
%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 = load i8, i8* %4
store i8 %13, i8* %7
%14 = add nsw i32 %6, -1
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %2, i32 0, i32 0)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = getelementptr inbounds i8, i8* %7, i64 1
%21 = icmp eq i8 %13, 0
br i1 %21, label %22, label %5
22: ; preds = %19
ret void
}