#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 */
scalar_t__ check_malloc(unsigned long long);
int /*<<< orphan*/ die(char *);
unsigned long long read_ull(int);
int /*<<< orphan*/ readn(int, char *, unsigned long long);
char *read_str(int fd) {
  unsigned long long sz = read_ull(fd);
  if (!sz) {
    die("read_str sz == 0");
  }
  char *buf = (char *)check_malloc(sz);
  readn(fd, buf, sz);
  buf[sz - 1] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<read_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <read_ull@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116e <read_str+0x2e>
lea    0xe97(%rip),%rdi
call   1060 <die@plt>
mov    -0x10(%rbp),%rdi
call   1050 <check_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <readn@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_str>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
call   1040 <read_ull@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1160 <read_str+0x20>
lea    0xea5(%rip),%rdi
call   1060 <die@plt>
mov    %rbx,%rdi
call   1050 <check_malloc@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1030 <readn@plt>
movb   $0x0,-0x1(%rbx,%rbp,1)
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"read_str sz == 0\00"
define i8* @read_str(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i64 @read_ull(i32 %5)
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = icmp ne i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = load i64, i64* %3
%13 = call i64 @check_malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %4
%15 = load i32, i32* %2
%16 = load i8*, i8** %4
%17 = load i64, i64* %3
%18 = call i32 @readn(i32 %15, i8* %16, i64 %17)
%19 = load i8*, i8** %4
%20 = load i64, i64* %3
%21 = sub i64 %20, 1
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 0, i8* %22
%23 = load i8*, i8** %4
ret i8* %23
}

LLVM IR (-O2):
@.str = [17 x i8] c"read_str sz == 0\00"
define i8* @read_str(i32 %0) {
%2 = tail call i64 @read_ull(i32 %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i64 @check_malloc(i64 %2)
%8 = inttoptr i64 %7 to i8*
%9 = tail call i32 @readn(i32 %0, i8* %8, i64 %2)
%10 = add i64 %2, -1
%11 = getelementptr inbounds i8, i8* %8, i64 %10
store i8 0, i8* %11
ret i8* %8
}