#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 termios {
  int dummy;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int tcsetattr(int, int, struct termios const *);
int setattr(int fd, int opt, const struct termios *arg) {
  int result, retries = 3;
  while (-1 == (result = tcsetattr(fd, opt, arg)) && retries > 0) {
    retries--;
    if (EINTR == errno)
      continue;
    break;
  }
  return result;
}


x86 assembly (clang, -O0):
<setattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x3,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1030 <tcsetattr@plt>
mov    %eax,%edx
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    %edx,%ecx
mov    %al,-0x19(%rbp)
jne    1159 <setattr+0x49>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1169 <setattr+0x59>
jmp    1196 <setattr+0x86>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <setattr+0x81>
jmp    1129 <setattr+0x19>
jmp    1196 <setattr+0x86>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setattr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
mov    $0xfffffffd,%ebp
mov    0x2ea9(%rip),%r12
mov    0x2eaa(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r14,%rdx
call   1030 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1168 <setattr+0x58>
test   %ebp,%ebp
je     1168 <setattr+0x58>
mov    (%r12),%rax
add    $0x1,%ebp
cmp    0x0(%r13),%rax
je     1140 <setattr+0x30>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@EINTR = global i64 0
@errno = global i64 0
define i32 @setattr(i32 %0, i32 %1, %struct.termios* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store %struct.termios* %2, %struct.termios** %6
store i32 3, i32* %8
br label %9
9: ; preds = %26, %3
%10 = load i32, i32* %4
%11 = load i32, i32* %5
%12 = load %struct.termios*, %struct.termios** %6
%13 = call i32 @tcsetattr(i32 %10, i32 %11, %struct.termios* %12)
store i32 %13, i32* %7
%14 = icmp eq i32 -1, %13
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* %8
%17 = icmp sgt i32 %16, 0
br label %18
18: ; preds = %15, %9
%19 = phi i1 [ false, %9 ], [ %17, %15 ]
br i1 %19, label %20, label %28
20: ; preds = %18
%21 = load i32, i32* %8
%22 = add nsw i32 %21, -1
store i32 %22, i32* %8
%23 = load i64, i64* @EINTR
%24 = load i64, i64* @errno
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %20
br label %9
27: ; preds = %20
br label %28
28: ; preds = %27, %18
%29 = load i32, i32* %7
ret i32 %29
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@EINTR = global i64 0
@errno = global i64 0
define i32 @setattr(i32 %0, i32 %1, %struct.termios* %2) {
br label %4
4: ; preds = %10, %3
%5 = phi i32 [ 3, %3 ], [ %11, %10 ]
%6 = tail call i32 @tcsetattr(i32 %0, i32 %1, %struct.termios* %2)
%7 = icmp eq i32 %6, -1
%8 = icmp ne i32 %5, 0
%9 = select i1 %7, i1 %8, i1 false
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = add nsw i32 %5, -1
%12 = load i64, i64* @EINTR
%13 = load i64, i64* @errno
%14 = icmp eq i64 %12, %13
br i1 %14, label %4, label %15
15: ; preds = %10, %4
%16 = phi i32 [ -1, %10 ], [ %6, %4 ]
ret i32 %16
}