#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 flock {
  int /*<<< orphan*/ l_type;
  int /*<<< orphan*/ l_pid;
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ sync();
int write(int, char *, int);
int __mib_flash_write(char *buf, int offset, int len) {
  int fh;
  struct flock fl;
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;
  fl.l_pid = getpid();
  fh = open(("/var/config/setting.bin"), O_RDWR);
  if (fh == -1)
    fh = open(("/var/config/setting.bin"), O_CREAT | O_RDWR);
  if (fh == -1) {
    ("Error: flash_write open fail! (Dev = %s)\n", ("/var/config/setting.bin"));
    return 0;
  }
  fcntl(fh, F_SETLKW, &fl);
  lseek(fh, offset, SEEK_SET);
  if (write(fh, buf, len) != len) {
    ("Error: flash_write write fail!\n");
    return 0;
  }
  fl.l_type = F_UNLCK;
  fcntl(fh, F_SETLK, &fl);
  close(fh);
  sync();
  ("Flash write to %x len=%d\n", offset, len);
  return 1;
}


x86 assembly (clang, -O0):
<__mib_flash_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f7 <__mib_flash_write+0x87>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
lea    0xe11(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    120d <__mib_flash_write+0x9d>
movl   $0x0,-0x4(%rbp)
jmp    1291 <__mib_flash_write+0x121>
mov    -0x1c(%rbp),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
cmp    -0x18(%rbp),%eax
je     125a <__mib_flash_write+0xea>
movl   $0x0,-0x4(%rbp)
jmp    1291 <__mib_flash_write+0x121>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    $0x0,%al
call   1080 <sync@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mib_flash_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebx
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
xor    %r12d,%r12d
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0xc(%rsp)
mov    0x2e23(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe38(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11f4 <__mib_flash_write+0x84>
mov    0x2e1d(%rip),%rax
mov    0x0(%r13),%esi
or     (%rax),%esi
lea    0xe18(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1262 <__mib_flash_write+0xf2>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r15d,%esi
call   1060 <lseek@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
jne    1262 <__mib_flash_write+0xf2>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1080 <sync@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i64, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str = [24 x i8] c"/var/config/setting.bin\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [42 x i8] c"Error: flash_write open fail! (Dev = %s)\0A\00"
@F_SETLKW = global i32 0
@.str.2 = [32 x i8] c"Error: flash_write write fail!\0A\00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.3 = [26 x i8] c"Flash write to %x len=%d\0A\00"
define i32 @__mib_flash_write(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.flock
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* @F_WRLCK
%11 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @SEEK_SET
%13 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 4
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 3
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i64 0, i64* %15
%16 = call i32 (...) @getpid()
%17 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load i32, i32* @O_CREAT
%24 = load i32, i32* @O_RDWR
%25 = or i32 %23, %24
%26 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 %26, i32* %8
br label %27
27: ; preds = %22, %3
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 0, i32* %4
br label %57
31: ; preds = %27
%32 = load i32, i32* %8
%33 = load i32, i32* @F_SETLKW
%34 = call i32 @fcntl(i32 %32, i32 %33, %struct.flock* %9)
%35 = load i32, i32* %8
%36 = load i32, i32* %6
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @lseek(i32 %35, i32 %36, i32 %37)
%39 = load i32, i32* %8
%40 = load i8*, i8** %5
%41 = load i32, i32* %7
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
%43 = load i32, i32* %7
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %31
store i32 0, i32* %4
br label %57
46: ; preds = %31
%47 = load i32, i32* @F_UNLCK
%48 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i32 %47, i32* %48
%49 = load i32, i32* %8
%50 = load i32, i32* @F_SETLK
%51 = call i32 @fcntl(i32 %49, i32 %50, %struct.flock* %9)
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
%54 = call i32 (...) @sync()
%55 = load i32, i32* %6
%56 = load i32, i32* %7
store i32 1, i32* %4
br label %57
57: ; preds = %46, %45, %30
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i64, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str = [24 x i8] c"/var/config/setting.bin\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
define i32 @__mib_flash_write(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.flock
%5 = bitcast %struct.flock* %4 to i8*
%6 = load i32, i32* @F_WRLCK
%7 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i32 %6, i32* %7
%8 = load i32, i32* @SEEK_SET
%9 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 4
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
%11 = bitcast i64* %10 to i8*
%12 = tail call i32 (...) @getpid()
%13 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %3
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_RDWR
%20 = or i32 %19, %18
%21 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %37, label %23
23: ; preds = %3, %17
%24 = phi i32 [ %21, %17 ], [ %15, %3 ]
%25 = load i32, i32* @F_SETLKW
%26 = call i32 @fcntl(i32 %24, i32 %25, %struct.flock* %4)
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @lseek(i32 %24, i32 %1, i32 %27)
%29 = call i32 @write(i32 %24, i8* %0, i32 %2)
%30 = icmp eq i32 %29, %2
br i1 %30, label %31, label %37
31: ; preds = %23
%32 = load i32, i32* @F_UNLCK
store i32 %32, i32* %7
%33 = load i32, i32* @F_SETLK
%34 = call i32 @fcntl(i32 %24, i32 %33, %struct.flock* %4)
%35 = call i32 @close(i32 %24)
%36 = call i32 (...) @sync()
br label %37
37: ; preds = %23, %17, %31
%38 = phi i32 [ 1, %31 ], [ 0, %17 ], [ 0, %23 ]
ret i32 %38
}