#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 timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
struct sched_param {
  int sched_priority;
};
struct pollfd {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ SCHED_FIFO;
int /*<<< orphan*/ TIMER_ABSTIME;
int atoi(char *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gpio_export(unsigned int);
int /*<<< orphan*/ gpio_set_dir(unsigned int, int);
int /*<<< orphan*/ out();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                       struct sched_param *);
int /*<<< orphan*/ tsnorm(struct timespec *);
int main(int argc, char **argv, char **envp) {
  struct pollfd fdset[2];
  int nfds = 2;
  int gpio_fd, timeout, rc;
  char *buf[64];
  unsigned int gpio;
  int len;
  struct timespec t;
  struct sched_param param;
  int interval = 50000;
  int i = 0;
  if (argc >= 2 && atoi(argv[1]) > 0) {
    printf("using realtime, priority: %d\n", atoi(argv[1]));
    param.sched_priority = atoi(argv[1]);
    if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
      perror("sched_setscheduler failed");
      exit(-1);
    }
  }
  if (argc >= 3)
    interval = atoi(argv[2]);
  clock_gettime(0, &t);
  t.tv_sec++;
  gpio = 139;
  gpio_export(gpio);
  gpio_set_dir(gpio, 1);
  for (i = 10000; i; i--) {
    clock_nanosleep(0, TIMER_ABSTIME, &t, NULL);
    out();
    t.tv_nsec += interval;
    tsnorm(&t);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x2,-0x24(%rbp)
movl   $0xc350,-0x24c(%rbp)
movl   $0x0,-0x250(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1271 <main+0xc1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
cmp    $0x0,%eax
jle    1271 <main+0xc1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%esi
lea    0xde2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x248(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x248(%rbp),%rdx
call   1070 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
jne    126c <main+0xbc>
lea    0xdc1(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    1271 <main+0xc1>
cmpl   $0x3,-0x8(%rbp)
jl     128e <main+0xde>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x24c(%rbp)
xor    %edi,%edi
lea    -0x240(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x23c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x23c(%rbp)
movl   $0x8b,-0x234(%rbp)
mov    -0x234(%rbp),%edi
call   1060 <gpio_export@plt>
mov    -0x234(%rbp),%edi
mov    $0x1,%esi
call   1040 <gpio_set_dir@plt>
movl   $0x2710,-0x250(%rbp)
cmpl   $0x0,-0x250(%rbp)
je     133b <main+0x18b>
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x240(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10d0 <clock_nanosleep@plt>
mov    $0x0,%al
call   10c0 <out@plt>
mov    -0x24c(%rbp),%eax
add    -0x240(%rbp),%eax
mov    %eax,-0x240(%rbp)
lea    -0x240(%rbp),%rdi
call   1080 <tsnorm@plt>
mov    -0x250(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x250(%rbp)
jmp    12da <main+0x12a>
xor    %eax,%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xc350,%r15d
cmp    $0x2,%edi
jl     1221 <main+0x71>
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
test   %eax,%eax
jle    1210 <main+0x60>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x8(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,0x8(%rsp)
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1070 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
je     1294 <main+0xe4>
cmp    $0x3,%ebx
jb     1221 <main+0x71>
mov    0x10(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r15d
lea    0x10(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
addl   $0x1,0x14(%rsp)
mov    $0x8b,%edi
call   1060 <gpio_export@plt>
mov    $0x8b,%edi
mov    $0x1,%esi
call   1040 <gpio_set_dir@plt>
mov    $0xffffd8f0,%ebp
mov    0x2d8e(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%r14),%esi
xor    %edi,%edi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10d0 <clock_nanosleep@plt>
xor    %eax,%eax
call   10c0 <out@plt>
add    %r15d,0x10(%rsp)
mov    %rbx,%rdi
call   1080 <tsnorm@plt>
inc    %ebp
jne    1260 <main+0xb0>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd83(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32 }
%struct.timespec = type { i32, i32 }
%struct.sched_param = type { i32 }
@.str = [30 x i8] c"using realtime, priority: %d\0A\00"
@SCHED_FIFO = global i32 0
@.str.1 = [26 x i8] c"sched_setscheduler failed\00"
@TIMER_ABSTIME = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [2 x %struct.pollfd]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [64 x i8*]
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timespec
%17 = alloca %struct.sched_param
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 2, i32* %9
store i32 50000, i32* %18
store i32 0, i32* %19
%20 = load i32, i32* %5
%21 = icmp sge i32 %20, 2
br i1 %21, label %22, label %46
22: ; preds = %3
%23 = load i8**, i8*** %6
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %46
28: ; preds = %22
%29 = load i8**, i8*** %6
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = load i8**, i8*** %6
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
%38 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %17, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* @SCHED_FIFO
%40 = call i32 @sched_setscheduler(i32 0, i32 %39, %struct.sched_param* %17)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %28
%43 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %28
br label %46
46: ; preds = %45, %22, %3
%47 = load i32, i32* %5
%48 = icmp sge i32 %47, 3
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = load i8**, i8*** %6
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
%53 = call i32 @atoi(i8* %52)
store i32 %53, i32* %18
br label %54
54: ; preds = %49, %46
%55 = call i32 @clock_gettime(i32 0, %struct.timespec* %16)
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %16, i32 0, i32 1
%57 = load i32, i32* %56
%58 = add nsw i32 %57, 1
store i32 %58, i32* %56
store i32 139, i32* %14
%59 = load i32, i32* %14
%60 = call i32 @gpio_export(i32 %59)
%61 = load i32, i32* %14
%62 = call i32 @gpio_set_dir(i32 %61, i32 1)
store i32 10000, i32* %19
br label %63
63: ; preds = %75, %54
%64 = load i32, i32* %19
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %78
66: ; preds = %63
%67 = load i32, i32* @TIMER_ABSTIME
%68 = call i32 @clock_nanosleep(i32 0, i32 %67, %struct.timespec* %16, i32* null)
%69 = call i32 (...) @out()
%70 = load i32, i32* %18
%71 = getelementptr inbounds %struct.timespec, %struct.timespec* %16, i32 0, i32 0
%72 = load i32, i32* %71
%73 = add nsw i32 %72, %70
store i32 %73, i32* %71
%74 = call i32 @tsnorm(%struct.timespec* %16)
br label %75
75: ; preds = %66
%76 = load i32, i32* %19
%77 = add nsw i32 %76, -1
store i32 %77, i32* %19
br label %63
78: ; preds = %63
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.sched_param = type { i32 }
%struct.pollfd = type { i32 }
@.str = [30 x i8] c"using realtime, priority: %d\0A\00"
@SCHED_FIFO = global i32 0
@.str.1 = [26 x i8] c"sched_setscheduler failed\00"
@TIMER_ABSTIME = global i32 0
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = alloca %struct.timespec
%5 = alloca %struct.sched_param
%6 = bitcast %struct.timespec* %4 to i8*
%7 = bitcast %struct.sched_param* %5 to i8*
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %31
9: ; preds = %3
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %9
%15 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %12)
%16 = load i8*, i8** %10
%17 = tail call i32 @atoi(i8* %16)
%18 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %5, i64 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @SCHED_FIFO
%20 = call i32 @sched_setscheduler(i32 0, i32 %19, %struct.sched_param* %5)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %14, %9
%26 = icmp ugt i32 %0, 2
br i1 %26, label %27, label %31
27: ; preds = %25
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
br label %31
31: ; preds = %3, %27, %25
%32 = phi i32 [ %30, %27 ], [ 50000, %25 ], [ 50000, %3 ]
%33 = call i32 @clock_gettime(i32 0, %struct.timespec* %4)
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%35 = load i32, i32* %34
%36 = add nsw i32 %35, 1
store i32 %36, i32* %34
%37 = call i32 @gpio_export(i32 139)
%38 = call i32 @gpio_set_dir(i32 139, i32 1)
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
br label %40
40: ; preds = %31, %40
%41 = phi i32 [ 10000, %31 ], [ %48, %40 ]
%42 = load i32, i32* @TIMER_ABSTIME
%43 = call i32 @clock_nanosleep(i32 0, i32 %42, %struct.timespec* %4, i32* null)
%44 = call i32 (...) @out()
%45 = load i32, i32* %39
%46 = add nsw i32 %45, %32
store i32 %46, i32* %39
%47 = call i32 @tsnorm(%struct.timespec* %4)
%48 = add nsw i32 %41, -1
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %40
50: ; preds = %40
ret i32 0
}