#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 ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int write(int, int /*<<< orphan*/ *, int);
int write8(int fd, off_t addr, uint8_t val) {
  if (lseek(fd, addr, SEEK_SET) != addr) {
    perror("persist: write8: lseek");
    return (1);
  }
  ssize_t b = write(fd, &val, 1);
  if (b < 0) {
    perror("persist: write8: write");
    return (1);
  }
  if (b < 1) {
    fprintf(stderr, "persist: write8: short count\n");
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<write8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    -0x10(%rbp),%rax
je     1189 <write8+0x49>
lea    0xe88(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <write8+0xb1>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bf <write8+0x7f>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <write8+0xb1>
cmpl   $0x1,-0x18(%rbp)
jge    11ea <write8+0xaa>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <write8+0xb1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write8>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %edx,0x4(%rsp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    %rbx,%rax
jne    11a2 <write8+0x62>
lea    0x4(%rsp),%rsi
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11b0 <write8+0x70>
xor    %ebx,%ebx
test   %eax,%eax
jne    1199 <write8+0x59>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
jmp    1194 <write8+0x54>
lea    0xe60(%rip),%rdi
call   1060 <perror@plt>
jmp    1199 <write8+0x59>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [23 x i8] c"persist: write8: lseek\00"
@.str.1 = [23 x i8] c"persist: write8: write\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"persist: write8: short count\0A\00"
define i32 @write8(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i64, i64* %6
%11 = load i32, i32* @SEEK_SET
%12 = call i64 @lseek(i32 %9, i64 %10, i32 %11)
%13 = load i64, i64* %6
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %31
17: ; preds = %3
%18 = load i32, i32* %5
%19 = call i32 @write(i32 %18, i32* %7, i32 1)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %31
24: ; preds = %17
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %4
br label %31
30: ; preds = %24
store i32 0, i32* %4
br label %31
31: ; preds = %30, %27, %22, %15
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [23 x i8] c"persist: write8: lseek\00"
@.str.1 = [23 x i8] c"persist: write8: write\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"persist: write8: short count\0A\00"
define i32 @write8(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
store i32 %2, i32* %4
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i64 %1, i32 %5)
%7 = icmp eq i64 %6, %1
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %20
10: ; preds = %3
%11 = call i32 @write(i32 %0, i32* %4, i32 1)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %20
15: ; preds = %10
%16 = icmp eq i32 %11, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %13, %17, %15, %8
%21 = phi i32 [ 1, %8 ], [ 1, %13 ], [ 1, %17 ], [ 0, %15 ]
ret i32 %21
}