#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 /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
#define EINTR 128
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ LOG_CRIT;
int /*<<< orphan*/ LOG_NOTICE;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ write(int, unsigned char *, int);
int worker_passive(int requestfd, int passivefd) {
  int nread;
  unsigned char buf[64];
  int nready;
  int fdmaxp1;
  fd_set readset;
  FD_ZERO(&readset);
  fdmaxp1 = (requestfd > passivefd ? requestfd : passivefd) + 1;
  syslog(LOG_NOTICE, "CHAP proxy passive worker");
  for (;;) {
    FD_SET(requestfd, &readset);
    FD_SET(passivefd, &readset);
    if ((nready = select(fdmaxp1, &readset, NULL, NULL, NULL)) < 0) {
      switch (errno) {
      case EINTR:
        continue;
        break;
      default:
        fprintf(stderr, "select: %m");
        syslog(LOG_CRIT, "select: %m");
        exit(EXIT_FAILURE);
      }
    }
    if (FD_ISSET(requestfd, &readset)) {
      if ((nread = read(requestfd, buf, sizeof(buf))) <= 0) {
        if (nread == 0) {
          exit(0);
        }
        switch (errno) {
        case EINTR:
          continue;
          break;
        default:
          exit(EXIT_SUCCESS);
        }
      }
      write(passivefd, buf, nread);
    }
    if (FD_ISSET(passivefd, &readset)) {
      if ((nread = read(passivefd, buf, sizeof(buf))) <= 0) {
        if (nread == 0) {
          exit(0);
        }
        switch (errno) {
        case EINTR:
          continue;
          break;
        default:
          exit(EXIT_SUCCESS);
        }
      }
      write(requestfd, buf, nread);
    }
  }
}


