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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ bzero(char **, int /*<<< orphan*/);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  char rbuf[33];
  char *wbuf = "test for write";
  fd = open("/dev/virtualdisk", O_RDWR, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
  if (fd == -1) {
    printf("open failed!\n");
    exit(EXIT_FAILURE);
  }
  if (argc > 1)
    wbuf = argv[1];
  if (lseek(fd, 0, SEEK_SET) == -1) {
    printf("lseek failed!\n");
    exit(EXIT_FAILURE);
  }
  printf("%s\n", wbuf);
  if (write(fd, wbuf, strlen(wbuf)) == -1) {
    printf("write failed!\n");
    exit(EXIT_FAILURE);
  }
  if (lseek(fd, 0, SEEK_SET) == -1) {
    printf("lseek failed!\n");
    exit(EXIT_FAILURE);
  }
  bzero(&rbuf, strlen(rbuf));
  if (read(fd, rbuf, sizeof(rbuf)) == -1) {
    printf("read failed!\n");
    exit(EXIT_FAILURE);
  }
  printf("Read from '%s' : |- %s -|\n", "/dev/virtualdisk", rbuf);
  if (close(fd) == -1) {
    printf("close failed!\n");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
mov    0x2df5(%rip),%rax
or     (%rax),%edx
mov    0x2de4(%rip),%rax
or     (%rax),%edx
lea    0xe2a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1213 <main+0x83>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1229 <main+0x99>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1261 <main+0xd1>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x48(%rbp),%rsi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12bd <main+0x12d>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d09(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12f5 <main+0x165>
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
call   10a0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x21,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
jne    1347 <main+0x1b7>
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x40(%rbp),%rdx
lea    0xd0c(%rip),%rdi
lea    0xcb6(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    138d <main+0x1fd>
lea    0xd01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c32(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2c14(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e2c(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e22(%rip),%rax
mov    0x2dfb(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%edx
lea    0xe2e(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    11f4 <main+0x54>
lea    0xe80(%rip),%rdi
jmp    1262 <main+0xc2>
mov    %eax,%ebx
cmp    $0x2,%ebp
jl     1201 <main+0x61>
mov    0x8(%r14),%r14
jmp    1208 <main+0x68>
lea    0xdf8(%rip),%r14
mov    0x2dc1(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
je     125b <main+0xbb>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    124a <main+0xaa>
lea    0xe0e(%rip),%rdi
jmp    1262 <main+0xc2>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xd5>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   10b0 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x21,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
jne    12a7 <main+0x107>
lea    0xda4(%rip),%rdi
jmp    1262 <main+0xc2>
lea    0xd72(%rip),%rdi
lea    0xd5a(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
jne    12d4 <main+0x134>
lea    0xd69(%rip),%rdi
jmp    1262 <main+0xc2>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"test for write\00"
@.str.1 = [17 x i8] c"/dev/virtualdisk\00"
@O_RDWR = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [14 x i8] c"open failed!\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [15 x i8] c"lseek failed!\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [15 x i8] c"write failed!\0A\00"
@.str.6 = [14 x i8] c"read failed!\0A\00"
@.str.7 = [27 x i8] c"Read from '%s' : |- %s -|\0A\00"
@.str.8 = [15 x i8] c"close failed!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [33 x i8]
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %8
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @S_IWUSR
%11 = load i32, i32* @S_IRUSR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %8
br label %31
31: ; preds = %27, %24
%32 = load i32, i32* %6
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @lseek(i32 %32, i32 0, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %31
%41 = load i8*, i8** %8
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %6
%44 = load i8*, i8** %8
%45 = load i8*, i8** %8
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %53
49: ; preds = %40
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %40
%54 = load i32, i32* %6
%55 = load i32, i32* @SEEK_SET
%56 = call i32 @lseek(i32 %54, i32 0, i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %62
58: ; preds = %53
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %53
%63 = bitcast [33 x i8]* %7 to i8**
%64 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%65 = call i32 @strlen(i8* %64)
%66 = call i32 @bzero(i8** %63, i32 %65)
%67 = load i32, i32* %6
%68 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%69 = call i32 @read(i32 %67, i8* %68, i32 33)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %75
71: ; preds = %62
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %62
%76 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %76)
%78 = load i32, i32* %6
%79 = call i32 @close(i32 %78)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %85
81: ; preds = %75
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%83 = load i32, i32* @EXIT_FAILURE
%84 = call i32 @exit(i32 %83)
unreachable
85: ; preds = %75
%86 = load i32, i32* @EXIT_SUCCESS
%87 = call i32 @exit(i32 %86)
unreachable
}

LLVM IR (-O2):
@.str = [15 x i8] c"test for write\00"
@.str.1 = [17 x i8] c"/dev/virtualdisk\00"
@O_RDWR = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.7 = [27 x i8] c"Read from '%s' : |- %s -|\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [14 x i8] c"close failed!\00"
@str.9 = [13 x i8] c"read failed!\00"
@str.11 = [14 x i8] c"write failed!\00"
@str.12 = [14 x i8] c"lseek failed!\00"
@str.13 = [13 x i8] c"open failed!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [33 x i8]
%4 = getelementptr inbounds [33 x i8], [33 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @S_IWUSR
%7 = load i32, i32* @S_IRUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.13, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = icmp sgt i32 %0, 1
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
br label %24
24: ; preds = %21, %19
%25 = phi i8* [ %23, %21 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %19 ]
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @lseek(i32 %13, i32 0, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.12, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = tail call i32 @puts(i8* dereferenceable(1) %25)
%35 = tail call i32 @strlen(i8* %25)
%36 = tail call i32 @write(i32 %13, i8* %25, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
%43 = load i32, i32* @SEEK_SET
%44 = tail call i32 @lseek(i32 %13, i32 0, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.12, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = tail call i32 @exit(i32 %48)
unreachable
50: ; preds = %42
%51 = bitcast [33 x i8]* %3 to i8**
%52 = call i32 @strlen(i8* %4)
%53 = call i32 @bzero(i8** %51, i32 %52)
%54 = call i32 @read(i32 %13, i8* %4, i32 33)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %50
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %50
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%62 = call i32 @close(i32 %13)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %68
64: ; preds = %60
%65 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %60
%69 = load i32, i32* @EXIT_SUCCESS
%70 = call i32 @exit(i32 %69)
unreachable
}