#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*/ readn(int, ...);
int /*<<< orphan*/ warnx(char *);
char *xmalloc(int);
int read_block(int fd, char **buffer) {
  int size = 0;
  readn(fd, &size, sizeof(int));
  if (size == 0) {
    *buffer = NULL;
    return 0;
  }
  *buffer = xmalloc(size);
  readn(fd, *buffer, size);
  if ((*buffer)[size - 1]) {
    warnx("read_block: input is not null-terminated");
    return -2;
  }
  return size;
}


x86 assembly (clang, -O0):
<read_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <readn@plt>
cmpl   $0x0,-0x14(%rbp)
jne    117a <read_block+0x4a>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11d8 <read_block+0xa8>
mov    -0x14(%rbp),%edi
call   1040 <xmalloc@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
mov    $0x0,%al
call   1050 <readn@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11d2 <read_block+0xa2>
lea    0xe3f(%rip),%rdi
call   1030 <warnx@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d8 <read_block+0xa8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_block>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movl   $0x0,0xc(%rsp)
xor    %ebx,%ebx
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <readn@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
je     119a <read_block+0x6a>
call   1040 <xmalloc@plt>
mov    %rax,(%r14)
mov    0xc(%rsp),%edx
mov    %ebp,%edi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <readn@plt>
mov    (%r14),%rax
movslq 0xc(%rsp),%rbx
cmpb   $0x0,-0x1(%rbx,%rax,1)
je     11a1 <read_block+0x71>
lea    0xe72(%rip),%rdi
call   1030 <warnx@plt>
mov    $0xfffffffe,%ebx
jmp    11a1 <read_block+0x71>
movq   $0x0,(%r14)
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [41 x i8] c"read_block: input is not null-terminated\00"
define i32 @read_block(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = call i32 (i32, ...) @readn(i32 %0, i32* %3, i64 4)
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %2
store i8* null, i8** %1
br label %22
9: ; preds = %2
%10 = call i8* @xmalloc(i32 %6)
store i8* %10, i8** %1
%11 = load i32, i32* %3
%12 = call i32 (i32, ...) @readn(i32 %0, i8* %10, i32 %11)
%13 = load i8*, i8** %1
%14 = load i32, i32* %3
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %9
%21 = call i32 @warnx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %9, %20, %8
%23 = phi i32 [ 0, %8 ], [ -2, %20 ], [ %14, %9 ]
ret i32 %23
}