#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 uint8_t;

/* Variables and functions */
int flash_read_jedec(int);
int flash_read_sign(int);
int flash_read_status(int);
int /*<<< orphan*/ flash_write_disable(int);
int /*<<< orphan*/ flash_write_enable(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usleep(int);
int is_switch_ok(int fd) {
  int wel = 0;
  uint8_t status = 0;
  printf("Flash signature: 0x%x\n", flash_read_sign(fd));
  printf("JEDEC: 0x%x\n", flash_read_jedec(fd));
  status = flash_read_status(fd);
  wel = status & (1 << 1);
  usleep(50 * 1000);
  wel ? flash_write_disable(fd) : flash_write_enable(fd);
  status = flash_read_status(fd);
  if (wel == (status & (1 << 1))) {
    printf("test WEL failed.\n");
    return -1;
  }
  wel ? flash_write_enable(fd) : flash_write_disable(fd);
  usleep(50 * 1000);
  status = flash_read_status(fd);
  if (wel != (status & (1 << 1))) {
    printf("test WEL failed 2.\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_switch_ok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <flash_read_sign@plt>
mov    %eax,%esi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <flash_read_jedec@plt>
mov    %eax,%esi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1030 <flash_read_status@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
and    $0x2,%eax
mov    %eax,-0xc(%rbp)
mov    $0xc350,%edi
call   1090 <usleep@plt>
cmpl   $0x0,-0xc(%rbp)
je     11ee <is_switch_ok+0x7e>
mov    -0x8(%rbp),%edi
call   1080 <flash_write_disable@plt>
jmp    11f6 <is_switch_ok+0x86>
mov    -0x8(%rbp),%edi
call   1040 <flash_write_enable@plt>
mov    -0x8(%rbp),%edi
call   1030 <flash_read_status@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
and    $0x2,%ecx
cmp    %ecx,%eax
jne    122c <is_switch_ok+0xbc>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <is_switch_ok+0x122>
cmpl   $0x0,-0xc(%rbp)
je     1243 <is_switch_ok+0xd3>
mov    -0x8(%rbp),%edi
call   1040 <flash_write_enable@plt>
jmp    124b <is_switch_ok+0xdb>
mov    -0x8(%rbp),%edi
call   1080 <flash_write_disable@plt>
mov    $0xc350,%edi
call   1090 <usleep@plt>
mov    -0x8(%rbp),%edi
call   1030 <flash_read_status@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
and    $0x2,%ecx
cmp    %ecx,%eax
je     128b <is_switch_ok+0x11b>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <is_switch_ok+0x122>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_switch_ok>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
call   1080 <flash_read_sign@plt>
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <flash_read_jedec@plt>
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1030 <flash_read_status@plt>
mov    %eax,%ebp
mov    $0xc350,%edi
call   10a0 <usleep@plt>
mov    %ebx,%edi
and    $0x2,%ebp
jne    11e4 <is_switch_ok+0x64>
call   1040 <flash_write_enable@plt>
mov    %ebx,%edi
call   1030 <flash_read_status@plt>
test   $0x2,%al
je     11f7 <is_switch_ok+0x77>
mov    %ebx,%edi
call   1090 <flash_write_disable@plt>
jmp    1207 <is_switch_ok+0x87>
call   1090 <flash_write_disable@plt>
mov    %ebx,%edi
call   1030 <flash_read_status@plt>
and    $0x2,%eax
cmp    %eax,%ebp
jne    1200 <is_switch_ok+0x80>
lea    0xe39(%rip),%rdi
jmp    122a <is_switch_ok+0xaa>
mov    %ebx,%edi
call   1040 <flash_write_enable@plt>
mov    $0xc350,%edi
call   10a0 <usleep@plt>
mov    %ebx,%edi
call   1030 <flash_read_status@plt>
mov    %eax,%ecx
and    $0x2,%ecx
xor    %eax,%eax
cmp    %ecx,%ebp
je     1234 <is_switch_ok+0xb4>
lea    0xdfa(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Flash signature: 0x%x\0A\00"
@.str.1 = [13 x i8] c"JEDEC: 0x%x\0A\00"
@.str.2 = [18 x i8] c"test WEL failed.\0A\00"
@.str.3 = [20 x i8] c"test WEL failed 2.\0A\00"
define i32 @is_switch_ok(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%6 = load i32, i32* %3
%7 = call i32 @flash_read_sign(i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %3
%10 = call i32 @flash_read_jedec(i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %3
%13 = call i32 @flash_read_status(i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = and i32 %14, 2
store i32 %15, i32* %4
%16 = call i32 @usleep(i32 50000)
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i32, i32* %3
%21 = call i32 @flash_write_disable(i32 %20)
br label %25
22: ; preds = %1
%23 = load i32, i32* %3
%24 = call i32 @flash_write_enable(i32 %23)
br label %25
25: ; preds = %22, %19
%26 = phi i32 [ %21, %19 ], [ %24, %22 ]
%27 = load i32, i32* %3
%28 = call i32 @flash_read_status(i32 %27)
store i32 %28, i32* %5
%29 = load i32, i32* %4
%30 = load i32, i32* %5
%31 = and i32 %30, 2
%32 = icmp eq i32 %29, %31
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
35: ; preds = %25
%36 = load i32, i32* %4
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %3
%40 = call i32 @flash_write_enable(i32 %39)
br label %44
41: ; preds = %35
%42 = load i32, i32* %3
%43 = call i32 @flash_write_disable(i32 %42)
br label %44
44: ; preds = %41, %38
%45 = phi i32 [ %40, %38 ], [ %43, %41 ]
%46 = call i32 @usleep(i32 50000)
%47 = load i32, i32* %3
%48 = call i32 @flash_read_status(i32 %47)
store i32 %48, i32* %5
%49 = load i32, i32* %4
%50 = load i32, i32* %5
%51 = and i32 %50, 2
%52 = icmp ne i32 %49, %51
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
55: ; preds = %44
store i32 0, i32* %2
br label %56
56: ; preds = %55, %53, %33
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
@.str = [23 x i8] c"Flash signature: 0x%x\0A\00"
@.str.1 = [13 x i8] c"JEDEC: 0x%x\0A\00"
@str = [19 x i8] c"test WEL failed 2.\00"
@str.4 = [17 x i8] c"test WEL failed.\00"
define i32 @is_switch_ok(i32 %0) {
%2 = tail call i32 @flash_read_sign(i32 %0)
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 @flash_read_jedec(i32 %0)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = tail call i32 @flash_read_status(i32 %0)
%7 = and i32 %6, 2
%8 = tail call i32 @usleep(i32 50000)
%9 = icmp eq i32 %7, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = tail call i32 @flash_write_enable(i32 %0)
%12 = tail call i32 @flash_read_status(i32 %0)
%13 = and i32 %12, 2
%14 = icmp eq i32 %13, 0
br i1 %14, label %29, label %22
15: ; preds = %1
%16 = tail call i32 @flash_write_disable(i32 %0)
%17 = tail call i32 @flash_read_status(i32 %0)
%18 = and i32 %17, 2
%19 = icmp eq i32 %7, %18
br i1 %19, label %29, label %20
20: ; preds = %15
%21 = tail call i32 @flash_write_enable(i32 %0)
br label %24
22: ; preds = %10
%23 = tail call i32 @flash_write_disable(i32 %0)
br label %24
24: ; preds = %22, %20
%25 = tail call i32 @usleep(i32 50000)
%26 = tail call i32 @flash_read_status(i32 %0)
%27 = and i32 %26, 2
%28 = icmp eq i32 %7, %27
br i1 %28, label %32, label %29
29: ; preds = %24, %10, %15
%30 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.4, i64 0, i64 0), %15 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.4, i64 0, i64 0), %10 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0), %24 ]
%31 = tail call i32 @puts(i8* dereferenceable(1) %30)
br label %32
32: ; preds = %29, %24
%33 = phi i32 [ 0, %24 ], [ -1, %29 ]
ret i32 %33
}