#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 */
struct stat {
  size_t st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ DEFFILEMODE;
int /*<<< orphan*/ EX_NOINPUT;
int /*<<< orphan*/ EX_UNAVAILABLE;
int /*<<< orphan*/ EX_USAGE;
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
int ftruncate(int, int /*<<< orphan*/);
char *malloc(size_t);
scalar_t__ memcmp(char *, char *, size_t);
int open(char const *, int, int /*<<< orphan*/);
int read(int, char *, size_t);
int /*<<< orphan*/ strlcat(char *, char *, size_t);
scalar_t__ strlen(char *);
int /*<<< orphan*/ usage();
int write(int, char *, size_t);
int main(int argc, char *argv[]) {
  struct stat sb;
  char *newcontent = NULL;
  size_t newcontentlength = 0;
  char *oldcontent = NULL;
  int ret;
  int dstfd;
  const char *dst = NULL;
  ssize_t readsize, writesize;
  int i;
  if (argc < 2) {
    usage();
  }
  dst = argv[1];
  for (i = 2; i < argc; i++) {
    newcontentlength += strlen(argv[i]) + 1;
  }
  newcontentlength += 1;
  newcontent = malloc(newcontentlength);
  if (newcontent == NULL)
    err(EX_UNAVAILABLE, "malloc() failed");
  newcontent[0] = '\0';
  for (i = 2; i < argc; i++) {
    strlcat(newcontent, argv[i], newcontentlength);
    if (i < argc - 1) {
      strlcat(newcontent, " ", newcontentlength);
    } else {
      strlcat(newcontent, "\n", newcontentlength);
    }
  }
  dstfd = open(dst, O_RDWR | O_CREAT | O_APPEND, DEFFILEMODE);
  if (dstfd < 0)
    err(EX_NOINPUT, "open(%s)", dst);
  ret = fstat(dstfd, &sb);
  if (ret < 0)
    err(EX_NOINPUT, "fstat(%s)", dst);
  if (!S_ISREG(sb.st_mode))
    err(EX_USAGE, "%s is not a regular file", dst);
  if (sb.st_size != newcontentlength) {
    goto replace;
  }
  oldcontent = malloc(newcontentlength);
  if (oldcontent == NULL)
    err(EX_UNAVAILABLE, "malloc(%lu) failed", newcontentlength);
  readsize = read(dstfd, oldcontent, newcontentlength);
  if (readsize == -1)
    err(EX_UNAVAILABLE, "read() failed");
  else if (readsize != newcontentlength)
    errx(EX_UNAVAILABLE, "short read of file");
  if (0 == memcmp(oldcontent, newcontent, newcontentlength)) {
    free(oldcontent);
    ret = close(dstfd);
    if (ret < 0)
      err(EX_UNAVAILABLE, "close() failed");
    exit(0);
  }
replace:
  ret = ftruncate(dstfd, 0);
  if (ret < 0)
    err(EX_UNAVAILABLE, "ftruncate() failed");
  writesize = write(dstfd, newcontent, newcontentlength);
  if (writesize == -1)
    err(EX_UNAVAILABLE, "write() failed");
  else if (writesize != newcontentlength)
    errx(EX_UNAVAILABLE, "short write of file");
  ret = close(dstfd);
  if (ret < 0)
    err(EX_NOINPUT, "close(dst)");
  return 0;
}


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)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1247 <main+0x47>
mov    $0x0,%al
call   10f0 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
movl   $0x2,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1291 <main+0x91>
mov    -0x10(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <strlen@plt>
add    $0x1,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    125a <main+0x5a>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10e0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12cc <main+0xcc>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xd3b(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x2,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1350 <main+0x150>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <strlcat@plt>
mov    -0x54(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1329 <main+0x129>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xcf1(%rip),%rsi
call   1050 <strlcat@plt>
jmp    133d <main+0x13d>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xcda(%rip),%rsi
call   1050 <strlcat@plt>
jmp    1342 <main+0x142>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12da <main+0xda>
mov    -0x48(%rbp),%rdi
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
mov    0x2c94(%rip),%rax
or     (%rax),%esi
mov    0x2c43(%rip),%rax
or     (%rax),%esi
mov    0x2c32(%rip),%rax
mov    (%rax),%edx
call   1100 <open@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    13a5 <main+0x1a5>
mov    0x2c47(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xc76(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x40(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1120 <fstat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    13d9 <main+0x1d9>
mov    0x2c13(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xc4b(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x18(%rbp),%edi
call   10c0 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1405 <main+0x205>
mov    0x2bdf(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xc29(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1418 <main+0x218>
jmp    150c <main+0x30c>
mov    -0x30(%rbp),%rdi
call   10e0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    144b <main+0x24b>
mov    0x2b89(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xbfc(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x40(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10a0 <read@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    1484 <main+0x284>
mov    0x2b51(%rip),%rax
mov    (%rax),%edi
lea    0xbdb(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
jmp    14ac <main+0x2ac>
movslq -0x4c(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     14a7 <main+0x2a7>
mov    0x2b27(%rip),%rax
mov    (%rax),%edi
lea    0xbbf(%rip),%rsi
call   1070 <errx@plt>
jmp    14ac <main+0x2ac>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10b0 <memcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1507 <main+0x307>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%edi
call   1090 <close@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1500 <main+0x300>
mov    0x2ad0(%rip),%rax
mov    (%rax),%edi
lea    0xb7b(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
xor    %edi,%edi
call   1110 <exit@plt>
jmp    150c <main+0x30c>
mov    -0x40(%rbp),%edi
xor    %esi,%esi
call   1080 <ftruncate@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    153a <main+0x33a>
mov    0x2a96(%rip),%rax
mov    (%rax),%edi
lea    0xb50(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x40(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1573 <main+0x373>
mov    0x2a62(%rip),%rax
mov    (%rax),%edi
lea    0xb2f(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
jmp    159b <main+0x39b>
movslq -0x50(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1596 <main+0x396>
mov    0x2a38(%rip),%rax
mov    (%rax),%edi
lea    0xb14(%rip),%rsi
call   1070 <errx@plt>
jmp    159b <main+0x39b>
mov    -0x40(%rbp),%edi
call   1090 <close@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    15c7 <main+0x3c7>
mov    0x2a21(%rip),%rax
mov    (%rax),%edi
lea    0xaf9(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%ebx
cmp    $0x1,%edi
jg     122c <main+0x2c>
xor    %r12d,%r12d
xor    %eax,%eax
call   10f0 <usage@plt>
mov    0x8(%r15),%rax
mov    %rax,(%rsp)
jmp    1271 <main+0x71>
mov    0x8(%r15),%rax
mov    %rax,(%rsp)
cmp    $0x2,%ebx
setne  %r12b
je     126e <main+0x6e>
mov    %rbx,%r14
mov    %ebx,%ebx
mov    $0x2,%ebp
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
mov    (%r15,%rbp,8),%rdi
call   1060 <strlen@plt>
add    %rax,%r13
add    $0x1,%r13
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1250 <main+0x50>
mov    %r14,%rbx
jmp    1274 <main+0x74>
xor    %r12d,%r12d
xor    %r13d,%r13d
add    $0x1,%r13
mov    %r13,%rdi
call   10e0 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    129f <main+0x9f>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
xor    %eax,%eax
call   10d0 <err@plt>
movb   $0x0,(%r14)
test   %r12b,%r12b
je     12f8 <main+0xf8>
lea    -0x1(%rbx),%eax
movslq %eax,%r12
mov    %ebx,%ebx
mov    $0x2,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15,%rbp,8),%rsi
mov    %r14,%rdi
mov    %r13,%rdx
call   1050 <strlcat@plt>
cmp    %r12,%rbp
lea    0xd39(%rip),%rsi
lea    0xd30(%rip),%rax
cmovl  %rax,%rsi
mov    %r14,%rdi
mov    %r13,%rdx
call   1050 <strlcat@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    12c0 <main+0xc0>
mov    0x2ce1(%rip),%rax
mov    0x2cf2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2c9f(%rip),%rax
or     (%rax),%esi
mov    0x2c8e(%rip),%rax
mov    (%rax),%edx
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1100 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1348 <main+0x148>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
lea    0xcd6(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10d0 <err@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1120 <fstat@plt>
test   %eax,%eax
jns    1372 <main+0x172>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
lea    0xcb5(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10d0 <err@plt>
mov    0x10(%rsp),%edi
call   10c0 <S_ISREG@plt>
test   %eax,%eax
je     138b <main+0x18b>
cmp    %r13,0x8(%rsp)
je     13b0 <main+0x1b0>
jmp    1438 <main+0x238>
mov    0x2c3e(%rip),%rax
mov    (%rax),%edi
lea    0xc8c(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10d0 <err@plt>
cmp    %r13,0x8(%rsp)
jne    1438 <main+0x238>
mov    %r13,%rdi
call   10e0 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    13da <main+0x1da>
mov    0x2bf9(%rip),%rax
mov    (%rax),%edi
lea    0xc70(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   10d0 <err@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r13,%rdx
call   10a0 <read@plt>
cmp    $0xffffffff,%eax
je     140a <main+0x20a>
cltq
cmp    %rax,%r13
je     1421 <main+0x221>
mov    0x2bc6(%rip),%rax
mov    (%rax),%edi
lea    0xc5e(%rip),%rsi
call   1070 <errx@plt>
jmp    1421 <main+0x221>
mov    0x2baf(%rip),%rax
mov    (%rax),%edi
lea    0xc39(%rip),%rsi
xor    %eax,%eax
call   10d0 <err@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   10b0 <memcmp@plt>
test   %rax,%rax
je     14d6 <main+0x2d6>
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <ftruncate@plt>
test   %eax,%eax
jns    145c <main+0x25c>
mov    0x2b74(%rip),%rax
mov    (%rax),%edi
lea    0xc2e(%rip),%rsi
xor    %eax,%eax
call   10d0 <err@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r13,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     148c <main+0x28c>
cltq
cmp    %rax,%r13
je     14a3 <main+0x2a3>
mov    0x2b44(%rip),%rax
mov    (%rax),%edi
lea    0xc20(%rip),%rsi
call   1070 <errx@plt>
jmp    14a3 <main+0x2a3>
mov    0x2b2d(%rip),%rax
mov    (%rax),%edi
lea    0xbfa(%rip),%rsi
xor    %eax,%eax
call   10d0 <err@plt>
mov    %ebp,%edi
call   1090 <close@plt>
test   %eax,%eax
jns    14c5 <main+0x2c5>
mov    0x2b23(%rip),%rax
mov    (%rax),%edi
lea    0xbfb(%rip),%rsi
xor    %eax,%eax
call   10d0 <err@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1090 <close@plt>
test   %eax,%eax
jns    1500 <main+0x300>
mov    0x2ad0(%rip),%rax
mov    (%rax),%edi
lea    0xb7b(%rip),%rsi
xor    %eax,%eax
call   10d0 <err@plt>
xor    %edi,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@EX_UNAVAILABLE = global i32 0
@.str = [16 x i8] c"malloc() failed\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@DEFFILEMODE = global i32 0
@EX_NOINPUT = global i32 0
@.str.3 = [9 x i8] c"open(%s)\00"
@.str.4 = [10 x i8] c"fstat(%s)\00"
@EX_USAGE = global i32 0
@.str.5 = [25 x i8] c"%s is not a regular file\00"
@.str.6 = [19 x i8] c"malloc(%lu) failed\00"
@.str.7 = [14 x i8] c"read() failed\00"
@.str.8 = [19 x i8] c"short read of file\00"
@.str.9 = [15 x i8] c"close() failed\00"
@.str.10 = [19 x i8] c"ftruncate() failed\00"
@.str.11 = [15 x i8] c"write() failed\00"
@.str.12 = [20 x i8] c"short write of file\00"
@.str.13 = [11 x i8] c"close(dst)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i64 0, i64* %8
store i8* null, i8** %9
store i8* null, i8** %12
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (...) @usage()
br label %20
20: ; preds = %18, %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %12
store i32 2, i32* %15
br label %24
24: ; preds = %38, %20
%25 = load i32, i32* %15
%26 = load i32, i32* %4
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %41
28: ; preds = %24
%29 = load i8**, i8*** %5
%30 = load i32, i32* %15
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %29, i64 %31
%33 = load i8*, i8** %32
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %34, 1
%36 = load i64, i64* %8
%37 = add i64 %36, %35
store i64 %37, i64* %8
br label %38
38: ; preds = %28
%39 = load i32, i32* %15
%40 = add nsw i32 %39, 1
store i32 %40, i32* %15
br label %24
41: ; preds = %24
%42 = load i64, i64* %8
%43 = add i64 %42, 1
store i64 %43, i64* %8
%44 = load i64, i64* %8
%45 = call i8* @malloc(i64 %44)
store i8* %45, i8** %7
%46 = load i8*, i8** %7
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %41
%49 = load i32, i32* @EX_UNAVAILABLE
%50 = call i32 (i32, i8*, ...) @err(i32 %49, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %51
51: ; preds = %48, %41
%52 = load i8*, i8** %7
%53 = getelementptr inbounds i8, i8* %52, i64 0
store i8 0, i8* %53
store i32 2, i32* %15
br label %54
54: ; preds = %80, %51
%55 = load i32, i32* %15
%56 = load i32, i32* %4
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %83
58: ; preds = %54
%59 = load i8*, i8** %7
%60 = load i8**, i8*** %5
%61 = load i32, i32* %15
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8*, i8** %60, i64 %62
%64 = load i8*, i8** %63
%65 = load i64, i64* %8
%66 = call i32 @strlcat(i8* %59, i8* %64, i64 %65)
%67 = load i32, i32* %15
%68 = load i32, i32* %4
%69 = sub nsw i32 %68, 1
%70 = icmp slt i32 %67, %69
br i1 %70, label %71, label %75
71: ; preds = %58
%72 = load i8*, i8** %7
%73 = load i64, i64* %8
%74 = call i32 @strlcat(i8* %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i64 %73)
br label %79
75: ; preds = %58
%76 = load i8*, i8** %7
%77 = load i64, i64* %8
%78 = call i32 @strlcat(i8* %76, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 %77)
br label %79
79: ; preds = %75, %71
br label %80
80: ; preds = %79
%81 = load i32, i32* %15
%82 = add nsw i32 %81, 1
store i32 %82, i32* %15
br label %54
83: ; preds = %54
%84 = load i8*, i8** %12
%85 = load i32, i32* @O_RDWR
%86 = load i32, i32* @O_CREAT
%87 = or i32 %85, %86
%88 = load i32, i32* @O_APPEND
%89 = or i32 %87, %88
%90 = load i32, i32* @DEFFILEMODE
%91 = call i32 @open(i8* %84, i32 %89, i32 %90)
store i32 %91, i32* %11
%92 = load i32, i32* %11
%93 = icmp slt i32 %92, 0
br i1 %93, label %94, label %98
94: ; preds = %83
%95 = load i32, i32* @EX_NOINPUT
%96 = load i8*, i8** %12
%97 = call i32 (i32, i8*, ...) @err(i32 %95, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %96)
br label %98
98: ; preds = %94, %83
%99 = load i32, i32* %11
%100 = call i32 @fstat(i32 %99, %struct.stat* %6)
store i32 %100, i32* %10
%101 = load i32, i32* %10
%102 = icmp slt i32 %101, 0
br i1 %102, label %103, label %107
103: ; preds = %98
%104 = load i32, i32* @EX_NOINPUT
%105 = load i8*, i8** %12
%106 = call i32 (i32, i8*, ...) @err(i32 %104, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %105)
br label %107
107: ; preds = %103, %98
%108 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%109 = load i32, i32* %108
%110 = call i32 @S_ISREG(i32 %109)
%111 = icmp ne i32 %110, 0
br i1 %111, label %116, label %112
112: ; preds = %107
%113 = load i32, i32* @EX_USAGE
%114 = load i8*, i8** %12
%115 = call i32 (i32, i8*, ...) @err(i32 %113, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %114)
br label %116
116: ; preds = %112, %107
%117 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%118 = load i64, i64* %117
%119 = load i64, i64* %8
%120 = icmp ne i64 %118, %119
br i1 %120, label %121, label %122
121: ; preds = %116
br label %169
122: ; preds = %116
%123 = load i64, i64* %8
%124 = call i8* @malloc(i64 %123)
store i8* %124, i8** %9
%125 = load i8*, i8** %9
%126 = icmp eq i8* %125, null
br i1 %126, label %127, label %131
127: ; preds = %122
%128 = load i32, i32* @EX_UNAVAILABLE
%129 = load i64, i64* %8
%130 = call i32 (i32, i8*, ...) @err(i32 %128, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i64 %129)
br label %131
131: ; preds = %127, %122
%132 = load i32, i32* %11
%133 = load i8*, i8** %9
%134 = load i64, i64* %8
%135 = call i32 @read(i32 %132, i8* %133, i64 %134)
store i32 %135, i32* %13
%136 = load i32, i32* %13
%137 = icmp eq i32 %136, -1
br i1 %137, label %138, label %141
138: ; preds = %131
%139 = load i32, i32* @EX_UNAVAILABLE
%140 = call i32 (i32, i8*, ...) @err(i32 %139, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %150
141: ; preds = %131
%142 = load i32, i32* %13
%143 = sext i32 %142 to i64
%144 = load i64, i64* %8
%145 = icmp ne i64 %143, %144
br i1 %145, label %146, label %149
146: ; preds = %141
%147 = load i32, i32* @EX_UNAVAILABLE
%148 = call i32 @errx(i32 %147, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %149
149: ; preds = %146, %141
br label %150
150: ; preds = %149, %138
%151 = load i8*, i8** %9
%152 = load i8*, i8** %7
%153 = load i64, i64* %8
%154 = call i64 @memcmp(i8* %151, i8* %152, i64 %153)
%155 = icmp eq i64 0, %154
br i1 %155, label %156, label %168
156: ; preds = %150
%157 = load i8*, i8** %9
%158 = call i32 @free(i8* %157)
%159 = load i32, i32* %11
%160 = call i32 @close(i32 %159)
store i32 %160, i32* %10
%161 = load i32, i32* %10
%162 = icmp slt i32 %161, 0
br i1 %162, label %163, label %166
163: ; preds = %156
%164 = load i32, i32* @EX_UNAVAILABLE
%165 = call i32 (i32, i8*, ...) @err(i32 %164, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %166
166: ; preds = %163, %156
%167 = call i32 @exit(i32 0)
unreachable
168: ; preds = %150
br label %169
169: ; preds = %168, %121
%170 = load i32, i32* %11
%171 = call i32 @ftruncate(i32 %170, i32 0)
store i32 %171, i32* %10
%172 = load i32, i32* %10
%173 = icmp slt i32 %172, 0
br i1 %173, label %174, label %177
174: ; preds = %169
%175 = load i32, i32* @EX_UNAVAILABLE
%176 = call i32 (i32, i8*, ...) @err(i32 %175, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0))
br label %177
177: ; preds = %174, %169
%178 = load i32, i32* %11
%179 = load i8*, i8** %7
%180 = load i64, i64* %8
%181 = call i32 @write(i32 %178, i8* %179, i64 %180)
store i32 %181, i32* %14
%182 = load i32, i32* %14
%183 = icmp eq i32 %182, -1
br i1 %183, label %184, label %187
184: ; preds = %177
%185 = load i32, i32* @EX_UNAVAILABLE
%186 = call i32 (i32, i8*, ...) @err(i32 %185, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
br label %196
187: ; preds = %177
%188 = load i32, i32* %14
%189 = sext i32 %188 to i64
%190 = load i64, i64* %8
%191 = icmp ne i64 %189, %190
br i1 %191, label %192, label %195
192: ; preds = %187
%193 = load i32, i32* @EX_UNAVAILABLE
%194 = call i32 @errx(i32 %193, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
br label %195
195: ; preds = %192, %187
br label %196
196: ; preds = %195, %184
%197 = load i32, i32* %11
%198 = call i32 @close(i32 %197)
store i32 %198, i32* %10
%199 = load i32, i32* %10
%200 = icmp slt i32 %199, 0
br i1 %200, label %201, label %204
201: ; preds = %196
%202 = load i32, i32* @EX_NOINPUT
%203 = call i32 (i32, i8*, ...) @err(i32 %202, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.13, i64 0, i64 0))
br label %204
204: ; preds = %201, %196
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@EX_UNAVAILABLE = global i32 0
@.str = [16 x i8] c"malloc() failed\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@DEFFILEMODE = global i32 0
@EX_NOINPUT = global i32 0
@.str.3 = [9 x i8] c"open(%s)\00"
@.str.4 = [10 x i8] c"fstat(%s)\00"
@EX_USAGE = global i32 0
@.str.5 = [25 x i8] c"%s is not a regular file\00"
@.str.6 = [19 x i8] c"malloc(%lu) failed\00"
@.str.7 = [14 x i8] c"read() failed\00"
@.str.8 = [19 x i8] c"short read of file\00"
@.str.9 = [15 x i8] c"close() failed\00"
@.str.10 = [19 x i8] c"ftruncate() failed\00"
@.str.11 = [15 x i8] c"write() failed\00"
@.str.12 = [20 x i8] c"short write of file\00"
@.str.13 = [11 x i8] c"close(dst)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 (...) @usage()
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
br label %26
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = icmp ne i32 %0, 2
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = zext i32 %0 to i64
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 2, %14 ], [ %24, %16 ]
%18 = phi i64 [ 0, %14 ], [ %23, %16 ]
%19 = getelementptr inbounds i8*, i8** %1, i64 %17
%20 = load i8*, i8** %19
%21 = tail call i64 @strlen(i8* dereferenceable(1) %20)
%22 = add i64 %18, 1
%23 = add i64 %22, %21
%24 = add nuw nsw i64 %17, 1
%25 = icmp eq i64 %24, %15
br i1 %25, label %26, label %16
26: ; preds = %16, %6, %10
%27 = phi i1 [ false, %10 ], [ false, %6 ], [ %13, %16 ]
%28 = phi i8* [ %12, %10 ], [ %9, %6 ], [ %12, %16 ]
%29 = phi i64 [ 0, %10 ], [ 0, %6 ], [ %23, %16 ]
%30 = add i64 %29, 1
%31 = tail call i8* @malloc(i64 %30)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32, i32* @EX_UNAVAILABLE
%35 = tail call i32 (i32, i8*, ...) @err(i32 %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %33, %26
store i8 0, i8* %31
br i1 %27, label %37, label %51
37: ; preds = %36
%38 = add nsw i32 %0, -1
%39 = sext i32 %38 to i64
%40 = zext i32 %0 to i64
br label %41
41: ; preds = %37, %41
%42 = phi i64 [ 2, %37 ], [ %49, %41 ]
%43 = getelementptr inbounds i8*, i8** %1, i64 %42
%44 = load i8*, i8** %43
%45 = tail call i32 @strlcat(i8* %31, i8* %44, i64 %30)
%46 = icmp slt i64 %42, %39
%47 = select i1 %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%48 = tail call i32 @strlcat(i8* %31, i8* %47, i64 %30)
%49 = add nuw nsw i64 %42, 1
%50 = icmp eq i64 %49, %40
br i1 %50, label %51, label %41
51: ; preds = %41, %36
%52 = load i32, i32* @O_RDWR
%53 = load i32, i32* @O_CREAT
%54 = or i32 %53, %52
%55 = load i32, i32* @O_APPEND
%56 = or i32 %54, %55
%57 = load i32, i32* @DEFFILEMODE
%58 = tail call i32 @open(i8* %28, i32 %56, i32 %57)
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %51
%61 = load i32, i32* @EX_NOINPUT
%62 = tail call i32 (i32, i8*, ...) @err(i32 %61, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %28)
br label %63
63: ; preds = %60, %51
%64 = call i32 @fstat(i32 %58, %struct.stat* %3)
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i32, i32* @EX_NOINPUT
%68 = tail call i32 (i32, i8*, ...) @err(i32 %67, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %28)
br label %69
69: ; preds = %66, %63
%70 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%71 = load i32, i32* %70
%72 = tail call i32 @S_ISREG(i32 %71)
%73 = icmp eq i32 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %69
%75 = load i32, i32* @EX_USAGE
%76 = tail call i32 (i32, i8*, ...) @err(i32 %75, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %28)
br label %77
77: ; preds = %74, %69
%78 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%79 = load i64, i64* %78
%80 = icmp eq i64 %79, %30
br i1 %80, label %81, label %111
81: ; preds = %77
%82 = tail call i8* @malloc(i64 %30)
%83 = icmp eq i8* %82, null
br i1 %83, label %84, label %87
84: ; preds = %81
%85 = load i32, i32* @EX_UNAVAILABLE
%86 = tail call i32 (i32, i8*, ...) @err(i32 %85, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i64 %30)
br label %87
87: ; preds = %84, %81
%88 = tail call i32 @read(i32 %58, i8* %82, i64 %30)
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %93
90: ; preds = %87
%91 = load i32, i32* @EX_UNAVAILABLE
%92 = tail call i32 (i32, i8*, ...) @err(i32 %91, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %99
93: ; preds = %87
%94 = sext i32 %88 to i64
%95 = icmp eq i64 %30, %94
br i1 %95, label %99, label %96
96: ; preds = %93
%97 = load i32, i32* @EX_UNAVAILABLE
%98 = tail call i32 @errx(i32 %97, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %99
99: ; preds = %93, %96, %90
%100 = tail call i64 @memcmp(i8* %82, i8* %31, i64 %30)
%101 = icmp eq i64 %100, 0
br i1 %101, label %102, label %111
102: ; preds = %99
%103 = tail call i32 @free(i8* %82)
%104 = tail call i32 @close(i32 %58)
%105 = icmp slt i32 %104, 0
br i1 %105, label %106, label %109
106: ; preds = %102
%107 = load i32, i32* @EX_UNAVAILABLE
%108 = tail call i32 (i32, i8*, ...) @err(i32 %107, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %109
109: ; preds = %106, %102
%110 = tail call i32 @exit(i32 0)
unreachable
111: ; preds = %99, %77
%112 = tail call i32 @ftruncate(i32 %58, i32 0)
%113 = icmp slt i32 %112, 0
br i1 %113, label %114, label %117
114: ; preds = %111
%115 = load i32, i32* @EX_UNAVAILABLE
%116 = tail call i32 (i32, i8*, ...) @err(i32 %115, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0))
br label %117
117: ; preds = %114, %111
%118 = tail call i32 @write(i32 %58, i8* %31, i64 %30)
%119 = icmp eq i32 %118, -1
br i1 %119, label %120, label %123
120: ; preds = %117
%121 = load i32, i32* @EX_UNAVAILABLE
%122 = tail call i32 (i32, i8*, ...) @err(i32 %121, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
br label %129
123: ; preds = %117
%124 = sext i32 %118 to i64
%125 = icmp eq i64 %30, %124
br i1 %125, label %129, label %126
126: ; preds = %123
%127 = load i32, i32* @EX_UNAVAILABLE
%128 = tail call i32 @errx(i32 %127, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
br label %129
129: ; preds = %123, %126, %120
%130 = tail call i32 @close(i32 %58)
%131 = icmp slt i32 %130, 0
br i1 %131, label %132, label %135
132: ; preds = %129
%133 = load i32, i32* @EX_NOINPUT
%134 = tail call i32 (i32, i8*, ...) @err(i32 %133, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.13, i64 0, i64 0))
br label %135
135: ; preds = %132, %129
ret i32 0
}