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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  if ((fd = open(argv[1], O_RDWR)) == -1) {
    err(1, "open failed");
  }
  if (lseek(fd, 1024, SEEK_SET) == (off_t)-1) {
    err(1, "lseek failed");
  }
  char buffer[512];
  switch (write(fd, buffer, 512)) {
  case 512:
    break;
  case -1:
    close(fd);
    err(1, "write failed");
  default:
    close(fd);
    errx(1, "write incomplete");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11ac <main+0x4c>
mov    $0x1,%edi
lea    0xe59(%rip),%rsi
call   1070 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11dd <main+0x7d>
mov    $0x1,%edi
lea    0xe34(%rip),%rsi
call   1070 <err@plt>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    %ecx,-0x224(%rbp)
sub    $0xffffffff,%eax
je     1222 <main+0xc2>
jmp    1207 <main+0xa7>
mov    -0x224(%rbp),%eax
sub    $0x200,%eax
jne    123b <main+0xdb>
jmp    121d <main+0xbd>
jmp    1254 <main+0xf4>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
lea    0xde3(%rip),%rsi
call   1070 <err@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
lea    0xdd7(%rip),%rsi
call   1040 <errx@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x200,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1192 <main+0x32>
lea    0xe78(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x400,%esi
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11be <main+0x5e>
lea    0xe58(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1030 <write@plt>
cmp    $0x200,%eax
je     1209 <main+0xa9>
cmp    $0xffffffff,%eax
jne    11f1 <main+0x91>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe32(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe27(%rip),%rsi
mov    $0x1,%edi
call   1040 <errx@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [12 x i8] c"open failed\00"
@SEEK_SET = global i32 0
@.str.1 = [13 x i8] c"lseek failed\00"
@.str.2 = [13 x i8] c"write failed\00"
@.str.3 = [17 x i8] c"write incomplete\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* %6
%18 = load i32, i32* @SEEK_SET
%19 = call i64 @lseek(i32 %17, i32 1024, i32 %18)
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %6
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%26 = call i32 @write(i32 %24, i8* %25, i32 512)
switch i32 %26, label %32 [
i32 512, label %27
i32 -1, label %28
]
27: ; preds = %23
br label %36
28: ; preds = %23
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
%31 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %23, %28
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = call i32 @errx(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %32, %27
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [12 x i8] c"open failed\00"
@SEEK_SET = global i32 0
@.str.1 = [13 x i8] c"lseek failed\00"
@.str.2 = [13 x i8] c"write failed\00"
@.str.3 = [17 x i8] c"write incomplete\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* @SEEK_SET
%13 = tail call i64 @lseek(i32 %7, i32 1024, i32 %12)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%19 = call i32 @write(i32 %7, i8* %18, i32 512)
switch i32 %19, label %23 [
i32 512, label %26
i32 -1, label %20
]
20: ; preds = %17
%21 = tail call i32 @close(i32 %7)
%22 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %17, %20
%24 = tail call i32 @close(i32 %7)
%25 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %23, %17
%27 = tail call i32 @close(i32 %7)
ret i32 0
}