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

/* Variables and functions */
int EINVAL;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ realpath(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, size_t);
int main(int argc, char *argv[]) {
  int fd, len;
  char path[PATH_MAX + 1];
  if (2 != argc)
    return (EINVAL);
  if (0 == realpath(argv[1], path))
    return (errno);
  len = strlen(path) + 1;
  fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
  if (fd >= 0) {
    int blkct, again;
    off_t blksz, total = len, where = 0;
    printf("writing %d bytes at offset %qu\n", len, where);
    (void)write(fd, path, (size_t)len);
    where = 2048;
    printf("writing %d bytes at offset %qu\n", len, where);
    (void)lseek(fd, where, SEEK_SET);
    (void)write(fd, path, (size_t)len);
    total += len;
    goto close;
    for (blkct = 12; blkct < 13; ++blkct) {
      for (blksz = 1024; blksz <= 4096; blksz <<= 1) {
        again = 1;
        where = blksz * blkct;
      dowrite:
        total += len;
        printf("writing %d bytes at offset %qu (%uKB, %uMB)\n", len, where,
               (unsigned)(where >> 10), (unsigned)(where >> 20));
        (void)lseek(fd, where, SEEK_SET);
        (void)write(fd, path, (size_t)len);
        if (again) {
          again = 0;
          where = blksz << blkct;
          goto dowrite;
        }
      }
    }
  close:
    close(fd);
    printf("wrote %qu bytes total\n", total);
  } else
    return (errno);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11db <main+0x6b>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1321 <main+0x1b1>
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <realpath@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1212 <main+0xa2>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1321 <main+0x1b1>
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2d63(%rip),%rax
or     (%rax),%esi
mov    0x2db2(%rip),%rax
or     (%rax),%esi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
mov    0x2d80(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     12fb <main+0x18b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x40(%rbp),%edx
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%rsi
mov    -0x14(%rbp),%edi
movslq -0x18(%rbp),%rdx
call   1030 <write@plt>
movl   $0x800,-0x40(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x40(%rbp),%edx
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    0x2cfb(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x48(%rbp),%rsi
mov    -0x14(%rbp),%edi
movslq -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    -0x18(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x3c(%rbp),%esi
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1313 <main+0x1a3>
mov    0x2cbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1321 <main+0x1b1>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x2,%edi
jne    1286 <main+0x116>
mov    0x8(%rsi),%rdi
mov    %rbx,%rsi
call   1080 <realpath@plt>
test   %rax,%rax
je     128f <main+0x11f>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    0x2e2d(%rip),%rax
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e13(%rip),%rax
or     (%rax),%esi
mov    0x2dca(%rip),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     128f <main+0x11f>
mov    %eax,%r15d
add    $0x1,%r12d
lea    0xdf1(%rip),%r13
xor    %r14d,%r14d
mov    %r13,%rdi
mov    %r12d,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movslq %r12d,%r12
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1030 <write@plt>
mov    %r13,%rdi
mov    %r12d,%esi
mov    $0x800,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    $0x800,%esi
call   1060 <lseek@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1030 <write@plt>
lea    (%r12,%r12,1),%ebx
mov    %r15d,%edi
call   1070 <close@plt>
lea    0xda5(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1299 <main+0x129>
mov    0x2d1b(%rip),%rax
jmp    1296 <main+0x126>
mov    0x2d2a(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [32 x i8] c"writing %d bytes at offset %qu\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [45 x i8] c"writing %d bytes at offset %qu (%uKB, %uMB)\0A\00"
@.str.2 = [23 x i8] c"wrote %qu bytes total\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* @PATH_MAX
%17 = add nsw i32 %16, 1
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %8
%20 = alloca i8, i64 %18
store i64 %18, i64* %9
%21 = load i32, i32* %4
%22 = icmp ne i32 2, %21
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = load i32, i32* @EINVAL
store i32 %24, i32* %3
store i32 1, i32* %10
br label %124
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i64 @realpath(i8* %28, i8* %20)
%30 = icmp eq i64 0, %29
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = load i32, i32* @errno
store i32 %32, i32* %3
store i32 1, i32* %10
br label %124
33: ; preds = %25
%34 = call i32 @strlen(i8* %20)
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
%36 = load i32, i32* @O_CREAT
%37 = load i32, i32* @O_WRONLY
%38 = or i32 %36, %37
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IRUSR
%42 = load i32, i32* @S_IWUSR
%43 = or i32 %41, %42
%44 = call i32 @open(i8* %20, i32 %40, i32 %43)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp sge i32 %45, 0
br i1 %46, label %47, label %121
47: ; preds = %33
%48 = load i32, i32* %7
store i32 %48, i32* %14
store i32 0, i32* %15
%49 = load i32, i32* %7
%50 = load i32, i32* %15
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %49, i32 %50)
%52 = load i32, i32* %6
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = call i32 @write(i32 %52, i8* %20, i64 %54)
store i32 2048, i32* %15
%56 = load i32, i32* %7
%57 = load i32, i32* %15
%58 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %56, i32 %57)
%59 = load i32, i32* %6
%60 = load i32, i32* %15
%61 = load i32, i32* @SEEK_SET
%62 = call i32 @lseek(i32 %59, i32 %60, i32 %61)
%63 = load i32, i32* %6
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = call i32 @write(i32 %63, i8* %20, i64 %65)
%67 = load i32, i32* %7
%68 = load i32, i32* %14
%69 = add nsw i32 %68, %67
store i32 %69, i32* %14
br label %116
70: ; No predecessors!
store i32 12, i32* %11
br label %71
71: ; preds = %112, %70
%72 = load i32, i32* %11
%73 = icmp slt i32 %72, 13
br i1 %73, label %74, label %115
74: ; preds = %71
store i32 1024, i32* %13
br label %75
75: ; preds = %108, %74
%76 = load i32, i32* %13
%77 = icmp sle i32 %76, 4096
br i1 %77, label %78, label %111
78: ; preds = %75
store i32 1, i32* %12
%79 = load i32, i32* %13
%80 = load i32, i32* %11
%81 = mul nsw i32 %79, %80
store i32 %81, i32* %15
br label %82
82: ; preds = %103, %78
%83 = load i32, i32* %7
%84 = load i32, i32* %14
%85 = add nsw i32 %84, %83
store i32 %85, i32* %14
%86 = load i32, i32* %7
%87 = load i32, i32* %15
%88 = load i32, i32* %15
%89 = ashr i32 %88, 10
%90 = load i32, i32* %15
%91 = ashr i32 %90, 20
%92 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %86, i32 %87, i32 %89, i32 %91)
%93 = load i32, i32* %6
%94 = load i32, i32* %15
%95 = load i32, i32* @SEEK_SET
%96 = call i32 @lseek(i32 %93, i32 %94, i32 %95)
%97 = load i32, i32* %6
%98 = load i32, i32* %7
%99 = sext i32 %98 to i64
%100 = call i32 @write(i32 %97, i8* %20, i64 %99)
%101 = load i32, i32* %12
%102 = icmp ne i32 %101, 0
br i1 %102, label %103, label %107
103: ; preds = %82
store i32 0, i32* %12
%104 = load i32, i32* %13
%105 = load i32, i32* %11
%106 = shl i32 %104, %105
store i32 %106, i32* %15
br label %82
107: ; preds = %82
br label %108
108: ; preds = %107
%109 = load i32, i32* %13
%110 = shl i32 %109, 1
store i32 %110, i32* %13
br label %75
111: ; preds = %75
br label %112
112: ; preds = %111
%113 = load i32, i32* %11
%114 = add nsw i32 %113, 1
store i32 %114, i32* %11
br label %71
115: ; preds = %71
br label %116
116: ; preds = %115, %47
%117 = load i32, i32* %6
%118 = call i32 @close(i32 %117)
%119 = load i32, i32* %14
%120 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %119)
br label %123
121: ; preds = %33
%122 = load i32, i32* @errno
store i32 %122, i32* %3
store i32 1, i32* %10
br label %124
123: ; preds = %116
store i32 0, i32* %3
store i32 1, i32* %10
br label %124
124: ; preds = %123, %121, %31, %23
%125 = load i8*, i8** %8
%126 = load i32, i32* %3
ret i32 %126
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [32 x i8] c"writing %d bytes at offset %qu\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [23 x i8] c"wrote %qu bytes total\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = load i32, i32* @EINVAL
br label %43
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i64 @realpath(i8* %12, i8* %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i32, i32* @errno
br label %43
17: ; preds = %10
%18 = call i32 @strlen(i8* %6)
%19 = add nsw i32 %18, 1
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_WRONLY
%22 = or i32 %21, %20
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %26, %25
%28 = call i32 @open(i8* %6, i32 %24, i32 %27)
%29 = icmp sgt i32 %28, -1
br i1 %29, label %30, label %41
30: ; preds = %17
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %19, i32 0)
%32 = sext i32 %19 to i64
%33 = call i32 @write(i32 %28, i8* %6, i64 %32)
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %19, i32 2048)
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %28, i32 2048, i32 %35)
%37 = call i32 @write(i32 %28, i8* %6, i64 %32)
%38 = shl nsw i32 %19, 1
%39 = call i32 @close(i32 %28)
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %38)
br label %43
41: ; preds = %17
%42 = load i32, i32* @errno
br label %43
43: ; preds = %30, %41, %15, %8
%44 = phi i32 [ %9, %8 ], [ %16, %15 ], [ 0, %30 ], [ %42, %41 ]
ret i32 %44
}