#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*/ SEEK_SET;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned int *, int);
int write(int, char *, int);
int mtd_test(int fd, int offset, char *buff, int size) {
  int i;
  int ret;
  unsigned int val;
  unsigned int *p = (unsigned int *)buff;
  lseek(fd, offset, SEEK_SET);
  ret = write(fd, buff, size);
  if (ret != size) {
    printf("failed to write mtd buff: %d bytes writes vs %d\n", ret, size);
    goto err;
  }
  lseek(fd, offset, SEEK_SET);
  for (i = 0; i < size / sizeof(unsigned int); i++) {
    ret = read(fd, &val, sizeof(unsigned int));
    if (ret != sizeof(unsigned int)) {
      printf("failed to read mtd buff\n");
      break;
    }
    if (val != p[i])
      printf("invalid read value: 0x%x != 0x%x\n", val, p[i]);
  }
err:
  return ret;
}


x86 assembly (clang, -O0):
<mtd_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11a8 <mtd_test+0x68>
mov    -0x1c(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1255 <mtd_test+0x115>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
movl   $0x0,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movslq -0x14(%rbp),%rcx
shr    $0x2,%rcx
cmp    %rcx,%rax
jae    1250 <mtd_test+0x110>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
je     120d <mtd_test+0xcd>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1250 <mtd_test+0x110>
mov    -0x20(%rbp),%eax
mov    -0x28(%rbp),%rcx
movslq -0x18(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
je     123d <mtd_test+0xfd>
mov    -0x20(%rbp),%esi
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1242 <mtd_test+0x102>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c3 <mtd_test+0x83>
jmp    1255 <mtd_test+0x115>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtd_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
mov    0x2e74(%rip),%r13
mov    0x0(%r13),%edx
call   1060 <lseek@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
jne    11f9 <mtd_test+0xa9>
mov    0x0(%r13),%edx
mov    %ebp,%edi
mov    %r15d,%esi
call   1060 <lseek@plt>
cmp    $0x4,%ebx
jb     1228 <mtd_test+0xd8>
movslq %ebx,%rax
shr    $0x2,%rax
cmp    $0x2,%rax
mov    $0x1,%r13d
cmovae %rax,%r13
lea    0x4(%rsp),%r15
xor    %r12d,%r12d
jmp    11c9 <mtd_test+0x79>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12
cmp    %r12,%r13
je     1223 <mtd_test+0xd3>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x4,%eax
jne    1213 <mtd_test+0xc3>
mov    0x4(%rsp),%esi
mov    (%r14,%r12,4),%edx
cmp    %edx,%esi
je     11c0 <mtd_test+0x70>
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11c0 <mtd_test+0x70>
mov    %eax,%r12d
lea    0xdfd(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%ebx
jmp    1228 <mtd_test+0xd8>
mov    %eax,%ebx
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
jmp    1228 <mtd_test+0xd8>
mov    $0x4,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [49 x i8] c"failed to write mtd buff: %d bytes writes vs %d\0A\00"
@.str.1 = [25 x i8] c"failed to read mtd buff\0A\00"
@.str.2 = [34 x i8] c"invalid read value: 0x%x != 0x%x\0A\00"
define i32 @mtd_test(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = load i8*, i8** %7
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %12
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @lseek(i32 %15, i32 %16, i32 %17)
%19 = load i32, i32* %5
%20 = load i8*, i8** %7
%21 = load i32, i32* %8
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = load i32, i32* %8
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %4
%27 = load i32, i32* %10
%28 = load i32, i32* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %28)
br label %71
30: ; preds = %4
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @lseek(i32 %31, i32 %32, i32 %33)
store i32 0, i32* %9
br label %35
35: ; preds = %67, %30
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = udiv i64 %39, 4
%41 = icmp ult i64 %37, %40
br i1 %41, label %42, label %70
42: ; preds = %35
%43 = load i32, i32* %5
%44 = call i32 @read(i32 %43, i32* %11, i32 4)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = icmp ne i64 %46, 4
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %70
50: ; preds = %42
%51 = load i32, i32* %11
%52 = load i32*, i32** %12
%53 = load i32, i32* %9
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = icmp ne i32 %51, %56
br i1 %57, label %58, label %66
58: ; preds = %50
%59 = load i32, i32* %11
%60 = load i32*, i32** %12
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
%64 = load i32, i32* %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %59, i32 %64)
br label %66
66: ; preds = %58, %50
br label %67
67: ; preds = %66
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
store i32 %69, i32* %9
br label %35
70: ; preds = %48, %35
br label %71
71: ; preds = %70, %26
%72 = load i32, i32* %10
ret i32 %72
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [49 x i8] c"failed to write mtd buff: %d bytes writes vs %d\0A\00"
@.str.2 = [34 x i8] c"invalid read value: 0x%x != 0x%x\0A\00"
@str = [24 x i8] c"failed to read mtd buff\00"
define i32 @mtd_test(i32 %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = bitcast i8* %2 to i32*
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @lseek(i32 %0, i32 %1, i32 %8)
%10 = tail call i32 @write(i32 %0, i8* %2, i32 %3)
%11 = icmp eq i32 %10, %3
br i1 %11, label %14, label %12
12: ; preds = %4
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %3)
br label %38
14: ; preds = %4
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %0, i32 %1, i32 %15)
%17 = icmp ult i32 %3, 4
br i1 %17, label %38, label %18
18: ; preds = %14
%19 = sext i32 %3 to i64
%20 = lshr i64 %19, 2
%21 = call i64 @llvm.umax.i64(i64 %20, i64 1)
br label %22
22: ; preds = %18, %35
%23 = phi i64 [ 0, %18 ], [ %36, %35 ]
%24 = call i32 @read(i32 %0, i32* %5, i32 4)
%25 = icmp eq i32 %24, 4
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %38
28: ; preds = %22
%29 = load i32, i32* %5
%30 = getelementptr inbounds i32, i32* %7, i64 %23
%31 = load i32, i32* %30
%32 = icmp eq i32 %29, %31
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %31)
br label %35
35: ; preds = %28, %33
%36 = add nuw nsw i64 %23, 1
%37 = icmp eq i64 %36, %21
br i1 %37, label %38, label %22
38: ; preds = %35, %14, %26, %12
%39 = phi i32 [ %10, %12 ], [ %24, %26 ], [ %3, %14 ], [ 4, %35 ]
ret i32 %39
}