x86 assembly (clang, -O0):
<worker_passive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x5c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    11be <worker_passive+0x2e>
mov    -0x4(%rbp),%eax
mov    %eax,-0x60(%rbp)
jmp    11c4 <worker_passive+0x34>
mov    -0x8(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1090 <syslog@plt>
mov    -0x4(%rbp),%edi
lea    -0x5c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x5c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x58(%rbp),%edi
lea    -0x5c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jge    1273 <worker_passive+0xe3>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    123b <worker_passive+0xab>
jmp    1236 <worker_passive+0xa6>
jmp    11e2 <worker_passive+0x52>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1090 <syslog@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x5c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12f2 <worker_passive+0x162>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     12e3 <worker_passive+0x153>
cmpl   $0x0,-0xc(%rbp)
jne    12b7 <worker_passive+0x127>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    12d5 <worker_passive+0x145>
jmp    12d0 <worker_passive+0x140>
jmp    11e2 <worker_passive+0x52>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x5c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1371 <worker_passive+0x1e1>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     1362 <worker_passive+0x1d2>
cmpl   $0x0,-0xc(%rbp)
jne    1336 <worker_passive+0x1a6>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2c93(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1354 <worker_passive+0x1c4>
jmp    134f <worker_passive+0x1bf>
jmp    11e2 <worker_passive+0x52>
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
jmp    11e2 <worker_passive+0x52>

x86 assembly (clang, -O2):
<worker_passive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
cmp    %r14d,%ebp
mov    %r14d,%r15d
cmovg  %ebp,%r15d
add    $0x1,%r15d
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1090 <syslog@plt>
mov    0x2df6(%rip),%r13
lea    0x10(%rsp),%r12
jmp    11fe <worker_passive+0x6e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpl   $0x80,0x0(%r13)
jne    12ae <worker_passive+0x11e>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %eax,%eax
js     11f0 <worker_passive+0x60>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1258 <worker_passive+0xc8>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x40,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1290 <worker_passive+0x100>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11fe <worker_passive+0x6e>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x40,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1290 <worker_passive+0x100>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11fe <worker_passive+0x6e>
nopl   (%rax)
je     12e5 <worker_passive+0x155>
cmpl   $0x80,0x0(%r13)
je     11fe <worker_passive+0x6e>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rbx
mov    %rbx,%rsi
call   1060 <fprintf@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1090 <syslog@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@LOG_NOTICE = global i32 0
@.str = [26 x i8] c"CHAP proxy passive worker\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [11 x i8] c"select: %m\00"
@LOG_CRIT = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @worker_passive(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = call i32 @FD_ZERO(i32* %9)
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %3
br label %18
16: ; preds = %2
%17 = load i32, i32* %4
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %15, %14 ], [ %17, %16 ]
%20 = add nsw i32 %19, 1
store i32 %20, i32* %8
%21 = load i32, i32* @LOG_NOTICE
%22 = call i32 @syslog(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %91, %82, %57, %33, %18
%24 = load i32, i32* %3
%25 = call i32 @FD_SET(i32 %24, i32* %9)
%26 = load i32, i32* %4
%27 = call i32 @FD_SET(i32 %26, i32* %9)
%28 = load i32, i32* %8
%29 = call i32 @select(i32 %28, i32* %9, i32* null, i32* null, i32* null)
store i32 %29, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %23
%32 = load i32, i32* @errno
switch i32 %32, label %34 [
i32 128, label %33
]
33: ; preds = %31
br label %23
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @LOG_CRIT
%38 = call i32 @syslog(i32 %37, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %23
%42 = load i32, i32* %3
%43 = call i64 @FD_ISSET(i32 %42, i32* %9)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %66
45: ; preds = %41
%46 = load i32, i32* %3
%47 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 64)
store i32 %48, i32* %5
%49 = icmp sle i32 %48, 0
br i1 %49, label %50, label %61
50: ; preds = %45
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %50
%56 = load i32, i32* @errno
switch i32 %56, label %58 [
i32 128, label %57
]
57: ; preds = %55
br label %23
58: ; preds = %55
%59 = load i32, i32* @EXIT_SUCCESS
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %45
%62 = load i32, i32* %4
%63 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%64 = load i32, i32* %5
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
br label %66
66: ; preds = %61, %41
%67 = load i32, i32* %4
%68 = call i64 @FD_ISSET(i32 %67, i32* %9)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %91
70: ; preds = %66
%71 = load i32, i32* %4
%72 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%73 = call i32 @read(i32 %71, i8* %72, i32 64)
store i32 %73, i32* %5
%74 = icmp sle i32 %73, 0
br i1 %74, label %75, label %86
75: ; preds = %70
%76 = load i32, i32* %5
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %75
%79 = call i32 @exit(i32 0)
unreachable
80: ; preds = %75
%81 = load i32, i32* @errno
switch i32 %81, label %83 [
i32 128, label %82
]
82: ; preds = %80
br label %23
83: ; preds = %80
%84 = load i32, i32* @EXIT_SUCCESS
%85 = call i32 @exit(i32 %84)
unreachable
86: ; preds = %70
%87 = load i32, i32* %3
%88 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%89 = load i32, i32* %5
%90 = call i32 @write(i32 %87, i8* %88, i32 %89)
br label %91
91: ; preds = %86, %66
br label %23
}

LLVM IR (-O2):
@LOG_NOTICE = global i32 0
@.str = [26 x i8] c"CHAP proxy passive worker\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [11 x i8] c"select: %m\00"
@LOG_CRIT = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @worker_passive(i32 %0, i32 %1) {
%3 = alloca [64 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = call i32 @FD_ZERO(i32* %4)
%8 = icmp sgt i32 %0, %1
%9 = select i1 %8, i32 %0, i32 %1
%10 = add nsw i32 %9, 1
%11 = load i32, i32* @LOG_NOTICE
%12 = call i32 @syslog(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %49, %2
%14 = call i32 @FD_SET(i32 %0, i32* %4)
%15 = call i32 @FD_SET(i32 %1, i32* %4)
%16 = call i32 @select(i32 %10, i32* %4, i32* null, i32* null, i32* null)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = icmp eq i32 %19, 128
br i1 %20, label %49, label %21
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @LOG_CRIT
%25 = call i32 @syslog(i32 %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %13
%29 = call i64 @FD_ISSET(i32 %0, i32* %4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %46, label %31
31: ; preds = %28
%32 = call i32 @read(i32 %0, i8* %5, i32 64)
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %44
34: ; preds = %31
%35 = icmp eq i32 %32, 0
br i1 %35, label %36, label %38
36: ; preds = %34
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = icmp eq i32 %39, 128
br i1 %40, label %49, label %41
41: ; preds = %38
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %31
%45 = call i32 @write(i32 %1, i8* %5, i32 %32)
br label %46
46: ; preds = %44, %28
%47 = call i64 @FD_ISSET(i32 %1, i32* %4)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %46, %63, %18, %38, %57
br label %13
50: ; preds = %46
%51 = call i32 @read(i32 %1, i8* %5, i32 64)
%52 = icmp slt i32 %51, 1
br i1 %52, label %53, label %63
53: ; preds = %50
%54 = icmp eq i32 %51, 0
br i1 %54, label %55, label %57
55: ; preds = %53
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %53
%58 = load i32, i32* @errno
%59 = icmp eq i32 %58, 128
br i1 %59, label %49, label %60
60: ; preds = %57
%61 = load i32, i32* @EXIT_SUCCESS
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %50
%64 = call i32 @write(i32 %0, i8* %5, i32 %51)
br label %49
}