#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*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcmp(char *, char *);
int write(int, unsigned char *, int);
int write_wifimode_to_boot(char *mode_str) {
  int fd = -1;
  unsigned char i_mode;
  fd = open("/dev/mmcblk0", O_RDWR);
  if (fd < 0) {
    printf("open %s failed.\n", "/dev/mmcblk0");
    return -1;
  }
  if (!strcmp(mode_str, "2g"))
    i_mode = 2;
  else if (!strcmp(mode_str, "5g")) {
    i_mode = 5;
  } else {
    printf("unsupported wifi mode\n");
    close(fd);
    return -1;
  }
  lseek(fd, 0x40000, SEEK_SET);
  if (write(fd, &i_mode, sizeof(unsigned char)) != sizeof(unsigned char)) {
    printf("write %s failed.\n", "/dev/mmcblk0");
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_wifimode_to_boot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b6 <write_wifimode_to_boot+0x56>
lea    0xe71(%rip),%rdi
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <write_wifimode_to_boot+0x12c>
mov    -0x10(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    11d8 <write_wifimode_to_boot+0x78>
movb   $0x2,-0x15(%rbp)
jmp    1221 <write_wifimode_to_boot+0xc1>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    11fa <write_wifimode_to_boot+0x9a>
movb   $0x5,-0x15(%rbp)
jmp    121c <write_wifimode_to_boot+0xbc>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <write_wifimode_to_boot+0x12c>
jmp    1221 <write_wifimode_to_boot+0xc1>
mov    -0x14(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
mov    $0x40000,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cltq
cmp    $0x1,%rax
je     127d <write_wifimode_to_boot+0x11d>
lea    0xde0(%rip),%rdi
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <write_wifimode_to_boot+0x12c>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_wifimode_to_boot>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     11c5 <write_wifimode_to_boot+0x55>
mov    %eax,%ebp
lea    0xe86(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
je     11dc <write_wifimode_to_boot+0x6c>
lea    0xe76(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
je     11e0 <write_wifimode_to_boot+0x70>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
jmp    1231 <write_wifimode_to_boot+0xc1>
lea    0xe41(%rip),%rdi
lea    0xe2d(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1238 <write_wifimode_to_boot+0xc8>
mov    $0x2,%al
jmp    11e2 <write_wifimode_to_boot+0x72>
mov    $0x5,%al
mov    %al,0x7(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x40000,%esi
call   1060 <lseek@plt>
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    121c <write_wifimode_to_boot+0xac>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    123d <write_wifimode_to_boot+0xcd>
lea    0xe01(%rip),%rdi
lea    0xdd6(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/mmcblk0\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"open %s failed.\0A\00"
@.str.2 = [3 x i8] c"2g\00"
@.str.3 = [3 x i8] c"5g\00"
@.str.4 = [23 x i8] c"unsupported wifi mode\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [18 x i8] c"write %s failed.\0A\00"
define i32 @write_wifimode_to_boot(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %3
store i32 -1, i32* %4
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %42
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @strcmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %12
store i8 2, i8* %5
br label %27
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = call i32 @strcmp(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %17
store i8 5, i8* %5
br label %26
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %2
br label %42
26: ; preds = %21
br label %27
27: ; preds = %26, %16
%28 = load i32, i32* %4
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @lseek(i32 %28, i32 262144, i32 %29)
%31 = load i32, i32* %4
%32 = call i32 @write(i32 %31, i8* %5, i32 1)
%33 = sext i32 %32 to i64
%34 = icmp ne i64 %33, 1
br i1 %34, label %35, label %39
35: ; preds = %27
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %2
br label %42
39: ; preds = %27
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
store i32 0, i32* %2
br label %42
42: ; preds = %39, %35, %22, %10
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/mmcblk0\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"open %s failed.\0A\00"
@.str.2 = [3 x i8] c"2g\00"
@.str.3 = [3 x i8] c"5g\00"
@SEEK_SET = global i32 0
@.str.5 = [18 x i8] c"write %s failed.\0A\00"
@str = [22 x i8] c"unsupported wifi mode\00"
define i32 @write_wifimode_to_boot(i8* nocapture %0) {
%2 = alloca i8
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %28
8: ; preds = %1
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @close(i32 %4)
br label %28
17: ; preds = %11, %8
%18 = phi i8 [ 2, %8 ], [ 5, %11 ]
store i8 %18, i8* %2
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @lseek(i32 %4, i32 262144, i32 %19)
%21 = call i32 @write(i32 %4, i8* %2, i32 1)
%22 = icmp eq i32 %21, 1
br i1 %22, label %26, label %23
23: ; preds = %17
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%25 = tail call i32 @close(i32 %4)
br label %28
26: ; preds = %17
%27 = tail call i32 @close(i32 %4)
br label %28
28: ; preds = %26, %23, %14, %6
%29 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %23 ], [ 0, %26 ]
ret i32 %29
}