#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 timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
struct katcl_line {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
#define EAGAIN 129
#define EINTR 128
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ arg_reply_katcl(struct katcl_line *);
scalar_t__ cmp_time_katcp(struct timeval *, struct timeval *);
int /*<<< orphan*/ errno;
int fileno_katcl(struct katcl_line *);
scalar_t__ flushing_katcl(struct katcl_line *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ have_katcl(struct katcl_line *);
int read_katcl(struct katcl_line *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ sub_time_katcp(struct timeval *, struct timeval *,
                                  struct timeval *);
int write_katcl(struct katcl_line *);
int complete_rpc_katcl(struct katcl_line *l, unsigned int flags,
                       struct timeval *until) {
  fd_set fsr, fsw;
  struct timeval tv, now;
  int result;
  int fd;
  fd = fileno_katcl(l);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  for (;;) {
    if (have_katcl(l) > 0) {
      if (arg_reply_katcl(l)) {
        return 1;
      }
      return 0;
    }
    FD_ZERO(&fsr);
    FD_ZERO(&fsw);
    FD_SET(fd, &fsr);
    if (flushing_katcl(l)) {
      FD_SET(fd, &fsw);
    }
    if (until) {
      gettimeofday(&now, NULL);
      if (cmp_time_katcp(&now, until) < 0) {
        sub_time_katcp(&tv, until, &now);
      } else {
        tv.tv_sec = 0;
        tv.tv_usec = 12;
      }
      result = select(fd + 1, &fsr, &fsw, NULL, &tv);
    } else {
      result = select(fd + 1, &fsr, &fsw, NULL, NULL);
    }
    switch (result) {
    case -1:
      switch (errno) {
      case EAGAIN:
      case EINTR:
        continue;
      default:
        return -1;
      }
      break;
    case 0:
      return -1;
    }
    if (FD_ISSET(fd, &fsw)) {
      result = write_katcl(l);
      if (result < 0) {
        return -1;
      }
    }
    if (FD_ISSET(fd, &fsr)) {
      result = read_katcl(l);
      if (result) {
        return -1;
      }
    }
  }
}


x86 assembly (clang, -O0):
<complete_rpc_katcl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   10d0 <fileno_katcl@plt>
mov    %eax,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   10e0 <have_katcl@plt>
cmp    $0x0,%rax
jle    123c <complete_rpc_katcl+0x6c>
mov    -0x10(%rbp),%rdi
call   1080 <arg_reply_katcl@plt>
cmp    $0x0,%rax
je     1230 <complete_rpc_katcl+0x60>
movl   $0x1,-0x4(%rbp)
jmp    13e8 <complete_rpc_katcl+0x218>
movl   $0x0,-0x4(%rbp)
jmp    13e8 <complete_rpc_katcl+0x218>
lea    -0x24(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
lea    -0x28(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x50(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x10(%rbp),%rdi
call   1030 <flushing_katcl@plt>
cmp    $0x0,%rax
je     1279 <complete_rpc_katcl+0xa9>
mov    -0x50(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10f0 <FD_SET@plt>
cmpq   $0x0,-0x20(%rbp)
je     12f0 <complete_rpc_katcl+0x120>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    -0x20(%rbp),%rsi
lea    -0x48(%rbp),%rdi
call   10c0 <cmp_time_katcp@plt>
cmp    $0x0,%rax
jge    12be <complete_rpc_katcl+0xee>
mov    -0x20(%rbp),%rsi
lea    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rdx
call   1090 <sub_time_katcp@plt>
jmp    12cd <complete_rpc_katcl+0xfd>
movq   $0x0,-0x30(%rbp)
movl   $0xc,-0x38(%rbp)
mov    -0x50(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
call   10b0 <select@plt>
mov    %eax,-0x4c(%rbp)
jmp    130e <complete_rpc_katcl+0x13e>
mov    -0x50(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   10b0 <select@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x54(%rbp)
sub    $0xffffffff,%eax
je     1332 <complete_rpc_katcl+0x162>
jmp    1322 <complete_rpc_katcl+0x152>
mov    -0x54(%rbp),%eax
test   %eax,%eax
je     135d <complete_rpc_katcl+0x18d>
jmp    1369 <complete_rpc_katcl+0x199>
mov    0x2ca7(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     1351 <complete_rpc_katcl+0x181>
jmp    134c <complete_rpc_katcl+0x17c>
jmp    11fe <complete_rpc_katcl+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e8 <complete_rpc_katcl+0x218>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e8 <complete_rpc_katcl+0x218>
mov    -0x50(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13a6 <complete_rpc_katcl+0x1d6>
mov    -0x10(%rbp),%rdi
call   1040 <write_katcl@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    13a1 <complete_rpc_katcl+0x1d1>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e8 <complete_rpc_katcl+0x218>
jmp    13a6 <complete_rpc_katcl+0x1d6>
mov    -0x50(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13e3 <complete_rpc_katcl+0x213>
mov    -0x10(%rbp),%rdi
call   1060 <read_katcl@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     13de <complete_rpc_katcl+0x20e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e8 <complete_rpc_katcl+0x218>
jmp    13e3 <complete_rpc_katcl+0x213>
jmp    11fe <complete_rpc_katcl+0x2e>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<complete_rpc_katcl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
mov    %rdi,%r12
call   10d0 <fileno_katcl@plt>
mov    %eax,%r13d
movq   $0x0,0x20(%rsp)
movl   $0x0,0x18(%rsp)
mov    %r12,%rdi
call   10e0 <have_katcl@plt>
test   %rax,%rax
jle    122e <complete_rpc_katcl+0x5e>
mov    %r12,%rdi
call   1080 <arg_reply_katcl@plt>
xor    %r15d,%r15d
test   %rax,%rax
setne  %r15b
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13d,%eax
add    $0x1,%eax
mov    %eax,0x10(%rsp)
lea    0x14(%rsp),%rbp
lea    0x28(%rsp),%rbx
jmp    127a <complete_rpc_katcl+0xaa>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    121c <complete_rpc_katcl+0x4c>
mov    %r12,%rdi
call   10e0 <have_katcl@plt>
test   %rax,%rax
lea    0x28(%rsp),%rbx
lea    0x14(%rsp),%rbp
jg     120a <complete_rpc_katcl+0x3a>
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   10a0 <FD_ZERO@plt>
mov    %rbp,%rdi
call   10a0 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %r15,%rsi
call   10f0 <FD_SET@plt>
mov    %r12,%rdi
call   1030 <flushing_katcl@plt>
test   %rax,%rax
je     12b2 <complete_rpc_katcl+0xe2>
mov    %r13d,%edi
mov    %rbp,%rsi
call   10f0 <FD_SET@plt>
test   %r14,%r14
je     12f0 <complete_rpc_katcl+0x120>
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10c0 <cmp_time_katcp@plt>
test   %rax,%rax
js     1310 <complete_rpc_katcl+0x140>
movq   $0x0,0x20(%rsp)
movl   $0xc,0x18(%rsp)
lea    0x18(%rsp),%r15
jmp    1323 <complete_rpc_katcl+0x153>
nopl   0x0(%rax)
mov    0x10(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
jmp    1334 <complete_rpc_katcl+0x164>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x18(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1090 <sub_time_katcp@plt>
mov    0x10(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
mov    %r15,%r8
call   10b0 <select@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     1250 <complete_rpc_katcl+0x80>
test   %eax,%eax
je     121c <complete_rpc_katcl+0x4c>
mov    %r13d,%edi
lea    0x14(%rsp),%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1372 <complete_rpc_katcl+0x1a2>
mov    %r12,%rdi
call   1040 <write_katcl@plt>
test   %eax,%eax
js     121c <complete_rpc_katcl+0x4c>
mov    %r13d,%edi
lea    0xc(%rsp),%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1263 <complete_rpc_katcl+0x93>
mov    %r12,%rdi
call   1060 <read_katcl@plt>
test   %eax,%eax
je     1263 <complete_rpc_katcl+0x93>
jmp    121c <complete_rpc_katcl+0x4c>

LLVM IR (-O0):
%struct.katcl_line = type { i32 }
%struct.timeval = type { i32, i64 }
@errno = global i32 0
define i32 @complete_rpc_katcl(%struct.katcl_line* %0, i32 %1, %struct.timeval* %2) {
%4 = alloca i32
%5 = alloca %struct.katcl_line*
%6 = alloca i32
%7 = alloca %struct.timeval*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca i32
store %struct.katcl_line* %0, %struct.katcl_line** %5
store i32 %1, i32* %6
store %struct.timeval* %2, %struct.timeval** %7
%14 = load %struct.katcl_line*, %struct.katcl_line** %5
%15 = call i32 @fileno_katcl(%struct.katcl_line* %14)
store i32 %15, i32* %13
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 0, i32* %17
br label %18
18: ; preds = %90, %65, %3
%19 = load %struct.katcl_line*, %struct.katcl_line** %5
%20 = call i64 @have_katcl(%struct.katcl_line* %19)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load %struct.katcl_line*, %struct.katcl_line** %5
%24 = call i64 @arg_reply_katcl(%struct.katcl_line* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 1, i32* %4
br label %91
27: ; preds = %22
store i32 0, i32* %4
br label %91
28: ; preds = %18
%29 = call i32 @FD_ZERO(i32* %8)
%30 = call i32 @FD_ZERO(i32* %9)
%31 = load i32, i32* %13
%32 = call i32 @FD_SET(i32 %31, i32* %8)
%33 = load %struct.katcl_line*, %struct.katcl_line** %5
%34 = call i64 @flushing_katcl(%struct.katcl_line* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = load i32, i32* %13
%38 = call i32 @FD_SET(i32 %37, i32* %9)
br label %39
39: ; preds = %36, %28
%40 = load %struct.timeval*, %struct.timeval** %7
%41 = icmp ne %struct.timeval* %40, null
br i1 %41, label %42, label %57
42: ; preds = %39
%43 = call i32 @gettimeofday(%struct.timeval* %11, i32* null)
%44 = load %struct.timeval*, %struct.timeval** %7
%45 = call i64 @cmp_time_katcp(%struct.timeval* %11, %struct.timeval* %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load %struct.timeval*, %struct.timeval** %7
%49 = call i32 @sub_time_katcp(%struct.timeval* %10, %struct.timeval* %48, %struct.timeval* %11)
br label %53
50: ; preds = %42
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %51
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 12, i32* %52
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %13
%55 = add nsw i32 %54, 1
%56 = call i32 @select(i32 %55, i32* %8, i32* %9, i32* null, %struct.timeval* %10)
store i32 %56, i32* %12
br label %61
57: ; preds = %39
%58 = load i32, i32* %13
%59 = add nsw i32 %58, 1
%60 = call i32 @select(i32 %59, i32* %8, i32* %9, i32* null, %struct.timeval* null)
store i32 %60, i32* %12
br label %61
61: ; preds = %57, %53
%62 = load i32, i32* %12
switch i32 %62, label %68 [
i32 -1, label %63
i32 0, label %67
]
63: ; preds = %61
%64 = load i32, i32* @errno
switch i32 %64, label %66 [
i32 129, label %65
i32 128, label %65
]
65: ; preds = %63, %63
br label %18
66: ; preds = %63
store i32 -1, i32* %4
br label %91
67: ; preds = %61
store i32 -1, i32* %4
br label %91
68: ; preds = %61
%69 = load i32, i32* %13
%70 = call i64 @FD_ISSET(i32 %69, i32* %9)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %79
72: ; preds = %68
%73 = load %struct.katcl_line*, %struct.katcl_line** %5
%74 = call i32 @write_katcl(%struct.katcl_line* %73)
store i32 %74, i32* %12
%75 = load i32, i32* %12
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %72
store i32 -1, i32* %4
br label %91
78: ; preds = %72
br label %79
79: ; preds = %78, %68
%80 = load i32, i32* %13
%81 = call i64 @FD_ISSET(i32 %80, i32* %8)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %90
83: ; preds = %79
%84 = load %struct.katcl_line*, %struct.katcl_line** %5
%85 = call i32 @read_katcl(%struct.katcl_line* %84)
store i32 %85, i32* %12
%86 = load i32, i32* %12
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %83
store i32 -1, i32* %4
br label %91
89: ; preds = %83
br label %90
90: ; preds = %89, %79
br label %18
91: ; preds = %88, %77, %67, %66, %27, %26
%92 = load i32, i32* %4
ret i32 %92
}

LLVM IR (-O2):
%struct.katcl_line = type { i32 }
%struct.timeval = type { i32, i64 }
@errno = global i32 0
define i32 @complete_rpc_katcl(%struct.katcl_line* %0, i32 %1, %struct.timeval* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast %struct.timeval* %6 to i8*
%11 = bitcast %struct.timeval* %7 to i8*
%12 = tail call i32 @fileno_katcl(%struct.katcl_line* %0)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 0, i32* %14
%15 = call i64 @have_katcl(%struct.katcl_line* %0)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = icmp eq %struct.timeval* %2, null
%19 = add nsw i32 %12, 1
br label %24
20: ; preds = %62, %3
%21 = call i64 @arg_reply_katcl(%struct.katcl_line* %0)
%22 = icmp ne i64 %21, 0
%23 = zext i1 %22 to i32
br label %65
24: ; preds = %17, %62
%25 = call i32 @FD_ZERO(i32* %4)
%26 = call i32 @FD_ZERO(i32* %5)
%27 = call i32 @FD_SET(i32 %12, i32* %4)
%28 = call i64 @flushing_katcl(%struct.katcl_line* %0)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = call i32 @FD_SET(i32 %12, i32* %5)
br label %32
32: ; preds = %30, %24
br i1 %18, label %42, label %33
33: ; preds = %32
%34 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%35 = call i64 @cmp_time_katcp(%struct.timeval* %7, %struct.timeval* %2)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @sub_time_katcp(%struct.timeval* %6, %struct.timeval* %2, %struct.timeval* %7)
br label %40
39: ; preds = %33
store i64 0, i64* %13
store i32 12, i32* %14
br label %40
40: ; preds = %39, %37
%41 = call i32 @select(i32 %19, i32* %4, i32* %5, i32* null, %struct.timeval* %6)
br label %44
42: ; preds = %32
%43 = call i32 @select(i32 %19, i32* %4, i32* %5, i32* null, %struct.timeval* null)
br label %44
44: ; preds = %42, %40
%45 = phi i32 [ %41, %40 ], [ %43, %42 ]
switch i32 %45, label %50 [
i32 -1, label %46
i32 0, label %65
]
46: ; preds = %44
%47 = load i32, i32* @errno
%48 = and i32 %47, -2
%49 = icmp eq i32 %48, 128
br i1 %49, label %62, label %65
50: ; preds = %44
%51 = call i64 @FD_ISSET(i32 %12, i32* %5)
%52 = icmp eq i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %50
%54 = call i32 @write_katcl(%struct.katcl_line* %0)
%55 = icmp slt i32 %54, 0
br i1 %55, label %65, label %56
56: ; preds = %53, %50
%57 = call i64 @FD_ISSET(i32 %12, i32* %4)
%58 = icmp eq i64 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %56
%60 = call i32 @read_katcl(%struct.katcl_line* %0)
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %56, %59, %46
%63 = call i64 @have_katcl(%struct.katcl_line* %0)
%64 = icmp sgt i64 %63, 0
br i1 %64, label %20, label %24
65: ; preds = %59, %53, %44, %46, %20
%66 = phi i32 [ %23, %20 ], [ -1, %46 ], [ -1, %44 ], [ -1, %53 ], [ -1, %59 ]
ret i32 %66
}