#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*/ uint8_t;
typedef int /*<<< orphan*/ uint32_t;
struct emmc_block_dev {
  int dummy;
};
struct block_device {
  int dummy;
};

/* Variables and functions */
scalar_t__ sd_do_data_command(struct emmc_block_dev *, int,
                              int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
scalar_t__ sd_ensure_data_mode(struct emmc_block_dev *);
int sd_write(struct block_device *dev, uint8_t *buf, size_t buf_size,
             uint32_t block_no) {
  struct emmc_block_dev *edev = (struct emmc_block_dev *)dev;
  if (sd_ensure_data_mode(edev) != 0)
    return -1;
  if (sd_do_data_command(edev, 1, buf, buf_size, block_no) < 0)
    return -1;
  return buf_size;
}


x86 assembly (clang, -O0):
<sd_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <sd_ensure_data_mode@plt>
cmp    $0x0,%rax
je     115e <sd_write+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <sd_write+0x75>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    $0x1,%esi
call   1040 <sd_do_data_command@plt>
cmp    $0x0,%rax
jge    118e <sd_write+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <sd_write+0x75>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sd_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%rbp
call   1030 <sd_ensure_data_mode@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1166 <sd_write+0x46>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %r14d,%r8d
call   1040 <sd_do_data_command@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.block_device = type { i32 }
%struct.emmc_block_dev = type { i32 }
define i32 @sd_write(%struct.block_device* %0, i32* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.block_device*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.emmc_block_dev*
store %struct.block_device* %0, %struct.block_device** %6
store i32* %1, i32** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%11 = load %struct.block_device*, %struct.block_device** %6
%12 = bitcast %struct.block_device* %11 to %struct.emmc_block_dev*
store %struct.emmc_block_dev* %12, %struct.emmc_block_dev** %10
%13 = load %struct.emmc_block_dev*, %struct.emmc_block_dev** %10
%14 = call i64 @sd_ensure_data_mode(%struct.emmc_block_dev* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %28
17: ; preds = %4
%18 = load %struct.emmc_block_dev*, %struct.emmc_block_dev** %10
%19 = load i32*, i32** %7
%20 = load i64, i64* %8
%21 = load i32, i32* %9
%22 = call i64 @sd_do_data_command(%struct.emmc_block_dev* %18, i32 1, i32* %19, i64 %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %5
br label %28
25: ; preds = %17
%26 = load i64, i64* %8
%27 = trunc i64 %26 to i32
store i32 %27, i32* %5
br label %28
28: ; preds = %25, %24, %16
%29 = load i32, i32* %5
ret i32 %29
}

LLVM IR (-O2):
%struct.block_device = type { i32 }
%struct.emmc_block_dev = type { i32 }
define i32 @sd_write(%struct.block_device* %0, i32* %1, i64 %2, i32 %3) {
%5 = bitcast %struct.block_device* %0 to %struct.emmc_block_dev*
%6 = tail call i64 @sd_ensure_data_mode(%struct.emmc_block_dev* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = tail call i64 @sd_do_data_command(%struct.emmc_block_dev* %5, i32 1, i32* %1, i64 %2, i32 %3)
%10 = icmp slt i64 %9, 0
%11 = trunc i64 %2 to i32
%12 = select i1 %10, i32 -1, i32 %11
br label %13
13: ; preds = %8, %4
%14 = phi i32 [ -1, %4 ], [ %12, %8 ]
ret i32 %14
}