#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 *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int write(int, char const *, size_t);
int otp_write(int fd, const char *buf, size_t len) {
  size_t nleft = len;
  ssize_t nwrote;
  while (nleft) {
    if ((nwrote = write(fd, &buf[len - nleft], nleft)) == -1) {
      if (errno == EINTR) {
        continue;
      } else {
        fprintf(stderr, "otpauth: write: %s\n", strerror(errno));
        (void)close(fd);
        return -1;
      }
    }
    nleft -= nwrote;
  }
  return 0;
}


x86 assembly (clang, -O0):
<otp_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1201 <otp_write+0xc1>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11ed <otp_write+0xad>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    11ac <otp_write+0x6c>
jmp    115b <otp_write+0x1b>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe2c(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <otp_write+0xc8>
movslq -0x24(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    115b <otp_write+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<otp_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %rdx,%rdx
je     11e4 <otp_write+0xa4>
mov    %rdx,%r12
mov    %edi,%r13d
mov    0x2e75(%rip),%r15
mov    0x2e66(%rip),%r14
mov    %rsi,%rbx
mov    %rdx,%rbp
mov    %rsi,(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    119d <otp_write+0x5d>
mov    (%r15),%rdi
cmp    (%r14),%rdi
je     1180 <otp_write+0x40>
jmp    11b8 <otp_write+0x78>
cltq
sub    %rax,%rbp
mov    %r12,%rbx
sub    %rbp,%rbx
mov    (%rsp),%rsi
add    %rsi,%rbx
test   %rbp,%rbp
jne    1180 <otp_write+0x40>
xor    %eax,%eax
jmp    11e4 <otp_write+0xa4>
mov    0x2e21(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe33(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %r13d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%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 = [20 x i8] c"otpauth: write: %s\0A\00"
define i32 @otp_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
store i64 %10, i64* %8
br label %11
11: ; preds = %36, %28, %3
%12 = load i64, i64* %8
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %41
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = load i64, i64* %8
%19 = sub i64 %17, %18
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = load i64, i64* %8
%22 = call i32 @write(i32 %15, i8* %20, i64 %21)
store i32 %22, i32* %9
%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 %11
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i64, i64* @errno
%32 = call i8* @strerror(i64 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 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 %42
36: ; preds = %14
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = load i64, i64* %8
%40 = sub i64 %39, %38
store i64 %40, i64* %8
br label %11
41: ; preds = %11
store i32 0, i32* %4
br label %42
42: ; preds = %41, %29
%43 = load i32, i32* %4
ret i32 %43
}

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