#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*/ word;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ erase_pot_mtd(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ write(int, unsigned int *, int);
void set_potval(char *value) {
  int devfd;
  int pot_value, len;
  unsigned int word;
  pot_value = atoi(value);
  if (pot_value < 0 || pot_value > 4320)
    return;
  system("/usr/bin/killall potd 2> /dev/null");
  erase_pot_mtd("/dev/mmcblk0p15");
  devfd = open("/dev/mmcblk0p15", O_RDWR);
  if (devfd < 0)
    goto start;
  len = pot_value >> 5;
  word = 0x00000000;
  while (len--)
    write(devfd, &word, sizeof(word));
  len = pot_value & 31;
  word = 0xFFFFFFFF >> len;
  write(devfd, &word, sizeof(word));
  close(devfd);
start:
  system("/usr/sbin/potd");
}


x86 assembly (clang, -O0):
<set_potval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     118f <set_potval+0x2f>
cmpl   $0x10e0,-0x10(%rbp)
jle    1194 <set_potval+0x34>
jmp    1248 <set_potval+0xe8>
lea    0xe65(%rip),%rdi
call   1040 <system@plt>
lea    0xe7c(%rip),%rdi
call   1050 <erase_pot_mtd@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d3 <set_potval+0x73>
jmp    123c <set_potval+0xdc>
mov    -0x10(%rbp),%eax
sar    $0x5,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     120d <set_potval+0xad>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    11e3 <set_potval+0x83>
mov    -0x10(%rbp),%eax
and    $0x1f,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    $0xffffffff,%eax
shr    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xdf0(%rip),%rdi
call   1040 <system@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_potval>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
call   1080 <atoi@plt>
cmp    $0x10e0,%eax
jbe    117e <set_potval+0x1e>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r14d
lea    0xe78(%rip),%rdi
call   1040 <system@plt>
lea    0xe8f(%rip),%rbx
mov    %rbx,%rdi
call   1050 <erase_pot_mtd@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     120a <set_potval+0xaa>
mov    %eax,%ebp
movl   $0x0,0x4(%rsp)
cmp    $0x20,%r14d
jb     11e4 <set_potval+0x84>
mov    %r14d,%ebx
shr    $0x5,%ebx
lea    0x4(%rsp),%r15
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebx
jne    11d0 <set_potval+0x70>
mov    $0xffffffff,%eax
mov    %r14d,%ecx
shr    %cl,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe22(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <system@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"/usr/bin/killall potd 2> /dev/null\00"
@.str.1 = [16 x i8] c"/dev/mmcblk0p15\00"
@O_RDWR = global i32 0
@.str.2 = [15 x i8] c"/usr/sbin/potd\00"
define void @set_potval(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @atoi(i8* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 4320
br i1 %13, label %14, label %15
14: ; preds = %11, %1
br label %44
15: ; preds = %11
%16 = call i32 @system(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @erase_pot_mtd(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %15
br label %42
23: ; preds = %15
%24 = load i32, i32* %4
%25 = ashr i32 %24, 5
store i32 %25, i32* %5
store i32 0, i32* %6
br label %26
26: ; preds = %30, %23
%27 = load i32, i32* %5
%28 = add nsw i32 %27, -1
store i32 %28, i32* %5
%29 = icmp ne i32 %27, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %3
%32 = call i32 @write(i32 %31, i32* %6, i32 4)
br label %26
33: ; preds = %26
%34 = load i32, i32* %4
%35 = and i32 %34, 31
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = lshr i32 -1, %36
store i32 %37, i32* %6
%38 = load i32, i32* %3
%39 = call i32 @write(i32 %38, i32* %6, i32 4)
%40 = load i32, i32* %3
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %33, %22
%43 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %14
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"/usr/bin/killall potd 2> /dev/null\00"
@.str.1 = [16 x i8] c"/dev/mmcblk0p15\00"
@O_RDWR = global i32 0
@.str.2 = [15 x i8] c"/usr/sbin/potd\00"
define void @set_potval(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @atoi(i8* %0)
%5 = icmp ugt i32 %4, 4320
br i1 %5, label %28, label %6
6: ; preds = %1
%7 = tail call i32 @system(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @erase_pot_mtd(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %6
store i32 0, i32* %2
%13 = icmp ult i32 %4, 32
br i1 %13, label %21, label %14
14: ; preds = %12
%15 = lshr i32 %4, 5
br label %16
16: ; preds = %14, %16
%17 = phi i32 [ %18, %16 ], [ %15, %14 ]
%18 = add nsw i32 %17, -1
%19 = call i32 @write(i32 %10, i32* %2, i32 4)
%20 = icmp eq i32 %18, 0
br i1 %20, label %21, label %16
21: ; preds = %16, %12
%22 = and i32 %4, 31
%23 = lshr i32 -1, %22
store i32 %23, i32* %2
%24 = call i32 @write(i32 %10, i32* %2, i32 4)
%25 = tail call i32 @close(i32 %10)
br label %26
26: ; preds = %6, %21
%27 = tail call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %1, %26
ret void
}