#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 double off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int lseek(int, double, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int write_block(const char *dev_path, int fd, const char *block_name,
                int block_num, char *data, int data_len) {
  int rc;
  off_t seek_pos = block_num * 1024;
  rc = lseek(fd, seek_pos, SEEK_SET);
  if (rc == -1) {
    printf("%failed seeking into position %lu "
           "of file '%s'\n",
           seek_pos, dev_path);
    return 0;
  }
  if (rc != seek_pos) {
    printf("failed seeking into position %lu of file '%s' - "
           "lseek returned %d.\n",
           seek_pos, dev_path, rc);
    return 0;
  }
  rc = write(fd, data, data_len);
  if (rc == -1) {
    printf("failed writing '%s' block into '%s' \n", block_name, dev_path);
    return 0;
  }
  if (rc != data_len) {
    printf("got only partial write when writing '%s' block "
           " into position %lu of file '%s'.\n",
           block_name, seek_pos, dev_path);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<write_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
mov    -0x24(%rbp),%eax
shl    $0xa,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x14(%rbp),%edi
movsd  -0x40(%rbp),%xmm0
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
call   1050 <lseek@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11a3 <write_block+0x73>
movsd  -0x40(%rbp),%xmm0
mov    -0x10(%rbp),%rsi
lea    0xe70(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125c <write_block+0x12c>
cvtsi2sdl -0x38(%rbp),%xmm0
ucomisd -0x40(%rbp),%xmm0
jne    11be <write_block+0x8e>
jp     11be <write_block+0x8e>
jmp    11e4 <write_block+0xb4>
movsd  -0x40(%rbp),%xmm0
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    0xe5f(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125c <write_block+0x12c>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1222 <write_block+0xf2>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125c <write_block+0x12c>
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
je     1255 <write_block+0x125>
mov    -0x20(%rbp),%rsi
movsd  -0x40(%rbp),%xmm0
mov    -0x10(%rbp),%rdx
lea    0xe58(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125c <write_block+0x12c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_block>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r9d,%r12d
mov    %r8,%rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
shl    $0xa,%ecx
cvtsi2sd %ecx,%xmm0
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
movsd  %xmm0,0x8(%rsp)
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     119a <write_block+0x6a>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jne    1181 <write_block+0x51>
jnp    11c2 <write_block+0x92>
movapd %xmm1,%xmm0
lea    0xea4(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
mov    $0x1,%al
call   1040 <printf@plt>
jmp    11b1 <write_block+0x81>
lea    0xe5f(%rip),%rdi
movsd  0x8(%rsp),%xmm0
mov    %r15,%rsi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11fa <write_block+0xca>
mov    $0x1,%ebx
cmp    %r12d,%eax
je     11b3 <write_block+0x83>
lea    0xeb5(%rip),%rdi
mov    %r14,%rsi
movsd  0x8(%rsp),%xmm0
mov    %r15,%rdx
mov    $0x1,%al
call   1040 <printf@plt>
jmp    11b1 <write_block+0x81>
lea    0xe73(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b3 <write_block+0x83>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [48 x i8] c"%failed seeking into position %lu of file '%s'\0A\00"
@.str.1 = [68 x i8] c"failed seeking into position %lu of file '%s' - lseek returned %d.\0A\00"
@.str.2 = [38 x i8] c"failed writing '%s' block into '%s' \0A\00"
@.str.3 = [81 x i8] c"got only partial write when writing '%s' block into position %lu of file '%s'.\0A\00"
define i32 @write_block(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca double
store i8* %0, i8** %8
store i32 %1, i32* %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%16 = load i32, i32* %11
%17 = mul nsw i32 %16, 1024
%18 = sitofp i32 %17 to double
store double %18, double* %15
%19 = load i32, i32* %9
%20 = load double, double* %15
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @lseek(i32 %19, double %20, i32 %21)
store i32 %22, i32* %14
%23 = load i32, i32* %14
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %6
%26 = load double, double* %15
%27 = load i8*, i8** %8
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), double %26, i8* %27)
store i32 0, i32* %7
br label %60
29: ; preds = %6
%30 = load i32, i32* %14
%31 = sitofp i32 %30 to double
%32 = load double, double* %15
%33 = fcmp une double %31, %32
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load double, double* %15
%36 = load i8*, i8** %8
%37 = load i32, i32* %14
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), double %35, i8* %36, i32 %37)
store i32 0, i32* %7
br label %60
39: ; preds = %29
%40 = load i32, i32* %9
%41 = load i8*, i8** %12
%42 = load i32, i32* %13
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
store i32 %43, i32* %14
%44 = load i32, i32* %14
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %39
%47 = load i8*, i8** %10
%48 = load i8*, i8** %8
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %47, i8* %48)
store i32 0, i32* %7
br label %60
50: ; preds = %39
%51 = load i32, i32* %14
%52 = load i32, i32* %13
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = load i8*, i8** %10
%56 = load double, double* %15
%57 = load i8*, i8** %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.3, i64 0, i64 0), i8* %55, double %56, i8* %57)
store i32 0, i32* %7
br label %60
59: ; preds = %50
store i32 1, i32* %7
br label %60
60: ; preds = %59, %54, %46, %34, %25
%61 = load i32, i32* %7
ret i32 %61
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [48 x i8] c"%failed seeking into position %lu of file '%s'\0A\00"
@.str.1 = [68 x i8] c"failed seeking into position %lu of file '%s' - lseek returned %d.\0A\00"
@.str.2 = [38 x i8] c"failed writing '%s' block into '%s' \0A\00"
@.str.3 = [81 x i8] c"got only partial write when writing '%s' block into position %lu of file '%s'.\0A\00"
define i32 @write_block(i8* %0, i32 %1, i8* %2, i32 %3, i8* nocapture %4, i32 %5) {
%7 = shl nsw i32 %3, 10
%8 = sitofp i32 %7 to double
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @lseek(i32 %1, double %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), double %8, i8* %0)
br label %28
14: ; preds = %6
%15 = sitofp i32 %10 to double
%16 = fcmp une double %15, %8
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), double %8, i8* %0, i32 %10)
br label %28
19: ; preds = %14
%20 = tail call i32 @write(i32 %1, i8* %4, i32 %5)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %0)
br label %28
24: ; preds = %19
%25 = icmp eq i32 %20, %5
br i1 %25, label %28, label %26
26: ; preds = %24
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @.str.3, i64 0, i64 0), i8* %2, double %8, i8* %0)
br label %28
28: ; preds = %24, %26, %22, %17, %12
%29 = phi i32 [ 0, %12 ], [ 0, %17 ], [ 0, %22 ], [ 0, %26 ], [ 1, %24 ]
ret i32 %29
}