#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strrchr(char *, char);
int init_timer(FILE *fd) {
  char timer_id[80];
  int retval;
  printf("\nPlease reset the timer. Hold switch closed for 1 second, then "
         "open.\n");
  if (!fgets(timer_id, 80, fd)) {
    fprintf(stderr, "Error reading from port! Please check timer.\n");
    return 0;
  }
  if (strrchr(timer_id, '8'))
    retval = 8;
  else if (strrchr(timer_id, '4'))
    retval = 4;
  else if (strrchr(timer_id, '2'))
    retval = 2;
  else {
    fprintf(stderr, "Error! unable to parse timer string\n\t%s\n", timer_id);
    return 0;
  }
  printf("Timer initialized. Timer reports \n\t%s\n", timer_id);
  printf("Found %d lanes\n", retval);
  return retval;
}


x86 assembly (clang, -O0):
<init_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x50,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    1198 <init_timer+0x58>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xec1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125e <init_timer+0x11e>
lea    -0x60(%rbp),%rdi
mov    $0x38,%esi
call   1040 <strrchr@plt>
cmp    $0x0,%rax
je     11bc <init_timer+0x7c>
movl   $0x8,-0x64(%rbp)
jmp    1235 <init_timer+0xf5>
lea    -0x60(%rbp),%rdi
mov    $0x34,%esi
call   1040 <strrchr@plt>
cmp    $0x0,%rax
je     11e0 <init_timer+0xa0>
movl   $0x4,-0x64(%rbp)
jmp    1230 <init_timer+0xf0>
lea    -0x60(%rbp),%rdi
mov    $0x32,%esi
call   1040 <strrchr@plt>
cmp    $0x0,%rax
je     1204 <init_timer+0xc4>
movl   $0x2,-0x64(%rbp)
jmp    122b <init_timer+0xeb>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125e <init_timer+0x11e>
jmp    1230 <init_timer+0xf0>
jmp    1235 <init_timer+0xf5>
lea    -0x60(%rbp),%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x64(%rbp),%esi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_timer>:
push   %rbx
sub    $0x50,%rsp
mov    %rdi,%rbx
lea    0xf2f(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %eax,%eax
je     11e0 <init_timer+0x90>
mov    %rsp,%rdi
mov    $0x38,%esi
call   1050 <strrchr@plt>
mov    $0x8,%ebx
test   %rax,%rax
jne    11bd <init_timer+0x6d>
mov    %rsp,%rdi
mov    $0x34,%esi
call   1050 <strrchr@plt>
mov    $0x4,%ebx
test   %rax,%rax
jne    11bd <init_timer+0x6d>
mov    %rsp,%rdi
mov    $0x32,%esi
call   1050 <strrchr@plt>
mov    $0x2,%ebx
test   %rax,%rax
je     1201 <init_timer+0xb1>
lea    0xe93(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea9(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f9 <init_timer+0xa9>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
ret
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
xor    %ebx,%ebx
mov    %rsp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11f9 <init_timer+0xa9>

LLVM IR (-O0):
@.str = [70 x i8] c"\0APlease reset the timer. Hold switch closed for 1 second, then open.\0A\00"
@stderr = global i32 0
@.str.1 = [46 x i8] c"Error reading from port! Please check timer.\0A\00"
@.str.2 = [41 x i8] c"Error! unable to parse timer string\0A\09%s\0A\00"
@.str.3 = [39 x i8] c"Timer initialized. Timer reports \0A\09%s\0A\00"
@.str.4 = [16 x i8] c"Found %d lanes\0A\00"
define i32 @init_timer(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [80 x i8]
%5 = alloca i32
store i32* %0, i32** %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%8 = load i32*, i32** %3
%9 = call i32 @fgets(i8* %7, i32 80, i32* %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %41
14: ; preds = %1
%15 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%16 = call i64 @strrchr(i8* %15, i8 signext 56)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 8, i32* %5
br label %35
19: ; preds = %14
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%21 = call i64 @strrchr(i8* %20, i8 signext 52)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 4, i32* %5
br label %34
24: ; preds = %19
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%26 = call i64 @strrchr(i8* %25, i8 signext 50)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 2, i32* %5
br label %33
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store i32 0, i32* %2
br label %41
33: ; preds = %28
br label %34
34: ; preds = %33, %23
br label %35
35: ; preds = %34, %18
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %5
store i32 %40, i32* %2
br label %41
41: ; preds = %35, %29, %11
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [46 x i8] c"Error reading from port! Please check timer.\0A\00"
@.str.2 = [41 x i8] c"Error! unable to parse timer string\0A\09%s\0A\00"
@.str.3 = [39 x i8] c"Timer initialized. Timer reports \0A\09%s\0A\00"
@.str.4 = [16 x i8] c"Found %d lanes\0A\00"
@str = [69 x i8] c"\0APlease reset the timer. Hold switch closed for 1 second, then open.\00"
define i32 @init_timer(i32* nocapture %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @str, i64 0, i64 0))
%5 = call i32 @fgets(i8* %3, i32 80, i32* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %26
10: ; preds = %1
%11 = call i64 @strrchr(i8* %3, i8 signext 56)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = call i64 @strrchr(i8* %3, i8 signext 52)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = call i64 @strrchr(i8* %3, i8 signext 50)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %26
22: ; preds = %16, %13, %10
%23 = phi i32 [ 8, %10 ], [ 4, %13 ], [ 2, %16 ]
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %23)
br label %26
26: ; preds = %22, %19, %7
%27 = phi i32 [ %23, %22 ], [ 0, %19 ], [ 0, %7 ]
ret i32 %27
}