#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_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ O_RDONLY;
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 /*<<< orphan*/);
int read(int, char *, int);
int __mib_flash_read(char *buf, int offset, int len) {
  int fh;
  struct flock fl;
  int ok = 1;
  fl.l_type = F_RDLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;
  fl.l_pid = getpid();
  fh = open(("/var/config/setting.bin"), O_RDONLY);
  if (fh == -1)
    return 0;
  fcntl(fh, F_SETLKW, &fl);
  lseek(fh, offset, SEEK_SET);
  if (read(fh, buf, len) != len)
    ok = 0;
  fl.l_type = F_UNLCK;
  fcntl(fh, F_SETLK, &fl);
  close(fh);
  ("Flash read from %x len=%d\n", offset, len);
  return ok;
}


x86 assembly (clang, -O0):
<__mib_flash_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x1,-0x44(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xe45(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d9 <__mib_flash_read+0x79>
movl   $0x0,-0x4(%rbp)
jmp    1250 <__mib_flash_read+0xf0>
mov    -0x1c(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1070 <read@plt>
cmp    -0x18(%rbp),%eax
je     1221 <__mib_flash_read+0xc1>
movl   $0x0,-0x44(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mib_flash_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r12d
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e35(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0xc(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1225 <__mib_flash_read+0xc5>
mov    %eax,%ebp
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    0x0(%r13),%edx
mov    %ebp,%edi
mov    %r15d,%esi
call   1050 <lseek@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1070 <read@plt>
xor    %ebx,%ebx
cmp    %r12d,%eax
sete   %bl
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%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_RDLCK = global i32 0
@SEEK_SET = global i32 0
@.str = [24 x i8] c"/var/config/setting.bin\00"
@O_RDONLY = global i32 0
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.1 = [27 x i8] c"Flash read from %x len=%d\0A\00"
define i32 @__mib_flash_read(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
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 1, i32* %10
%11 = load i32, i32* @F_RDLCK
%12 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @SEEK_SET
%14 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 4
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 3
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i64 0, i64* %16
%17 = call i32 (...) @getpid()
%18 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %3
store i32 0, i32* %4
br label %50
24: ; preds = %3
%25 = load i32, i32* %8
%26 = load i32, i32* @F_SETLKW
%27 = call i32 @fcntl(i32 %25, i32 %26, %struct.flock* %9)
%28 = load i32, i32* %8
%29 = load i32, i32* %6
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %28, i32 %29, i32 %30)
%32 = load i32, i32* %8
%33 = load i8*, i8** %5
%34 = load i32, i32* %7
%35 = call i32 @read(i32 %32, i8* %33, i32 %34)
%36 = load i32, i32* %7
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %24
store i32 0, i32* %10
br label %39
39: ; preds = %38, %24
%40 = load i32, i32* @F_UNLCK
%41 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %8
%43 = load i32, i32* @F_SETLK
%44 = call i32 @fcntl(i32 %42, i32 %43, %struct.flock* %9)
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %6
%48 = load i32, i32* %7
%49 = load i32, i32* %10
store i32 %49, i32* %4
br label %50
50: ; preds = %39, %23
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i64, i32 }
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@.str = [24 x i8] c"/var/config/setting.bin\00"
@O_RDONLY = global i32 0
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
define i32 @__mib_flash_read(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.flock
%5 = bitcast %struct.flock* %4 to i8*
%6 = load i32, i32* @F_RDLCK
%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_RDONLY
%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 %29, label %17
17: ; preds = %3
%18 = load i32, i32* @F_SETLKW
%19 = call i32 @fcntl(i32 %15, i32 %18, %struct.flock* %4)
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @lseek(i32 %15, i32 %1, i32 %20)
%22 = call i32 @read(i32 %15, i8* %0, i32 %2)
%23 = icmp eq i32 %22, %2
%24 = zext i1 %23 to i32
%25 = load i32, i32* @F_UNLCK
store i32 %25, i32* %7
%26 = load i32, i32* @F_SETLK
%27 = call i32 @fcntl(i32 %15, i32 %26, %struct.flock* %4)
%28 = call i32 @close(i32 %15)
br label %29
29: ; preds = %3, %17
%30 = phi i32 [ %24, %17 ], [ 0, %3 ]
ret i32 %30
}