#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_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
size_t findSyncByte(char *, size_t);
long lseek(int, long, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, long, ...);
size_t read(int, char *, size_t);
int /*<<< orphan*/ write(int, char *, size_t);
int main(int argc, char *argv[]) {
  size_t BUFFER_SIZE = 188;
  char buf[BUFFER_SIZE];
  size_t size;
  size_t offset = 0;
  int source = open(argv[1], O_RDONLY, 0);
  int dest = open(argv[2], O_WRONLY | O_CREAT, 0644);
  while ((size = read(source, buf, BUFFER_SIZE)) > 0) {
    long int currentpos = lseek(source, 0, SEEK_CUR);
    offset = findSyncByte(buf, size);
    if (offset == 0) {
      write(dest, buf, size);
    } else {
      printf("found sync byte at offset %d of filepos %ld\n", (int)offset,
             currentpos);
      lseek(source, currentpos - 188 + offset, SEEK_SET);
      printf("set fd to %ld\n", lseek(source, 0, SEEK_CUR));
    }
  }
  close(source);
  close(dest);
  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)
movq   $0xbc,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x40(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x3c(%rbp),%edi
mov    -0x18(%rbp),%rdx
call   1070 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jbe    12cb <main+0x15b>
mov    -0x3c(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rsi
call   1080 <findSyncByte@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1267 <main+0xf7>
mov    -0x50(%rbp),%rsi
mov    -0x40(%rbp),%edi
mov    -0x30(%rbp),%rdx
call   1030 <write@plt>
jmp    12c6 <main+0x156>
mov    -0x38(%rbp),%rax
movslq %eax,%rsi
mov    -0x48(%rbp),%rdx
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3c(%rbp),%edi
mov    -0x48(%rbp),%rsi
sub    $0xbc,%rsi
add    -0x38(%rbp),%rsi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0x3c(%rbp),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
mov    %rax,%rsi
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ff <main+0x8f>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%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
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    0x2e4c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,0xc(%rsp)
lea    0x10(%rsp),%rsi
mov    $0xbc,%edx
mov    %ebx,%edi
call   1070 <read@plt>
test   %rax,%rax
je     128a <main+0x11a>
mov    %rax,%rbp
mov    0x2dfe(%rip),%r14
lea    0x10(%rsp),%r13
jmp    1256 <main+0xe6>
nopl   0x0(%rax)
mov    %rax,%r12
movslq %r12d,%rsi
lea    0xe03(%rip),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    (%r15,%r12,1),%rsi
add    $0xffffffffffffff44,%rsi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1050 <lseek@plt>
mov    (%r14),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0xdf8(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xbc,%edx
mov    %ebx,%edi
mov    %r13,%rsi
call   1070 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
je     128a <main+0x11a>
mov    (%r14),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,%r15
mov    %r13,%rdi
mov    %rbp,%rsi
call   1080 <findSyncByte@plt>
test   %rax,%rax
jne    11f0 <main+0x80>
mov    0xc(%rsp),%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
jmp    123f <main+0xcf>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@SEEK_CUR = global i32 0
@.str = [45 x i8] c"found sync byte at offset %d of filepos %ld\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [15 x i8] c"set fd to %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 188, i64* %6
%14 = load i64, i64* %6
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %7
%16 = alloca i8, i64 %14
store i64 %14, i64* %8
store i64 0, i64* %10
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20, i32 0)
store i32 %21, i32* %11
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %24, i32 %27, i32 420)
store i32 %28, i32* %12
br label %29
29: ; preds = %63, %2
%30 = load i32, i32* %11
%31 = load i64, i64* %6
%32 = call i64 @read(i32 %30, i8* %16, i64 %31)
store i64 %32, i64* %9
%33 = icmp ugt i64 %32, 0
br i1 %33, label %34, label %64
34: ; preds = %29
%35 = load i32, i32* %11
%36 = load i32, i32* @SEEK_CUR
%37 = call i64 @lseek(i32 %35, i64 0, i32 %36)
store i64 %37, i64* %13
%38 = load i64, i64* %9
%39 = call i64 @findSyncByte(i8* %16, i64 %38)
store i64 %39, i64* %10
%40 = load i64, i64* %10
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %34
%43 = load i32, i32* %12
%44 = load i64, i64* %9
%45 = call i32 @write(i32 %43, i8* %16, i64 %44)
br label %63
46: ; preds = %34
%47 = load i64, i64* %10
%48 = trunc i64 %47 to i32
%49 = sext i32 %48 to i64
%50 = load i64, i64* %13
%51 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %49, i64 %50)
%52 = load i32, i32* %11
%53 = load i64, i64* %13
%54 = sub nsw i64 %53, 188
%55 = load i64, i64* %10
%56 = add i64 %54, %55
%57 = load i32, i32* @SEEK_SET
%58 = call i64 @lseek(i32 %52, i64 %56, i32 %57)
%59 = load i32, i32* %11
%60 = load i32, i32* @SEEK_CUR
%61 = call i64 @lseek(i32 %59, i64 0, i32 %60)
%62 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %61)
br label %63
63: ; preds = %46, %42
br label %29
64: ; preds = %29
%65 = load i32, i32* %11
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %12
%68 = call i32 @close(i32 %67)
store i32 0, i32* %3
%69 = load i8*, i8** %7
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@SEEK_CUR = global i32 0
@.str = [45 x i8] c"found sync byte at offset %d of filepos %ld\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [15 x i8] c"set fd to %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [188 x i8]
%4 = getelementptr inbounds [188 x i8], [188 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7, i32 0)
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %10, i32 %13, i32 420)
%15 = call i64 @read(i32 %8, i8* %4, i64 188)
%16 = icmp eq i64 %15, 0
br i1 %16, label %39, label %17
17: ; preds = %2, %36
%18 = phi i64 [ %37, %36 ], [ %15, %2 ]
%19 = load i32, i32* @SEEK_CUR
%20 = call i64 @lseek(i32 %8, i64 0, i32 %19)
%21 = call i64 @findSyncByte(i8* %4, i64 %18)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @write(i32 %14, i8* %4, i64 %18)
br label %36
25: ; preds = %17
%26 = shl i64 %21, 32
%27 = ashr exact i64 %26, 32
%28 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %27, i64 %20)
%29 = add nsw i64 %20, -188
%30 = add i64 %29, %21
%31 = load i32, i32* @SEEK_SET
%32 = call i64 @lseek(i32 %8, i64 %30, i32 %31)
%33 = load i32, i32* @SEEK_CUR
%34 = call i64 @lseek(i32 %8, i64 0, i32 %33)
%35 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %34)
br label %36
36: ; preds = %25, %23
%37 = call i64 @read(i32 %8, i8* %4, i64 188)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %17
39: ; preds = %36, %2
%40 = call i32 @close(i32 %8)
%41 = call i32 @close(i32 %14)
ret i32 0
}