#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);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, char **, int);
int main(void) {
  int fd;
  char buf[100] = "abc";
  fd = open("/dev/char_device", O_RDWR);
  if (fd < 0) {
    perror("open: ");
    return 1;
  }
  if (lseek(fd, 1, SEEK_SET) < 0) {
    perror("lseek: ");
    return 1;
  }
  if (write(fd, &buf, 4) < 0) {
    perror("write: ");
    return 1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
movb   $0x61,-0x70(%rbp)
movb   $0x62,-0x6f(%rbp)
movb   $0x63,-0x6e(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c5 <main+0x65>
lea    0xe5d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <main+0xdf>
mov    -0x8(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    11fd <main+0x9d>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <main+0xdf>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1230 <main+0xd0>
lea    0xe01(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <main+0xdf>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x60(%rsp)
movw   $0x6261,(%rsp)
movb   $0x63,0x2(%rsp)
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e9 <main+0x99>
mov    %eax,%ebx
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%ebp
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     11f2 <main+0xa2>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1200 <main+0xb0>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe21(%rip),%rdi
jmp    1207 <main+0xb7>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
jmp    11e0 <main+0x90>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebp
jmp    11e0 <main+0x90>

LLVM IR (-O0):
@.str = [17 x i8] c"/dev/char_device\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open: \00"
@SEEK_SET = global i32 0
@.str.2 = [8 x i8] c"lseek: \00"
@.str.3 = [8 x i8] c"write: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
store i32 0, i32* %1
%4 = bitcast [100 x i8]* %3 to i8*
%5 = bitcast i8* %4 to [100 x i8]*
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i32 0, i32 0
store i8 97, i8* %6
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i32 0, i32 1
store i8 98, i8* %7
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i32 0, i32 2
store i8 99, i8* %8
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %32
15: ; preds = %0
%16 = load i32, i32* %2
%17 = load i32, i32* @SEEK_SET
%18 = call i64 @lseek(i32 %16, i32 1, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %32
22: ; preds = %15
%23 = load i32, i32* %2
%24 = bitcast [100 x i8]* %3 to i8**
%25 = call i64 @write(i32 %23, i8** %24, i32 4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %32
29: ; preds = %22
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
store i32 0, i32* %1
br label %32
32: ; preds = %29, %27, %20, %13
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [17 x i8] c"/dev/char_device\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open: \00"
@SEEK_SET = global i32 0
@.str.2 = [8 x i8] c"lseek: \00"
@.str.3 = [8 x i8] c"write: \00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
store i8 97, i8* %2
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 1
store i8 98, i8* %3
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 2
store i8 99, i8* %4
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %24
10: ; preds = %0
%11 = load i32, i32* @SEEK_SET
%12 = tail call i64 @lseek(i32 %6, i32 1, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
16: ; preds = %10
%17 = bitcast [100 x i8]* %1 to i8**
%18 = call i64 @write(i32 %6, i8** %17, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %24
22: ; preds = %16
%23 = tail call i32 @close(i32 %6)
br label %24
24: ; preds = %22, %20, %14, %8
%25 = phi i32 [ 1, %8 ], [ 1, %14 ], [ 1, %20 ], [ 0, %22 ]
ret i32 %25
}