#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 tcgetattr(int, struct termios *);
int getattr(int fd, struct termios *arg) {
  int result, retries = 3;
  while (-1 == (result = tcgetattr(fd, arg)) && retries > 0) {
    retries--;
    if (EINTR == errno)
      continue;
    break;
  }
  return result;
}


x86 assembly (clang, -O0):
<getattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <tcgetattr@plt>
mov    %eax,%edx
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    %edx,%ecx
mov    %al,-0x19(%rbp)
jne    1153 <getattr+0x43>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1163 <getattr+0x53>
jmp    1190 <getattr+0x80>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    118b <getattr+0x7b>
jmp    1126 <getattr+0x16>
jmp    1190 <getattr+0x80>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getattr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0xfffffffd,%ebx
mov    0x2eaf(%rip),%r15
mov    0x2eb0(%rip),%r12
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1154 <getattr+0x44>
test   %ebx,%ebx
je     1154 <getattr+0x44>
mov    (%r15),%rax
add    $0x1,%ebx
cmp    (%r12),%rax
je     1130 <getattr+0x20>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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