#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 timeval {
  scalar_t__ tv_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int atoi(char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ gpio_direction(int, int);
int /*<<< orphan*/ gpio_done();
int /*<<< orphan*/ gpio_export(int);
int /*<<< orphan*/ gpio_init();
int /*<<< orphan*/ gpio_unexport(int);
int /*<<< orphan*/ gpio_write(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ warning(char *, char *, char *, char *);
int task_gpio_test_speed(int argc, char *argv[]) {
  int pin, cycles, i;
  struct timeval tv1, tv2;
  double time_diff;
  if (argc != 5) {
    warning("usage: %s %s %s <GPIO pin> <bit flip cycles>\n", argv[0], argv[1],
            argv[2]);
    return EXIT_FAILURE;
  }
  pin = atoi(argv[3]);
  cycles = atoi(argv[4]);
  printf("%s: bit flip GPIO pin %d for %d cycles\n", argv[0], pin, cycles);
  gpio_init();
  gpio_export(pin);
  gpio_direction(pin, 1);
  gettimeofday(&tv1, NULL);
  for (i = 0; i < cycles; ++i) {
    gpio_write(pin, i % 2);
  }
  gpio_write(pin, 0);
  gettimeofday(&tv2, NULL);
  time_diff = (double)(tv2.tv_usec - tv1.tv_usec) / 1000000. +
              (double)(tv2.tv_sec - tv1.tv_sec);
  printf("wall clock of %.4f seconds for %g pin changes per second\n",
         time_diff, (double)cycles / time_diff);
  gpio_unexport(pin);
  gpio_done();
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<task_gpio_test_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     11ed <task_gpio_test_speed+0x4d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe31(%rip),%rdi
call   1030 <warning@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1305 <task_gpio_test_speed+0x165>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10c0 <gpio_init@plt>
mov    -0x14(%rbp),%edi
call   1070 <gpio_export@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1090 <gpio_direction@plt>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1287 <task_gpio_test_speed+0xe7>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
call   1080 <gpio_write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1258 <task_gpio_test_speed+0xb8>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1080 <gpio_write@plt>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x38(%rbp),%rax
sub    -0x28(%rbp),%rax
cvtsi2sd %rax,%xmm0
movsd  0xd4d(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    -0x40(%rbp),%rax
sub    -0x30(%rbp),%rax
cvtsi2sd %rax,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
cvtsi2sdl -0x18(%rbp),%xmm1
divsd  -0x48(%rbp),%xmm1
lea    0xd7b(%rip),%rdi
mov    $0x2,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   10a0 <gpio_unexport@plt>
mov    $0x0,%al
call   1040 <gpio_done@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<task_gpio_test_speed>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
cmp    $0x5,%edi
jne    12a5 <task_gpio_test_speed+0x105>
mov    0x18(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r15d
mov    0x20(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
mov    (%r14),%rsi
lea    0xe5f(%rip),%rdi
xor    %ebx,%ebx
mov    %r15d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10c0 <gpio_init@plt>
mov    %r15d,%edi
call   1070 <gpio_export@plt>
mov    %r15d,%edi
mov    $0x1,%esi
call   1090 <gpio_direction@plt>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
test   %ebp,%ebp
jle    1234 <task_gpio_test_speed+0x94>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%esi
and    $0x1,%esi
mov    %r15d,%edi
call   1080 <gpio_write@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1220 <task_gpio_test_speed+0x80>
mov    %r15d,%edi
xor    %esi,%esi
call   1080 <gpio_write@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x8(%rsp),%rax
mov    0x10(%rsp),%rcx
sub    0x20(%rsp),%rcx
cvtsi2sd %rcx,%xmm1
divsd  0xd9a(%rip),%xmm1
sub    0x18(%rsp),%rax
cvtsi2sd %rax,%xmm0
addsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
divsd  %xmm0,%xmm1
lea    0xdd8(%rip),%rdi
mov    $0x2,%al
call   1050 <printf@plt>
mov    %r15d,%edi
call   10a0 <gpio_unexport@plt>
xor    %eax,%eax
call   1040 <gpio_done@plt>
mov    0x2d35(%rip),%rax
jmp    12c3 <task_gpio_test_speed+0x123>
mov    (%r14),%rsi
mov    0x8(%r14),%rdx
mov    0x10(%r14),%rcx
lea    0xd51(%rip),%rdi
call   1030 <warning@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.str = [46 x i8] c"usage: %s %s %s <GPIO pin> <bit flip cycles>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [40 x i8] c"%s: bit flip GPIO pin %d for %d cycles\0A\00"
@.str.2 = [58 x i8] c"wall clock of %.4f seconds for %g pin changes per second\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @task_gpio_test_speed(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca %struct.timeval
%11 = alloca double
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 5
br i1 %13, label %14, label %26
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @warning(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %20, i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %3
br label %87
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 3
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %6
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 4
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %7
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %37, i32 %38, i32 %39)
%41 = call i32 (...) @gpio_init()
%42 = load i32, i32* %6
%43 = call i32 @gpio_export(i32 %42)
%44 = load i32, i32* %6
%45 = call i32 @gpio_direction(i32 %44, i32 1)
%46 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
store i32 0, i32* %8
br label %47
47: ; preds = %56, %26
%48 = load i32, i32* %8
%49 = load i32, i32* %7
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %59
51: ; preds = %47
%52 = load i32, i32* %6
%53 = load i32, i32* %8
%54 = srem i32 %53, 2
%55 = call i32 @gpio_write(i32 %52, i32 %54)
br label %56
56: ; preds = %51
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
br label %47
59: ; preds = %47
%60 = load i32, i32* %6
%61 = call i32 @gpio_write(i32 %60, i32 0)
%62 = call i32 @gettimeofday(%struct.timeval* %10, i32* null)
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%64 = load i64, i64* %63
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%66 = load i64, i64* %65
%67 = sub nsw i64 %64, %66
%68 = sitofp i64 %67 to double
%69 = fdiv double %68, 1.000000e+06
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%71 = load i64, i64* %70
%72 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%73 = load i64, i64* %72
%74 = sub nsw i64 %71, %73
%75 = sitofp i64 %74 to double
%76 = fadd double %69, %75
store double %76, double* %11
%77 = load double, double* %11
%78 = load i32, i32* %7
%79 = sitofp i32 %78 to double
%80 = load double, double* %11
%81 = fdiv double %79, %80
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), double %77, double %81)
%83 = load i32, i32* %6
%84 = call i32 @gpio_unexport(i32 %83)
%85 = call i32 (...) @gpio_done()
%86 = load i32, i32* @EXIT_SUCCESS
store i32 %86, i32* %3
br label %87
87: ; preds = %59, %14
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [46 x i8] c"usage: %s %s %s <GPIO pin> <bit flip cycles>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [40 x i8] c"%s: bit flip GPIO pin %d for %d cycles\0A\00"
@.str.2 = [58 x i8] c"wall clock of %.4f seconds for %g pin changes per second\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @task_gpio_test_speed(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = icmp eq i32 %0, 5
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @warning(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %11, i8* %13)
br label %57
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 3
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 4
%20 = load i8*, i8** %19
%21 = tail call i32 @atoi(i8* %20)
%22 = load i8*, i8** %1
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %18, i32 %21)
%24 = tail call i32 (...) @gpio_init()
%25 = tail call i32 @gpio_export(i32 %18)
%26 = tail call i32 @gpio_direction(i32 %18, i32 1)
%27 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%28 = icmp sgt i32 %21, 0
br i1 %28, label %29, label %35
29: ; preds = %15, %29
%30 = phi i32 [ %33, %29 ], [ 0, %15 ]
%31 = and i32 %30, 1
%32 = tail call i32 @gpio_write(i32 %18, i32 %31)
%33 = add nuw nsw i32 %30, 1
%34 = icmp eq i32 %33, %21
br i1 %34, label %35, label %29
35: ; preds = %29, %15
%36 = tail call i32 @gpio_write(i32 %18, i32 0)
%37 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%39 = load i64, i64* %38
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%41 = load i64, i64* %40
%42 = sub nsw i64 %39, %41
%43 = sitofp i64 %42 to double
%44 = fdiv double %43, 1.000000e+06
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%46 = load i64, i64* %45
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%48 = load i64, i64* %47
%49 = sub nsw i64 %46, %48
%50 = sitofp i64 %49 to double
%51 = fadd double %44, %50
%52 = sitofp i32 %21 to double
%53 = fdiv double %52, %51
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), double %51, double %53)
%55 = tail call i32 @gpio_unexport(i32 %18)
%56 = tail call i32 (...) @gpio_done()
br label %57
57: ; preds = %35, %8
%58 = phi i32* [ @EXIT_FAILURE, %8 ], [ @EXIT_SUCCESS, %35 ]
%59 = load i32, i32* %58
ret i32 %59
}