#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 */

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int ioctl(int, unsigned long, void *);
int xioctl(int fd, unsigned long int request, void *arg) {
  for (int i = 0; i < 100; i++) {
    int r = ioctl(fd, request, arg);
    if (r != -1 || errno != EINTR)
      return r;
  }
  return -1;
}


x86 assembly (clang, -O0):
<xioctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    1189 <xioctl+0x79>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    116b <xioctl+0x5b>
mov    0x2e88(%rip),%rax
mov    (%rax),%rax
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
je     1176 <xioctl+0x66>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <xioctl+0x80>
jmp    117b <xioctl+0x6b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112a <xioctl+0x1a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xioctl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x63,%ebx
mov    0x2eb1(%rip),%r12
mov    0x2ea2(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1166 <xioctl+0x56>
mov    (%r12),%rdx
sub    $0x1,%ebx
setb   %cl
cmp    0x0(%r13),%rdx
jne    1166 <xioctl+0x56>
test   %cl,%cl
je     1140 <xioctl+0x30>
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
define i32 @xioctl(i32 %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 0, i32* %8
br label %10
10: ; preds = %27, %3
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 100
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i64, i64* %6
%16 = load i8*, i8** %7
%17 = call i32 @ioctl(i32 %14, i64 %15, i8* %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp ne i32 %18, -1
br i1 %19, label %24, label %20
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20, %13
%25 = load i32, i32* %9
store i32 %25, i32* %4
br label %31
26: ; preds = %20
br label %27
27: ; preds = %26
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %10
30: ; preds = %10
store i32 -1, i32* %4
br label %31
31: ; preds = %30, %24
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @xioctl(i32 %0, i64 %1, i8* %2) {
br label %4
4: ; preds = %8, %3
%5 = phi i32 [ 0, %3 ], [ %12, %8 ]
%6 = tail call i32 @ioctl(i32 %0, i64 %1, i8* %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp ne i64 %9, %10
%12 = add nuw nsw i32 %5, 1
%13 = icmp eq i32 %12, 100
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %15, label %4
15: ; preds = %4, %8
ret i32 %6
}