#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ ENOBUFS;
int /*<<< orphan*/ errno;
char *pop_buffer(char *, size_t *, size_t);
int read(int, char *, size_t);
char *strndup(char *, size_t);
char *read_field(int fd, char *buf, size_t *fill, size_t len) {
  for (;;) {
    char *r;
    size_t n;
    ssize_t z;
    r = pop_buffer(buf, fill, len);
    if (r != NULL)
      return r;
    n = len - *fill;
    if (n == 0) {
      errno = ENOBUFS;
      return NULL;
    }
    z = read(fd, buf + *fill, n);
    ;
    if (z == -1)
      return NULL;
    if (z == 0) {
      if (*fill == 0)
        break;
      r = strndup(buf, *fill);
      if (r == NULL)
        return NULL;
      *fill = 0;
      return r;
    }
    *fill += z;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<read_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <pop_buffer@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1174 <read_field+0x44>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125a <read_field+0x12a>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ad <read_field+0x7d>
mov    0x2e63(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3a(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    125a <read_field+0x12a>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
add    (%rax),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11de <read_field+0xae>
movq   $0x0,-0x8(%rbp)
jmp    125a <read_field+0x12a>
cmpl   $0x0,-0x3c(%rbp)
jne    123f <read_field+0x10f>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11fb <read_field+0xcb>
jmp    1252 <read_field+0x122>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strndup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1227 <read_field+0xf7>
movq   $0x0,-0x8(%rbp)
jmp    125a <read_field+0x12a>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125a <read_field+0x12a>
movslq -0x3c(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
jmp    1147 <read_field+0x17>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_field>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %rcx,%rdx
call   1050 <pop_buffer@plt>
mov    %rax,%r14
test   %rax,%rax
je     1165 <read_field+0x35>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%rsi
mov    %r12,%rdx
sub    %rsi,%rdx
je     11ab <read_field+0x7b>
add    %r15,%rsi
mov    %ebp,%edi
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1159 <read_field+0x29>
test   %eax,%eax
je     11bf <read_field+0x8f>
cltq
add    %rax,(%rbx)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1050 <pop_buffer@plt>
test   %rax,%rax
je     1170 <read_field+0x40>
mov    %rax,%r14
jmp    1159 <read_field+0x29>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    0x2e1d(%rip),%rcx
mov    %eax,(%rcx)
jmp    11e3 <read_field+0xb3>
mov    (%rbx),%rsi
test   %rsi,%rsi
je     11e3 <read_field+0xb3>
mov    %r15,%rdi
call   1040 <strndup@plt>
test   %rax,%rax
je     11e3 <read_field+0xb3>
mov    %rax,%r14
movq   $0x0,(%rbx)
jmp    1159 <read_field+0x29>
xor    %r14d,%r14d
jmp    1159 <read_field+0x29>

LLVM IR (-O0):
@ENOBUFS = global i32 0
@errno = global i32 0
define i8* @read_field(i32 %0, i8* %1, i64* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64* %2, i64** %8
store i64 %3, i64* %9
br label %13
13: ; preds = %61, %4
%14 = load i8*, i8** %7
%15 = load i64*, i64** %8
%16 = load i64, i64* %9
%17 = call i8* @pop_buffer(i8* %14, i64* %15, i64 %16)
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = load i8*, i8** %10
store i8* %21, i8** %5
br label %68
22: ; preds = %13
%23 = load i64, i64* %9
%24 = load i64*, i64** %8
%25 = load i64, i64* %24
%26 = sub i64 %23, %25
store i64 %26, i64* %11
%27 = load i64, i64* %11
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = load i32, i32* @ENOBUFS
store i32 %30, i32* @errno
store i8* null, i8** %5
br label %68
31: ; preds = %22
%32 = load i32, i32* %6
%33 = load i8*, i8** %7
%34 = load i64*, i64** %8
%35 = load i64, i64* %34
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i64, i64* %11
%38 = call i32 @read(i32 %32, i8* %36, i64 %37)
store i32 %38, i32* %12
%39 = load i32, i32* %12
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %31
store i8* null, i8** %5
br label %68
42: ; preds = %31
%43 = load i32, i32* %12
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %61
45: ; preds = %42
%46 = load i64*, i64** %8
%47 = load i64, i64* %46
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
br label %67
50: ; preds = %45
%51 = load i8*, i8** %7
%52 = load i64*, i64** %8
%53 = load i64, i64* %52
%54 = call i8* @strndup(i8* %51, i64 %53)
store i8* %54, i8** %10
%55 = load i8*, i8** %10
%56 = icmp eq i8* %55, null
br i1 %56, label %57, label %58
57: ; preds = %50
store i8* null, i8** %5
br label %68
58: ; preds = %50
%59 = load i64*, i64** %8
store i64 0, i64* %59
%60 = load i8*, i8** %10
store i8* %60, i8** %5
br label %68
61: ; preds = %42
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = load i64*, i64** %8
%65 = load i64, i64* %64
%66 = add i64 %65, %63
store i64 %66, i64* %64
br label %13
67: ; preds = %49
store i8* null, i8** %5
br label %68
68: ; preds = %67, %58, %57, %41, %29, %20
%69 = load i8*, i8** %5
ret i8* %69
}

LLVM IR (-O2):
@ENOBUFS = global i32 0
@errno = global i32 0
define i8* @read_field(i32 %0, i8* %1, i64* %2, i64 %3) {
%5 = tail call i8* @pop_buffer(i8* %1, i64* %2, i64 %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %29
7: ; preds = %4, %23
%8 = load i64, i64* %2
%9 = sub i64 %3, %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = load i32, i32* @ENOBUFS
store i32 %12, i32* @errno
br label %29
13: ; preds = %7
%14 = getelementptr inbounds i8, i8* %1, i64 %8
%15 = tail call i32 @read(i32 %0, i8* %14, i64 %9)
switch i32 %15, label %23 [
i32 -1, label %29
i32 0, label %16
]
16: ; preds = %13
%17 = load i64, i64* %2
%18 = icmp eq i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %16
%20 = tail call i8* @strndup(i8* %1, i64 %17)
%21 = icmp eq i8* %20, null
br i1 %21, label %29, label %22
22: ; preds = %19
store i64 0, i64* %2
br label %29
23: ; preds = %13
%24 = sext i32 %15 to i64
%25 = load i64, i64* %2
%26 = add i64 %25, %24
store i64 %26, i64* %2
%27 = tail call i8* @pop_buffer(i8* %1, i64* %2, i64 %3)
%28 = icmp eq i8* %27, null
br i1 %28, label %7, label %29
29: ; preds = %23, %13, %4, %19, %22, %11, %16
%30 = phi i8* [ null, %16 ], [ null, %19 ], [ %20, %22 ], [ null, %11 ], [ %5, %4 ], [ %27, %23 ], [ null, %13 ]
ret i8* %30
}