#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 */
struct track_t {
  int dummy;
};
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int read(int, void *, size_t);
void *track_access_pcm(struct track_t *, size_t *);
int /*<<< orphan*/ track_commit(struct track_t *, int);
int read_from_pipe(int fd, struct track_t *tr) {
  for (;;) {
    void *pcm;
    size_t len;
    ssize_t z;
    pcm = track_access_pcm(tr, &len);
    if (pcm == NULL)
      return -1;
    z = read(fd, pcm, len);
    if (z == -1) {
      if (errno == EAGAIN) {
        return 0;
      } else {
        perror("read");
        return -1;
      }
    }
    if (z == 0)
      break;
    track_commit(tr, z);
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_from_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <track_access_pcm@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <read_from_pipe+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <read_from_pipe+0xb9>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11d2 <read_from_pipe+0x92>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    11ba <read_from_pipe+0x7a>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <read_from_pipe+0xb9>
lea    0xe3f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <read_from_pipe+0xb9>
cmpl   $0x0,-0x24(%rbp)
jne    11e1 <read_from_pipe+0xa1>
jmp    11f2 <read_from_pipe+0xb2>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <track_commit@plt>
jmp    114f <read_from_pipe+0xf>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_pipe>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <track_access_pcm@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11ce <read_from_pipe+0x8e>
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rsp),%rdx
mov    %ebp,%edi
mov    %rax,%rsi
call   1040 <read@plt>
test   %eax,%eax
je     11c8 <read_from_pipe+0x88>
cmp    $0xffffffff,%eax
je     11a3 <read_from_pipe+0x63>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <track_commit@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <track_access_pcm@plt>
test   %rax,%rax
jne    1170 <read_from_pipe+0x30>
jmp    11ce <read_from_pipe+0x8e>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%rax
je     11ce <read_from_pipe+0x8e>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.track_t = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [5 x i8] c"read\00"
define i32 @read_from_pipe(i32 %0, %struct.track_t* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.track_t*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %4
store %struct.track_t* %1, %struct.track_t** %5
br label %9
9: ; preds = %33, %2
%10 = load %struct.track_t*, %struct.track_t** %5
%11 = call i8* @track_access_pcm(%struct.track_t* %10, i64* %7)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 -1, i32* %3
br label %38
15: ; preds = %9
%16 = load i32, i32* %4
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = call i32 @read(i32 %16, i8* %17, i64 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %29
22: ; preds = %15
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 0, i32* %3
br label %38
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
29: ; preds = %15
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
br label %37
33: ; preds = %29
%34 = load %struct.track_t*, %struct.track_t** %5
%35 = load i32, i32* %8
%36 = call i32 @track_commit(%struct.track_t* %34, i32 %35)
br label %9
37: ; preds = %32
store i32 -1, i32* %3
br label %38
38: ; preds = %37, %27, %26, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.track_t = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [5 x i8] c"read\00"
define i32 @read_from_pipe(i32 %0, %struct.track_t* %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = call i8* @track_access_pcm(%struct.track_t* %1, i64* %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %2, %19
%8 = phi i8* [ %21, %19 ], [ %5, %2 ]
%9 = load i64, i64* %3
%10 = call i32 @read(i32 %0, i8* %8, i64 %9)
switch i32 %10, label %19 [
i32 -1, label %11
i32 0, label %23
]
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %19, %2, %15, %11
%18 = phi i32 [ 0, %11 ], [ -1, %15 ], [ -1, %2 ], [ -1, %19 ]
br label %24
19: ; preds = %7
%20 = call i32 @track_commit(%struct.track_t* %1, i32 %10)
%21 = call i8* @track_access_pcm(%struct.track_t* %1, i64* %3)
%22 = icmp eq i8* %21, null
br i1 %22, label %17, label %7
23: ; preds = %7
br label %24
24: ; preds = %17, %23
%25 = phi i32 [ -1, %23 ], [ %18, %17 ]
ret i32 %25
}