#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 O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ atoi(char *);
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  if (argc != 4) {
    fprintf(stderr, "Usage: %s file offset text\n", argv[0]);
    return 1;
  }
  int fd = open(argv[1], O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    fprintf(stderr, "open: %s\n", strerror(errno));
    return 1;
  }
  if (lseek(fd, atoi(argv[2]), SEEK_SET) == -1) {
    fprintf(stderr, "lseek: %s\n", strerror(errno));
    return 1;
  }
  int len = strlen(argv[3]);
  if (write(fd, argv[3], len) != len) {
    fprintf(stderr, "write: %s\n", strerror(errno));
    return 1;
  }
  if (close(fd) == -1) {
    fprintf(stderr, "close: %s\n", strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11c8 <main+0x48>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1362 <main+0x1e2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    0x2de5(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1247 <main+0xc7>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xde6(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1362 <main+0x1e2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12ae <main+0x12e>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd89(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1362 <main+0x1e2>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     1312 <main+0x192>
mov    0x2cef(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd30(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1362 <main+0x1e2>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    135b <main+0x1db>
mov    0x2ca6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xcf2(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1362 <main+0x1e2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x4,%edi
jne    124b <main+0xcb>
mov    0x8(%rbx),%rdi
mov    0x2e0d(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
mov    0x2e02(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1260 <main+0xe0>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%edx
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     1280 <main+0x100>
mov    0x18(%rbx),%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    0x18(%rbx),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
jne    12a0 <main+0x120>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    12cd <main+0x14d>
mov    0x2d9e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdf3(%rip),%rsi
jmp    12be <main+0x13e>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xda2(%rip),%rsi
jmp    12c3 <main+0x143>
mov    0x2d69(%rip),%rax
mov    (%rax),%ebx
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd9e(%rip),%rsi
jmp    12be <main+0x13e>
mov    0x2d49(%rip),%rax
mov    (%rax),%ebx
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd88(%rip),%rsi
jmp    12be <main+0x13e>
mov    0x2d29(%rip),%rax
mov    (%rax),%ebx
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd73(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Usage: %s file offset text\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [10 x i8] c"open: %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [11 x i8] c"lseek: %s\0A\00"
@.str.3 = [11 x i8] c"write: %s\0A\00"
@.str.4 = [11 x i8] c"close: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 4
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 1, i32* %3
br label %78
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = load i32, i32* @O_APPEND
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %19, i32 %24, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %16
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 1, i32* %3
br label %78
36: ; preds = %16
%37 = load i32, i32* %6
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i32 @atoi(i8* %40)
%42 = load i32, i32* @SEEK_SET
%43 = call i32 @lseek(i32 %37, i32 %41, i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %50
45: ; preds = %36
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %48)
store i32 1, i32* %3
br label %78
50: ; preds = %36
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 3
%53 = load i8*, i8** %52
%54 = call i32 @strlen(i8* %53)
store i32 %54, i32* %7
%55 = load i32, i32* %6
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 3
%58 = load i8*, i8** %57
%59 = load i32, i32* %7
%60 = call i32 @write(i32 %55, i8* %58, i32 %59)
%61 = load i32, i32* %7
%62 = icmp ne i32 %60, %61
br i1 %62, label %63, label %68
63: ; preds = %50
%64 = load i32, i32* @stderr
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %66)
store i32 1, i32* %3
br label %78
68: ; preds = %50
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %77
72: ; preds = %68
%73 = load i32, i32* @stderr
%74 = load i32, i32* @errno
%75 = call i8* @strerror(i32 %74)
%76 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %75)
store i32 1, i32* %3
br label %78
77: ; preds = %68
store i32 0, i32* %3
br label %78
78: ; preds = %77, %72, %63, %45, %31, %10
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Usage: %s file offset text\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [10 x i8] c"open: %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [11 x i8] c"lseek: %s\0A\00"
@.str.3 = [11 x i8] c"write: %s\0A\00"
@.str.4 = [11 x i8] c"close: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %58
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_APPEND
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %10, i32 %15, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %8
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %58
26: ; preds = %8
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = tail call i32 @atoi(i8* %28)
%30 = load i32, i32* @SEEK_SET
%31 = tail call i32 @lseek(i32 %19, i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %58
38: ; preds = %26
%39 = getelementptr inbounds i8*, i8** %1, i64 3
%40 = load i8*, i8** %39
%41 = tail call i32 @strlen(i8* %40)
%42 = load i8*, i8** %39
%43 = tail call i32 @write(i32 %19, i8* %42, i32 %41)
%44 = icmp eq i32 %43, %41
br i1 %44, label %50, label %45
45: ; preds = %38
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = tail call i8* @strerror(i32 %47)
%49 = tail call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %58
50: ; preds = %38
%51 = tail call i32 @close(i32 %19)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = tail call i8* @strerror(i32 %55)
%57 = tail call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %21, %33, %50, %53, %45, %4
%59 = phi i32 [ 1, %4 ], [ 1, %21 ], [ 1, %33 ], [ 1, %45 ], [ 1, %53 ], [ 0, %50 ]
ret i32 %59
}