#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 */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int ser_getc(int fd, char *c) {
  int r;
  int rt = 10;
  do {
    r = read(fd, c, 1);
    if (r == -1) {
      if (rt-- > 0) {
        usleep(100 * 1000);
        continue;
      }
      fprintf(stderr, "%s\n", strerror(errno));
      return -1;
    }
  } while (r != 1);
  return r;
}


x86 assembly (clang, -O0):
<ser_getc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xa,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11cf <ser_getc+0x8f>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0x0,%eax
jle    1197 <ser_getc+0x57>
mov    $0x186a0,%edi
call   1060 <usleep@plt>
jmp    11d4 <ser_getc+0x94>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe42(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <ser_getc+0xa4>
jmp    11d4 <ser_getc+0x94>
cmpl   $0x1,-0x14(%rbp)
jne    1156 <ser_getc+0x16>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ser_getc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0xa,%ebp
jmp    1165 <ser_getc+0x25>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x1,%ebx
je     118f <ser_getc+0x4f>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1160 <ser_getc+0x20>
test   %ebp,%ebp
jle    1196 <ser_getc+0x56>
add    $0xffffffff,%ebp
mov    $0x186a0,%edi
call   1060 <usleep@plt>
jmp    1160 <ser_getc+0x20>
mov    $0x1,%eax
jmp    11c3 <ser_getc+0x83>
mov    0x2e43(%rip),%rax
mov    (%rax),%ebx
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe4c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @ser_getc(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 10, i32* %7
br label %8
8: ; preds = %26, %2
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = call i32 @read(i32 %9, i8* %10, i32 1)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %25
14: ; preds = %8
%15 = load i32, i32* %7
%16 = add nsw i32 %15, -1
store i32 %16, i32* %7
%17 = icmp sgt i32 %15, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @usleep(i32 100000)
br label %26
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 -1, i32* %3
br label %31
25: ; preds = %8
br label %26
26: ; preds = %25, %18
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 1
br i1 %28, label %8, label %29
29: ; preds = %26
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %20
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @ser_getc(i32 %0, i8* nocapture %1) {
br label %3
3: ; preds = %17, %2
%4 = phi i32 [ 10, %2 ], [ %18, %17 ]
%5 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %17
7: ; preds = %3
%8 = icmp sgt i32 %4, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = add nsw i32 %4, -1
%11 = tail call i32 @usleep(i32 100000)
br label %17
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %20
17: ; preds = %3, %9
%18 = phi i32 [ %10, %9 ], [ %4, %3 ]
%19 = icmp eq i32 %5, 1
br i1 %19, label %20, label %3
20: ; preds = %17, %12
%21 = phi i32 [ -1, %12 ], [ 1, %17 ]
ret i32 %21
}