#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*/ die(char *, int, int);
char *my_mallocc(int, char *);
int read(int, char *, int);
int readi(int, int *);
int readslen(int fd, char **dest) {
  int i;
  int r = readi(fd, &i);
  if (!r)
    return r;
  (*dest) = my_mallocc(i + 1, "string read");
  r = read(fd, *dest, i);
  if (r != i)
    die("Couldn't read full string (Expected %d, got %d)\n", i, r);
  return r;
}


x86 assembly (clang, -O0):
<readslen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1060 <readi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1173 <readslen+0x33>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c8 <readslen+0x88>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    0xe80(%rip),%rsi
call   1030 <my_mallocc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11c2 <readslen+0x82>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe4f(%rip),%rdi
call   1050 <die@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readslen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
lea    0x4(%rsp),%rsi
call   1060 <readi@plt>
test   %eax,%eax
je     1195 <readslen+0x55>
mov    0x4(%rsp),%edi
add    $0x1,%edi
lea    0xe9b(%rip),%rsi
call   1030 <my_mallocc@plt>
mov    %rax,(%r14)
mov    0x4(%rsp),%edx
mov    %ebx,%edi
mov    %rax,%rsi
call   1040 <read@plt>
mov    %eax,%ebx
mov    0x4(%rsp),%esi
cmp    %esi,%eax
je     1197 <readslen+0x57>
lea    0xe80(%rip),%rdi
mov    %ebx,%edx
call   1050 <die@plt>
jmp    1197 <readslen+0x57>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [12 x i8] c"string read\00"
@.str.1 = [49 x i8] c"Couldn't read full string (Expected %d, got %d)\0A\00"
define i32 @readslen(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = call i32 @readi(i32 %8, i32* %6)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = load i32, i32* %7
store i32 %13, i32* %3
br label %33
14: ; preds = %2
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
%17 = call i8* @my_mallocc(i32 %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%18 = load i8**, i8*** %5
store i8* %17, i8** %18
%19 = load i32, i32* %4
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = load i32, i32* %6
%23 = call i32 @read(i32 %19, i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = load i32, i32* %6
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %14
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i32 @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
br label %31
31: ; preds = %27, %14
%32 = load i32, i32* %7
store i32 %32, i32* %3
br label %33
33: ; preds = %31, %12
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [12 x i8] c"string read\00"
@.str.1 = [49 x i8] c"Couldn't read full string (Expected %d, got %d)\0A\00"
define i32 @readslen(i32 %0, i8** nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @readi(i32 %0, i32* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %2
%8 = load i32, i32* %3
%9 = add nsw i32 %8, 1
%10 = call i8* @my_mallocc(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %1
%11 = load i32, i32* %3
%12 = call i32 @read(i32 %0, i8* %10, i32 %11)
%13 = load i32, i32* %3
%14 = icmp eq i32 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %7
%16 = call i32 @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %12)
br label %17
17: ; preds = %7, %15, %2
%18 = phi i32 [ 0, %2 ], [ %12, %15 ], [ %12, %7 ]
ret i32 %18
}