#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 ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int otp_read(int fd, char *buf, size_t len) {
  ssize_t n;
  size_t nread = 0;
  while (nread < len) {
    if ((n = read(fd, &buf[nread], len - nread)) == -1) {
      if (errno == EINTR) {
        continue;
      } else {
        fprintf(stderr, "otpauth: read: %s\n", strerror(errno));
        (void)close(fd);
        return -1;
      }
    }
    if (!n) {
      fprintf(stderr, "otpauth: disconnect\n");
      (void)close(fd);
      return -1;
    }
    nread += n;
  }
  return nread;
}


x86 assembly (clang, -O0):
<otp_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1235 <otp_read+0xf5>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11ef <otp_read+0xaf>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    11ac <otp_read+0x6c>
jmp    115b <otp_read+0x1b>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <otp_read+0xfc>
cmpl   $0x0,-0x1c(%rbp)
jne    1224 <otp_read+0xe4>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <otp_read+0xfc>
movslq -0x1c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    115b <otp_read+0x1b>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<otp_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     11bc <otp_read+0x7c>
mov    %edi,%r13d
xor    %r14d,%r14d
mov    0x2e78(%rip),%r12
mov    0x2e69(%rip),%r15
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    119e <otp_read+0x5e>
mov    (%r12),%rdi
cmp    (%r15),%rdi
je     1180 <otp_read+0x40>
jmp    11c1 <otp_read+0x81>
test   %eax,%eax
je     11e4 <otp_read+0xa4>
cltq
add    %rax,%r14
mov    0x8(%rsp),%rsi
lea    (%rsi,%r14,1),%rbp
mov    0x10(%rsp),%rbx
sub    %r14,%rbx
ja     1180 <otp_read+0x40>
jmp    1209 <otp_read+0xc9>
xor    %r14d,%r14d
jmp    1209 <otp_read+0xc9>
mov    0x2e18(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe2a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11fb <otp_read+0xbb>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r13d,%edi
call   1030 <close@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):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [19 x i8] c"otpauth: read: %s\0A\00"
@.str.1 = [21 x i8] c"otpauth: disconnect\0A\00"
define i32 @otp_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %9
br label %10
10: ; preds = %44, %28, %3
%11 = load i64, i64* %9
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %49
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %9
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %9
%21 = sub i64 %19, %20
%22 = call i32 @read(i32 %15, i8* %18, i64 %21)
store i32 %22, i32* %8
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %36
24: ; preds = %14
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
br label %10
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i64, i64* @errno
%32 = call i8* @strerror(i64 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %4
br label %52
36: ; preds = %14
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%42 = load i32, i32* %5
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %4
br label %52
44: ; preds = %36
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = load i64, i64* %9
%48 = add i64 %47, %46
store i64 %48, i64* %9
br label %10
49: ; preds = %10
%50 = load i64, i64* %9
%51 = trunc i64 %50 to i32
store i32 %51, i32* %4
br label %52
52: ; preds = %49, %39, %29
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [19 x i8] c"otpauth: read: %s\0A\00"
@.str.1 = [21 x i8] c"otpauth: disconnect\0A\00"
define i32 @otp_read(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %32, label %5
5: ; preds = %3, %24
%6 = phi i64 [ %29, %24 ], [ %2, %3 ]
%7 = phi i8* [ %28, %24 ], [ %1, %3 ]
%8 = phi i64 [ %26, %24 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %11
%10 = tail call i32 @read(i32 %0, i8* %7, i64 %6)
switch i32 %10, label %24 [
i32 -1, label %11
i32 0, label %20
]
11: ; preds = %9
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %9, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i8* @strerror(i64 %12)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = tail call i32 @close(i32 %0)
br label %32
20: ; preds = %9
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @close(i32 %0)
br label %32
24: ; preds = %9
%25 = sext i32 %10 to i64
%26 = add i64 %8, %25
%27 = icmp ult i64 %26, %2
%28 = getelementptr inbounds i8, i8* %1, i64 %26
%29 = sub i64 %2, %26
br i1 %27, label %5, label %30
30: ; preds = %24
%31 = trunc i64 %26 to i32
br label %32
32: ; preds = %3, %30, %20, %15
%33 = phi i32 [ -1, %15 ], [ -1, %20 ], [ 0, %3 ], [ %31, %30 ]
ret i32 %33
}