#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*/ err(int, char *);
unsigned char *malloc(int);
int read(int, unsigned char *, int);
unsigned char *_child_getstr(int fd, int len) {
  unsigned char *s, *pos;
  if (!(s = malloc(len + 1)))
    err(1, "malloc");
  pos = s;
  while (len) {
    int nb;
    nb = read(fd, pos, len);
    if (nb < 0)
      err(1, "read");
    pos += nb;
    len -= nb;
  }
  *pos = '\0';
  return s;
}


x86 assembly (clang, -O0):
<_child_getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1168 <_child_getstr+0x38>
mov    $0x1,%edi
lea    0xe9d(%rip),%rsi
call   1040 <err@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11c8 <_child_getstr+0x98>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a7 <_child_getstr+0x77>
mov    $0x1,%edi
lea    0xe65(%rip),%rsi
call   1040 <err@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
jmp    1170 <_child_getstr+0x40>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_child_getstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r13d
mov    %edi,%r15d
lea    0x1(%r13),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1163 <_child_getstr+0x33>
lea    0xea7(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
mov    %r14,%rbp
test   %r13d,%r13d
je     11ae <_child_getstr+0x7e>
lea    0xe95(%rip),%r12
mov    %r14,%rbp
jmp    118b <_child_getstr+0x5b>
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rax
add    %rax,%rbp
sub    %ebx,%r13d
je     11ae <_child_getstr+0x7e>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r13d,%edx
call   1030 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1180 <_child_getstr+0x50>
mov    $0x1,%edi
mov    %r12,%rsi
call   1040 <err@plt>
jmp    1180 <_child_getstr+0x50>
movb   $0x0,0x0(%rbp)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [5 x i8] c"read\00"
define noalias i8* @_child_getstr(i32 %0, i32 %1) {
%3 = add nsw i32 %1, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = icmp eq i32 %1, 0
br i1 %9, label %22, label %10
10: ; preds = %8, %17
%11 = phi i32 [ %20, %17 ], [ %1, %8 ]
%12 = phi i8* [ %19, %17 ], [ %4, %8 ]
%13 = tail call i32 @read(i32 %0, i8* %12, i32 %11)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = sext i32 %13 to i64
%19 = getelementptr inbounds i8, i8* %12, i64 %18
%20 = sub nsw i32 %11, %13
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %10
22: ; preds = %17, %8
%23 = phi i8* [ %4, %8 ], [ %19, %17 ]
store i8 0, i8* %23
ret i8* %4
}