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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int recv(int, scalar_t__ *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ send_command(int, int);
int /*<<< orphan*/ stderr;
ssize_t read_passphrase(int socket, uint8_t *msg, ssize_t msg_size) {
  fd_set fds;
  struct timeval timeout;
  ssize_t result;
  for (;;) {
    fputs("\a", stderr);
    if (!send_command(socket, 1))
      return -1;
    FD_ZERO(&fds);
    FD_SET(socket, &fds);
    timeout.tv_sec = 10;
    timeout.tv_usec = 0;
    result = select(socket + 1, &fds, NULL, NULL, &timeout);
    if (result < 0 && errno != EINTR) {
      perror("select");
      return -1;
    } else if (result > 0) {
      result = recv(socket, msg, msg_size, 0);
      if (result < 0) {
        perror("recv");
        return -1;
      }
      if (msg_size > 0 && msg[0] == 0) {
        send_command(socket, 2);
        return result;
      }
    }
  }
}


x86 assembly (clang, -O0):
<read_passphrase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1070 <send_command@plt>
cmp    $0x0,%eax
jne    11b9 <read_passphrase+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <read_passphrase+0x145>
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0xa,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1236 <read_passphrase+0xc6>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
je     1236 <read_passphrase+0xc6>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <read_passphrase+0x145>
cmpl   $0x0,-0x2c(%rbp)
jle    12ab <read_passphrase+0x13b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1276 <read_passphrase+0x106>
lea    0xda4(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <read_passphrase+0x145>
cmpl   $0x0,-0x14(%rbp)
jle    12a6 <read_passphrase+0x136>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12a6 <read_passphrase+0x136>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1070 <send_command@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <read_passphrase+0x145>
jmp    12ab <read_passphrase+0x13b>
jmp    12b0 <read_passphrase+0x140>
jmp    1182 <read_passphrase+0x12>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_passphrase>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r12d
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1040 <fputs@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <send_command@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
je     128e <read_passphrase+0x11e>
lea    0x1(%rbx),%r13d
lea    0x4(%rsp),%rbp
jmp    1203 <read_passphrase+0x93>
mov    0x2e11(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    1273 <read_passphrase+0x103>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xe16(%rip),%rdi
call   1040 <fputs@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <send_command@plt>
test   %eax,%eax
je     1288 <read_passphrase+0x118>
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
movl   $0xa,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r13d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1060 <select@plt>
test   %eax,%eax
js     11c0 <read_passphrase+0x50>
je     11da <read_passphrase+0x6a>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     127c <read_passphrase+0x10c>
test   %r12d,%r12d
jle    11da <read_passphrase+0x6a>
cmpq   $0x0,(%r15)
jne    11da <read_passphrase+0x6a>
mov    %eax,%r14d
mov    %ebx,%edi
mov    $0x2,%esi
call   1070 <send_command@plt>
jmp    128e <read_passphrase+0x11e>
lea    0xd88(%rip),%rdi
jmp    1283 <read_passphrase+0x113>
lea    0xd86(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [2 x i8] c"\07\00"
@stderr = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [5 x i8] c"recv\00"
define i32 @read_passphrase(i32 %0, i64* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i32 %0, i32* %5
store i64* %1, i64** %6
store i32 %2, i32* %7
br label %11
11: ; preds = %61, %3
%12 = load i32, i32* @stderr
%13 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %5
%15 = call i32 @send_command(i32 %14, i32 1)
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %11
store i32 -1, i32* %4
br label %62
18: ; preds = %11
%19 = call i32 @FD_ZERO(i32* %8)
%20 = load i32, i32* %5
%21 = call i32 @FD_SET(i32 %20, i32* %8)
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 10, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %23
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %18
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %62
35: ; preds = %29, %18
%36 = load i32, i32* %10
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %60
38: ; preds = %35
%39 = load i32, i32* %5
%40 = load i64*, i64** %6
%41 = load i32, i32* %7
%42 = call i32 @recv(i32 %39, i64* %40, i32 %41, i32 0)
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %62
47: ; preds = %38
%48 = load i32, i32* %7
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %47
%51 = load i64*, i64** %6
%52 = getelementptr inbounds i64, i64* %51, i64 0
%53 = load i64, i64* %52
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = load i32, i32* %5
%57 = call i32 @send_command(i32 %56, i32 2)
%58 = load i32, i32* %10
store i32 %58, i32* %4
br label %62
59: ; preds = %50, %47
br label %60
60: ; preds = %59, %35
br label %61
61: ; preds = %60
br label %11
62: ; preds = %55, %45, %33, %17
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [2 x i8] c"\07\00"
@stderr = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [5 x i8] c"recv\00"
define i32 @read_passphrase(i32 %0, i64* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = load i32, i32* @stderr
%9 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @send_command(i32 %0, i32 1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %46, label %12
12: ; preds = %3
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%15 = add nsw i32 %0, 1
%16 = icmp sgt i32 %2, 0
br label %17
17: ; preds = %12, %41
%18 = call i32 @FD_ZERO(i32* %4)
%19 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 10, i32* %13
store i64 0, i64* %14
%20 = call i32 @select(i32 %15, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %41, label %26
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %46
28: ; preds = %17
%29 = icmp eq i32 %20, 0
br i1 %29, label %41, label %30
30: ; preds = %28
%31 = call i32 @recv(i32 %0, i64* %1, i32 %2, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %46
35: ; preds = %30
br i1 %16, label %36, label %41
36: ; preds = %35
%37 = load i64, i64* %1
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @send_command(i32 %0, i32 2)
br label %46
41: ; preds = %22, %28, %36, %35
%42 = load i32, i32* @stderr
%43 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = call i32 @send_command(i32 %0, i32 1)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %17
46: ; preds = %41, %3, %39, %33, %26
%47 = phi i32 [ -1, %26 ], [ -1, %33 ], [ %31, %39 ], [ -1, %3 ], [ -1, %41 ]
ret i32 %47
}