#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 *, char *);
int write(int, unsigned int *, int);
int write_sector_num_to_p4(unsigned long disk_size) {
  unsigned int p4_sector = disk_size;
  int fd = -1;
  fd = open("/dev/mmcblk0", O_RDWR);
  if (fd < 0) {
    printf("open %s failed.\n", "/dev/mmcblk0");
    return -1;
  }
  lseek(fd, 0x1fa, SEEK_SET);
  if (write(fd, &p4_sector, sizeof(unsigned int)) != sizeof(unsigned int)) {
    printf("write %s failed.\n", "/dev/mmcblk0");
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_sector_num_to_p4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ab <write_sector_num_to_p4+0x5b>
lea    0xe7a(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <write_sector_num_to_p4+0xc4>
mov    -0x18(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    $0x1fa,%esi
call   1050 <lseek@plt>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
je     1205 <write_sector_num_to_p4+0xb5>
lea    0xe39(%rip),%rdi
lea    0xe14(%rip),%rsi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <write_sector_num_to_p4+0xc4>
mov    -0x18(%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_sector_num_to_p4>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,0xc(%rsp)
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11aa <write_sector_num_to_p4+0x5a>
mov    %eax,%ebx
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x1fa,%esi
call   1050 <lseek@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    11bf <write_sector_num_to_p4+0x6f>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11de <write_sector_num_to_p4+0x8e>
lea    0xe5c(%rip),%rdi
lea    0xe48(%rip),%rsi
call   1040 <printf@plt>
jmp    11d9 <write_sector_num_to_p4+0x89>
lea    0xe58(%rip),%rdi
lea    0xe33(%rip),%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
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"
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"write %s failed.\0A\00"
define i32 @write_sector_num_to_p4(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i64 %0, i64* %3
%6 = load i64, i64* %3
%7 = trunc i64 %6 to i32
store i32 %7, i32* %4
store i32 -1, i32* %5
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @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 %29
14: ; preds = %1
%15 = load i32, i32* %5
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @lseek(i32 %15, i32 506, i32 %16)
%18 = load i32, i32* %5
%19 = call i32 @write(i32 %18, i32* %4, i32 4)
%20 = sext i32 %19 to i64
%21 = icmp ne i64 %20, 4
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %2
br label %29
26: ; preds = %14
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
store i32 0, i32* %2
br label %29
29: ; preds = %26, %22, %12
%30 = load i32, i32* %2
ret i32 %30
}

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"
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"write %s failed.\0A\00"
define i32 @write_sector_num_to_p4(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = trunc i64 %0 to i32
store i32 %4, i32* %2
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @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))
br label %20
10: ; preds = %1
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %6, i32 506, i32 %11)
%13 = call i32 @write(i32 %6, i32* %2, i32 4)
%14 = icmp eq i32 %13, 4
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @close(i32 %6)
br label %20
18: ; preds = %10
%19 = tail call i32 @close(i32 %6)
br label %20
20: ; preds = %18, %15, %8
%21 = phi i32 [ -1, %8 ], [ -1, %15 ], [ 0, %18 ]
ret i32 %21
}