#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*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
int qaoed_file_write(int fd, unsigned char *buff, int len, off_t pos) {
  int n;
  if (lseek(fd, pos, SEEK_SET) == -1) {
    perror("fseek()");
    return (-1);
  }
  n = write(fd, buff, len);
  if (n < 1)
    perror("fwrite()\n");
  return (n);
}


x86 assembly (clang, -O0):
<qaoed_file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    0x2e8e(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    117a <qaoed_file_write+0x4a>
lea    0xe97(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <qaoed_file_write+0x78>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    11a2 <qaoed_file_write+0x72>
lea    0xe6b(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qaoed_file_write>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e9d(%rip),%rax
mov    (%rax),%edx
mov    %ecx,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     116b <qaoed_file_write+0x3b>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1179 <qaoed_file_write+0x49>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%ebx
lea    0xe89(%rip),%rdi
jmp    1180 <qaoed_file_write+0x50>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
jmp    1164 <qaoed_file_write+0x34>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [8 x i8] c"fseek()\00"
@.str.1 = [10 x i8] c"fwrite()\0A\00"
define i32 @qaoed_file_write(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %6
%12 = load i32, i32* %9
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @lseek(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %29
18: ; preds = %4
%19 = load i32, i32* %6
%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 = icmp slt i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
%28 = load i32, i32* %10
store i32 %28, i32* %5
br label %29
29: ; preds = %27, %16
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [8 x i8] c"fseek()\00"
@.str.1 = [10 x i8] c"fwrite()\0A\00"
define i32 @qaoed_file_write(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @lseek(i32 %0, i32 %3, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i32 @write(i32 %0, i8* %1, i32 %2)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %8, %4
%12 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %8 ]
%13 = phi i32 [ -1, %4 ], [ %9, %8 ]
%14 = tail call i32 @perror(i8* %12)
br label %15
15: ; preds = %11, %8
%16 = phi i32 [ %9, %8 ], [ %13, %11 ]
ret i32 %16
}