#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_NONBLOCK;
int O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dump_file(int, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int in_fd, out_fd;
  char *in_fname, *out_fname;
  if (argc != 3) {
    printf("Usage: sr_collect <input file> <output file>\n");
    return 0;
  }
  in_fname = argv[1];
  in_fd = open(in_fname, O_RDONLY | O_NONBLOCK);
  if (!in_fd) {
    printf("Error opening input file: %s\n", in_fname);
    return 0;
  }
  out_fname = argv[2];
  out_fd = open(out_fname, O_CREAT | O_RDWR | O_TRUNC);
  if (!out_fd) {
    printf("Error opening output file: %s\n", out_fname);
    close(in_fd);
    return 0;
  }
  dump_file(in_fd, out_fd);
  close(in_fd);
  close(out_fd);
  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   $0x3,-0x8(%rbp)
je     117a <main+0x3a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1251 <main+0x111>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11cc <main+0x8c>
mov    -0x20(%rbp),%rsi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1251 <main+0x111>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%rax
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    122f <main+0xef>
mov    -0x28(%rbp),%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1251 <main+0x111>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <dump_file@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
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
cmp    $0x3,%edi
jne    11c4 <main+0x74>
mov    %rsi,%rbx
mov    0x8(%rsi),%rbp
mov    0x2e61(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
je     11d2 <main+0x82>
mov    %eax,%r14d
mov    0x2e70(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rbx
mov    0x2e52(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
je     11e5 <main+0x95>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %eax,%esi
call   1050 <dump_file@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
jmp    11f9 <main+0xa9>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
jmp    11fe <main+0xae>
lea    0xe27(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11fe <main+0xae>
lea    0xe32(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Usage: sr_collect <input file> <output file>\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [30 x i8] c"Error opening input file: %s\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [31 x i8] c"Error opening output file: %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
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %54
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %8
%18 = load i8*, i8** %8
%19 = load i32, i32* @O_RDONLY
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %18, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %14
%26 = load i8*, i8** %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 0, i32* %3
br label %54
28: ; preds = %14
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = load i32, i32* @O_CREAT
%34 = load i32, i32* @O_RDWR
%35 = or i32 %33, %34
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %32, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp ne i32 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %28
%42 = load i8*, i8** %9
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
store i32 0, i32* %3
br label %54
46: ; preds = %28
%47 = load i32, i32* %6
%48 = load i32, i32* %7
%49 = call i32 @dump_file(i32 %47, i32 %48)
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
store i32 0, i32* %3
br label %54
54: ; preds = %46, %41, %25, %12
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [30 x i8] c"Error opening input file: %s\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [31 x i8] c"Error opening output file: %s\0A\00"
@str = [45 x i8] c"Usage: sr_collect <input file> <output file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
br label %33
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %8, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %33
16: ; preds = %6
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %20, %19
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = tail call i32 @open(i8* %18, i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %16
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%28 = tail call i32 @close(i32 %12)
br label %33
29: ; preds = %16
%30 = tail call i32 @dump_file(i32 %12, i32 %24)
%31 = tail call i32 @close(i32 %12)
%32 = tail call i32 @close(i32 %24)
br label %33
33: ; preds = %29, %26, %14, %4
ret i32 0
}