#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 katcp_dispatch {
  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*/ *);
int dispatch_katcp(struct katcp_dispatch *);
int /*<<< orphan*/ errno;
scalar_t__ exited_katcp(struct katcp_dispatch *);
scalar_t__ flushing_katcp(struct katcp_dispatch *);
int /*<<< orphan*/ name_katcp(struct katcp_dispatch *, char *, char *, int);
int net_connect(char *, int, int /*<<< orphan*/);
int read_katcp(struct katcp_dispatch *);
int /*<<< orphan*/ reset_katcp(struct katcp_dispatch *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int terminate_katcp(struct katcp_dispatch *, int);
int write_katcp(struct katcp_dispatch *);
int run_client_katcp(struct katcp_dispatch *d, char *host, int port) {
  int fd, result;
  fd_set fsr, fsw;
  fd = net_connect(host, port, 0);
  if (fd < 0) {
    return terminate_katcp(d, 10);
  }
  reset_katcp(d, fd);
  name_katcp(d, "%s:%d", host, port);
  while (exited_katcp(d) == 0) {
    FD_ZERO(&fsr);
    FD_ZERO(&fsw);
    FD_SET(fd, &fsr);
    if (flushing_katcp(d)) {
      FD_SET(fd, &fsw);
    }
    result = select(fd + 1, &fsr, &fsw, NULL, NULL);
    switch (result) {
    case -1:
      switch (errno) {
      case EAGAIN:
      case EINTR:
        continue;
      default:
        return terminate_katcp(d, 10);
      }
      break;
    }
    if (FD_ISSET(fd, &fsr)) {
      result = read_katcp(d);
      if (result) {
        if (result < 0) {
          return terminate_katcp(d, 10);
        } else {
          terminate_katcp(d, 2);
        }
      }
    }
    result = dispatch_katcp(d);
    if (result < 0) {
      return terminate_katcp(d, 10);
    }
    if (FD_ISSET(fd, &fsw)) {
      result = write_katcp(d);
      if (result < 0) {
        return terminate_katcp(d, 10);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_client_katcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1080 <net_connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1214 <run_client_katcp+0x44>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <terminate_katcp@plt>
mov    %eax,-0x4(%rbp)
jmp    13ce <run_client_katcp+0x1fe>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   10b0 <reset_katcp@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xdce(%rip),%rsi
call   10c0 <name_katcp@plt>
mov    -0x10(%rbp),%rdi
call   10d0 <exited_katcp@plt>
cmp    $0x0,%rax
jne    13c7 <run_client_katcp+0x1f7>
lea    -0x28(%rbp),%rdi
call   1090 <FD_ZERO@plt>
lea    -0x2c(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x10(%rbp),%rdi
call   1050 <flushing_katcp@plt>
cmp    $0x0,%rax
je     1287 <run_client_katcp+0xb7>
mov    -0x20(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
mov    %r8,%rcx
call   10a0 <select@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
sub    $0xffffffff,%eax
jne    12eb <run_client_katcp+0x11b>
jmp    12b6 <run_client_katcp+0xe6>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     12d5 <run_client_katcp+0x105>
jmp    12d0 <run_client_katcp+0x100>
jmp    1237 <run_client_katcp+0x67>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <terminate_katcp@plt>
mov    %eax,-0x4(%rbp)
jmp    13ce <run_client_katcp+0x1fe>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <FD_ISSET@plt>
cmp    $0x0,%rax
je     134f <run_client_katcp+0x17f>
mov    -0x10(%rbp),%rdi
call   1070 <read_katcp@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     134a <run_client_katcp+0x17a>
cmpl   $0x0,-0x24(%rbp)
jge    1337 <run_client_katcp+0x167>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <terminate_katcp@plt>
mov    %eax,-0x4(%rbp)
jmp    13ce <run_client_katcp+0x1fe>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1030 <terminate_katcp@plt>
jmp    134a <run_client_katcp+0x17a>
jmp    134f <run_client_katcp+0x17f>
mov    -0x10(%rbp),%rdi
call   10e0 <dispatch_katcp@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    137b <run_client_katcp+0x1ab>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <terminate_katcp@plt>
mov    %eax,-0x4(%rbp)
jmp    13ce <run_client_katcp+0x1fe>
mov    -0x20(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1060 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13c2 <run_client_katcp+0x1f2>
mov    -0x10(%rbp),%rdi
call   1040 <write_katcp@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    13bd <run_client_katcp+0x1ed>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <terminate_katcp@plt>
mov    %eax,-0x4(%rbp)
jmp    13ce <run_client_katcp+0x1fe>
jmp    13c2 <run_client_katcp+0x1f2>
jmp    1237 <run_client_katcp+0x67>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_client_katcp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbx
xor    %r14d,%r14d
mov    %rsi,%rdi
mov    %edx,%esi
xor    %edx,%edx
call   1080 <net_connect@plt>
test   %eax,%eax
js     1327 <run_client_katcp+0x157>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   10b0 <reset_katcp@plt>
lea    0xdf2(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %r15d,%ecx
call   10c0 <name_katcp@plt>
mov    %rbx,%rdi
call   10d0 <exited_katcp@plt>
test   %rax,%rax
jne    1345 <run_client_katcp+0x175>
mov    %ebp,%r14d
add    $0x1,%r14d
lea    0x4(%rsp),%r15
mov    %rsp,%r12
mov    0x2d9d(%rip),%r13
jmp    1273 <run_client_katcp+0xa3>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    1315 <run_client_katcp+0x145>
mov    %rbx,%rdi
call   10d0 <exited_katcp@plt>
test   %rax,%rax
jne    1342 <run_client_katcp+0x172>
mov    %r15,%rdi
call   1090 <FD_ZERO@plt>
mov    %r12,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   10f0 <FD_SET@plt>
mov    %rbx,%rdi
call   1050 <flushing_katcp@plt>
test   %rax,%rax
je     12a4 <run_client_katcp+0xd4>
mov    %ebp,%edi
mov    %r12,%rsi
call   10f0 <FD_SET@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
cmp    $0xffffffff,%eax
je     1250 <run_client_katcp+0x80>
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     12e6 <run_client_katcp+0x116>
mov    %rbx,%rdi
call   1070 <read_katcp@plt>
test   %eax,%eax
je     12e6 <run_client_katcp+0x116>
js     1315 <run_client_katcp+0x145>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <terminate_katcp@plt>
mov    %rbx,%rdi
call   10e0 <dispatch_katcp@plt>
test   %eax,%eax
js     1315 <run_client_katcp+0x145>
mov    %ebp,%edi
mov    %r12,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1262 <run_client_katcp+0x92>
mov    %rbx,%rdi
call   1040 <write_katcp@plt>
test   %eax,%eax
jns    1262 <run_client_katcp+0x92>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <terminate_katcp@plt>
mov    %eax,%r14d
jmp    1345 <run_client_katcp+0x175>
mov    %rbx,%rdi
mov    $0xa,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <terminate_katcp@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.katcp_dispatch = type { i32 }
@.str = [6 x i8] c"%s:%d\00"
@errno = global i32 0
define i32 @run_client_katcp(%struct.katcp_dispatch* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.katcp_dispatch*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store %struct.katcp_dispatch* %0, %struct.katcp_dispatch** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = call i32 @net_connect(i8* %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%19 = call i32 @terminate_katcp(%struct.katcp_dispatch* %18, i32 10)
store i32 %19, i32* %4
br label %97
20: ; preds = %3
%21 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%22 = load i32, i32* %8
%23 = call i32 @reset_katcp(%struct.katcp_dispatch* %21, i32 %22)
%24 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @name_katcp(%struct.katcp_dispatch* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %26)
br label %28
28: ; preds = %95, %50, %20
%29 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%30 = call i64 @exited_katcp(%struct.katcp_dispatch* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %96
32: ; preds = %28
%33 = call i32 @FD_ZERO(i32* %10)
%34 = call i32 @FD_ZERO(i32* %11)
%35 = load i32, i32* %8
%36 = call i32 @FD_SET(i32 %35, i32* %10)
%37 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%38 = call i64 @flushing_katcp(%struct.katcp_dispatch* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* %8
%42 = call i32 @FD_SET(i32 %41, i32* %11)
br label %43
43: ; preds = %40, %32
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
%46 = call i32 @select(i32 %45, i32* %10, i32* %11, i32* null, i32* null)
store i32 %46, i32* %9
%47 = load i32, i32* %9
switch i32 %47, label %54 [
i32 -1, label %48
]
48: ; preds = %43
%49 = load i32, i32* @errno
switch i32 %49, label %51 [
i32 129, label %50
i32 128, label %50
]
50: ; preds = %48, %48
br label %28
51: ; preds = %48
%52 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%53 = call i32 @terminate_katcp(%struct.katcp_dispatch* %52, i32 10)
store i32 %53, i32* %4
br label %97
54: ; preds = %43
%55 = load i32, i32* %8
%56 = call i64 @FD_ISSET(i32 %55, i32* %10)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %74
58: ; preds = %54
%59 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%60 = call i32 @read_katcp(%struct.katcp_dispatch* %59)
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %73
63: ; preds = %58
%64 = load i32, i32* %9
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%68 = call i32 @terminate_katcp(%struct.katcp_dispatch* %67, i32 10)
store i32 %68, i32* %4
br label %97
69: ; preds = %63
%70 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%71 = call i32 @terminate_katcp(%struct.katcp_dispatch* %70, i32 2)
br label %72
72: ; preds = %69
br label %73
73: ; preds = %72, %58
br label %74
74: ; preds = %73, %54
%75 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%76 = call i32 @dispatch_katcp(%struct.katcp_dispatch* %75)
store i32 %76, i32* %9
%77 = load i32, i32* %9
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %74
%80 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%81 = call i32 @terminate_katcp(%struct.katcp_dispatch* %80, i32 10)
store i32 %81, i32* %4
br label %97
82: ; preds = %74
%83 = load i32, i32* %8
%84 = call i64 @FD_ISSET(i32 %83, i32* %11)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %95
86: ; preds = %82
%87 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%88 = call i32 @write_katcp(%struct.katcp_dispatch* %87)
store i32 %88, i32* %9
%89 = load i32, i32* %9
%90 = icmp slt i32 %89, 0
br i1 %90, label %91, label %94
91: ; preds = %86
%92 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %5
%93 = call i32 @terminate_katcp(%struct.katcp_dispatch* %92, i32 10)
store i32 %93, i32* %4
br label %97
94: ; preds = %86
br label %95
95: ; preds = %94, %82
br label %28
96: ; preds = %28
store i32 0, i32* %4
br label %97
97: ; preds = %96, %91, %79, %66, %51, %17
%98 = load i32, i32* %4
ret i32 %98
}

LLVM IR (-O2):
%struct.katcp_dispatch = type { i32 }
@.str = [6 x i8] c"%s:%d\00"
@errno = global i32 0
define i32 @run_client_katcp(%struct.katcp_dispatch* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = tail call i32 @net_connect(i8* %1, i32 %2, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @terminate_katcp(%struct.katcp_dispatch* %0, i32 10)
br label %64
12: ; preds = %3
%13 = tail call i32 @reset_katcp(%struct.katcp_dispatch* %0, i32 %8)
%14 = tail call i32 @name_katcp(%struct.katcp_dispatch* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %2)
%15 = call i64 @exited_katcp(%struct.katcp_dispatch* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %64
17: ; preds = %12
%18 = add nuw nsw i32 %8, 1
br label %19
19: ; preds = %17, %61
%20 = call i32 @FD_ZERO(i32* %4)
%21 = call i32 @FD_ZERO(i32* %5)
%22 = call i32 @FD_SET(i32 %8, i32* %4)
%23 = call i64 @flushing_katcp(%struct.katcp_dispatch* %0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %19
%26 = call i32 @FD_SET(i32 %8, i32* %5)
br label %27
27: ; preds = %25, %19
%28 = call i32 @select(i32 %18, i32* %4, i32* %5, i32* null, i32* null)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = and i32 %31, -2
%33 = icmp eq i32 %32, 128
br i1 %33, label %61, label %34
34: ; preds = %30
%35 = call i32 @terminate_katcp(%struct.katcp_dispatch* %0, i32 10)
br label %64
36: ; preds = %27
%37 = call i64 @FD_ISSET(i32 %8, i32* %4)
%38 = icmp eq i64 %37, 0
br i1 %38, label %48, label %39
39: ; preds = %36
%40 = call i32 @read_katcp(%struct.katcp_dispatch* %0)
%41 = icmp eq i32 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %39
%43 = icmp slt i32 %40, 0
br i1 %43, label %44, label %46
44: ; preds = %42
%45 = call i32 @terminate_katcp(%struct.katcp_dispatch* %0, i32 10)
br label %64
46: ; preds = %42
%47 = call i32 @terminate_katcp(%struct.katcp_dispatch* %0, i32 2)
br label %48
48: ; preds = %39, %46, %36
%49 = call i32 @dispatch_katcp(%struct.katcp_dispatch* %0)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @terminate_katcp(%struct.katcp_dispatch* %0, i32 10)
br label %64
53: ; preds = %48
%54 = call i64 @FD_ISSET(i32 %8, i32* %5)
%55 = icmp eq i64 %54, 0
br i1 %55, label %61, label %56
56: ; preds = %53
%57 = call i32 @write_katcp(%struct.katcp_dispatch* %0)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @terminate_katcp(%struct.katcp_dispatch* %0, i32 10)
br label %64
61: ; preds = %53, %56, %30
%62 = call i64 @exited_katcp(%struct.katcp_dispatch* %0)
%63 = icmp eq i64 %62, 0
br i1 %63, label %19, label %64
64: ; preds = %61, %12, %59, %51, %44, %34, %10
%65 = phi i32 [ %11, %10 ], [ %35, %34 ], [ %45, %44 ], [ %52, %51 ], [ %60, %59 ], [ 0, %12 ], [ 0, %61 ]
ret i32 %65
}