#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 option {
  char *member_0;
  char member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ const member_1;
};
struct hv_start_fcopy {
  int dummy;
};
struct hv_fcopy_hdr {
  int operation;
};
struct hv_do_fcopy {
  int dummy;
};
typedef int /*<<< orphan*/ kernel_modver;
typedef int __u32;

/* Variables and functions */
#define CANCEL_FCOPY 132
#define COMPLETE_FCOPY 131
int /*<<< orphan*/ EXIT_FAILURE;
int FCOPY_CURRENT_VERSION;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ LOG_USER;
int /*<<< orphan*/ O_RDWR;
#define START_FILE_COPY 130
#define WRITE_TO_FILE 129
scalar_t__ daemon(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getopt_long(int, char **, char *, struct option *, int *);
int getpid();
int hv_copy_cancel();
int hv_copy_data(struct hv_do_fcopy *);
int hv_copy_finished();
int hv_start_fcopy(struct hv_start_fcopy *);
#define no_argument 128
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int pread(int, char **, int, int /*<<< orphan*/);
int /*<<< orphan*/ print_usage(char **);
int pwrite(int, int *, int, int /*<<< orphan*/);
int strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int write(int, int *, int);
int main(int argc, char *argv[]) {
  int fcopy_fd, len;
  int error;
  int daemonize = 1, long_index = 0, opt;
  int version = FCOPY_CURRENT_VERSION;
  char *buffer[4096 * 2];
  struct hv_fcopy_hdr *in_msg;
  int in_handshake = 1;
  __u32 kernel_modver;
  static struct option long_options[] = {{"help", no_argument, 0, 'h'},
                                         {"no-daemon", no_argument, 0, 'n'},
                                         {0, 0, 0, 0}};
  while ((opt = getopt_long(argc, argv, "hn", long_options, &long_index)) !=
         -1) {
    switch (opt) {
    case 'n':
      daemonize = 0;
      break;
    case 'h':
    default:
      print_usage(argv);
      exit(EXIT_FAILURE);
    }
  }
  if (daemonize && daemon(1, 0)) {
    syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
    exit(EXIT_FAILURE);
  }
  openlog("HV_FCOPY", 0, LOG_USER);
  syslog(LOG_INFO, "starting; pid is:%d", getpid());
  fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
  if (fcopy_fd < 0) {
    syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s", errno,
           strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
    syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
    exit(EXIT_FAILURE);
  }
  while (1) {
    len = pread(fcopy_fd, buffer, (4096 * 2), 0);
    if (len < 0) {
      syslog(LOG_ERR, "pread failed: %s", strerror(errno));
      exit(EXIT_FAILURE);
    }
    if (in_handshake) {
      if (len != sizeof(kernel_modver)) {
        syslog(LOG_ERR, "invalid version negotiation");
        exit(EXIT_FAILURE);
      }
      kernel_modver = *(__u32 *)buffer;
      in_handshake = 0;
      syslog(LOG_INFO, "kernel module version: %d", kernel_modver);
      continue;
    }
    in_msg = (struct hv_fcopy_hdr *)buffer;
    switch (in_msg->operation) {
    case START_FILE_COPY:
      error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
      break;
    case WRITE_TO_FILE:
      error = hv_copy_data((struct hv_do_fcopy *)in_msg);
      break;
    case COMPLETE_FCOPY:
      error = hv_copy_finished();
      break;
    case CANCEL_FCOPY:
      error = hv_copy_cancel();
      break;
    default:
      syslog(LOG_ERR, "Unknown operation: %d", in_msg->operation);
    }
    if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
      syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10070,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x1,-0x1003c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xdd4(%rip),%rdx
lea    0x2e5e(%rip),%rcx
lea    -0x24(%rbp),%r8
call   1060 <getopt_long@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
je     12bf <main+0xbf>
mov    -0x28(%rbp),%eax
mov    %eax,-0x10044(%rbp)
sub    $0x68,%eax
je     129e <main+0x9e>
jmp    127e <main+0x7e>
mov    -0x10044(%rbp),%eax
sub    $0x6e,%eax
jne    12a3 <main+0xa3>
jmp    1292 <main+0x92>
movl   $0x0,-0x20(%rbp)
jmp    12ba <main+0xba>
jmp    12a3 <main+0xa3>
mov    -0x10(%rbp),%rdi
call   1110 <print_usage@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    123d <main+0x3d>
cmpl   $0x0,-0x20(%rbp)
je     1320 <main+0x120>
mov    $0x1,%edi
xor    %esi,%esi
call   10a0 <daemon@plt>
cmp    $0x0,%rax
je     1320 <main+0x120>
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10048(%rbp)
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    -0x10048(%rbp),%edi
mov    %eax,%edx
lea    0xd17(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edx
lea    0xd0d(%rip),%rdi
xor    %esi,%esi
call   10e0 <openlog@plt>
mov    0x2c82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1004c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x1004c(%rbp),%edi
mov    %eax,%edx
lea    0xcea(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    0x2c7e(%rip),%rax
mov    (%rax),%esi
lea    0xce7(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    13db <main+0x1db>
mov    0x2c24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10054(%rbp)
mov    0x2c35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10050(%rbp)
mov    0x2c26(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    -0x10054(%rbp),%edi
mov    -0x10050(%rbp),%edx
mov    %eax,%ecx
lea    0xca8(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    0x2bf4(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
je     1439 <main+0x239>
mov    0x2bb1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10058(%rbp)
mov    0x2bc2(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    -0x10058(%rbp),%edi
mov    %eax,%edx
lea    0xc78(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    0x2b96(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    143e <main+0x23e>
mov    -0x14(%rbp),%edi
lea    -0x10030(%rbp),%rsi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   10d0 <pread@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    14a2 <main+0x2a2>
mov    0x2b48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1005c(%rbp)
mov    0x2b59(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    -0x1005c(%rbp),%edi
mov    %eax,%edx
lea    0xc27(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    0x2b2d(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
cmpl   $0x0,-0x1003c(%rbp)
je     151a <main+0x31a>
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
je     14e2 <main+0x2e2>
mov    0x2aec(%rip),%rax
mov    (%rax),%edi
lea    0xbf8(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    0x2aed(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x10030(%rbp),%eax
mov    %eax,-0x10040(%rbp)
movl   $0x0,-0x1003c(%rbp)
mov    0x2ac1(%rip),%rax
mov    (%rax),%edi
mov    -0x10040(%rbp),%edx
lea    0xbd3(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
jmp    143e <main+0x23e>
lea    -0x10030(%rbp),%rax
mov    %rax,-0x10038(%rbp)
mov    -0x10038(%rbp),%rax
mov    (%rax),%eax
add    $0xffffff7f,%eax
mov    %eax,%ecx
mov    %rcx,-0x10068(%rbp)
sub    $0x3,%eax
ja     15a5 <main+0x3a5>
mov    -0x10068(%rbp),%rax
lea    0xaaa(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10038(%rbp),%rdi
call   1070 <hv_start_fcopy@plt>
mov    %eax,-0x1c(%rbp)
jmp    15c5 <main+0x3c5>
mov    -0x10038(%rbp),%rdi
call   1100 <hv_copy_data@plt>
mov    %eax,-0x1c(%rbp)
jmp    15c5 <main+0x3c5>
mov    $0x0,%al
call   1090 <hv_copy_finished@plt>
mov    %eax,-0x1c(%rbp)
jmp    15c5 <main+0x3c5>
mov    $0x0,%al
call   1050 <hv_copy_cancel@plt>
mov    %eax,-0x1c(%rbp)
jmp    15c5 <main+0x3c5>
mov    0x2a04(%rip),%rax
mov    (%rax),%edi
mov    -0x10038(%rbp),%rax
mov    (%rax),%edx
lea    0xb3d(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   10b0 <pwrite@plt>
cltq
cmp    $0x4,%rax
je     1625 <main+0x425>
mov    0x29c5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1006c(%rbp)
mov    0x29d6(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    -0x1006c(%rbp),%edi
mov    %eax,%edx
lea    0xb01(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    0x29aa(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    143e <main+0x23e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %rsi,%r13
mov    %edi,%ebp
movl   $0x0,0xc(%rsp)
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
xor    %ecx,%ecx
lea    0xdeb(%rip),%r14
lea    0x2e75(%rip),%r15
lea    0xc(%rsp),%r12
mov    %ecx,%ebx
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %r12,%r8
call   1060 <getopt_long@plt>
mov    $0x1,%cl
cmp    $0x6e,%eax
je     1240 <main+0x40>
cmp    $0xffffffff,%eax
jne    1304 <main+0x104>
test   $0x1,%bl
jne    127f <main+0x7f>
mov    $0x1,%edi
xor    %esi,%esi
call   10a0 <daemon@plt>
test   %rax,%rax
jne    1360 <main+0x160>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
lea    0xdae(%rip),%rdi
xor    %esi,%esi
call   10e0 <openlog@plt>
mov    0x2d23(%rip),%rbp
mov    0x0(%rbp),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd98(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
lea    0xd91(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
jns    1311 <main+0x111>
mov    0x2cd7(%rip),%rax
mov    (%rax),%ebx
mov    0x2cee(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1120 <strerror@plt>
lea    0xd7c(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <syslog@plt>
jmp    13a9 <main+0x1a9>
mov    %r13,%rdi
call   1110 <print_usage@plt>
jmp    13a9 <main+0x1a9>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    1380 <main+0x180>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   10d0 <pread@plt>
test   %eax,%eax
jns    13b7 <main+0x1b7>
mov    0x2c69(%rip),%rax
mov    (%rax),%ebx
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xd56(%rip),%rsi
jmp    139e <main+0x19e>
mov    0x2c49(%rip),%rax
mov    (%rax),%ebx
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xca4(%rip),%rsi
jmp    139e <main+0x19e>
mov    0x2c29(%rip),%rax
mov    (%rax),%ebx
mov    0x2c40(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xcfe(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    0x2c18(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
cmp    $0x4,%eax
jne    14af <main+0x2af>
mov    0x10(%rsp),%edx
mov    0x0(%rbp),%edi
lea    0xd13(%rip),%rsi
xor    %eax,%eax
call   1080 <syslog@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   10d0 <pread@plt>
test   %eax,%eax
js     1340 <main+0x140>
mov    0x2bb9(%rip),%r13
lea    0xcfd(%rip),%r14
lea    0x4(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xbf1(%rip),%rbp
nop
mov    0x10(%rsp),%edx
lea    -0x81(%rdx),%eax
cmp    $0x3,%eax
ja     1440 <main+0x240>
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
mov    %r12,%rdi
call   1100 <hv_copy_data@plt>
jmp    1477 <main+0x277>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x0(%r13),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <syslog@plt>
jmp    147b <main+0x27b>
mov    %r12,%rdi
call   1070 <hv_start_fcopy@plt>
jmp    1477 <main+0x277>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <hv_copy_finished@plt>
jmp    1477 <main+0x277>
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <hv_copy_cancel@plt>
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   10b0 <pwrite@plt>
cmp    $0x4,%eax
jne    14cb <main+0x2cb>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   10d0 <pread@plt>
test   %eax,%eax
jns    1410 <main+0x210>
jmp    1340 <main+0x140>
mov    0x2afa(%rip),%rax
mov    (%rax),%edi
lea    0xc06(%rip),%rsi
xor    %eax,%eax
call   1080 <syslog@plt>
jmp    13a9 <main+0x1a9>
mov    0x0(%r13),%ebx
mov    0x2afa(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc2d(%rip),%rsi
jmp    139e <main+0x19e>

LLVM IR (-O0):
%struct.option = type { i8*, i8, i32, i32 }
%struct.hv_fcopy_hdr = type { i32 }
%struct.hv_start_fcopy = type { i32 }
%struct.hv_do_fcopy = type { i32 }
@FCOPY_CURRENT_VERSION = global i32 0
@main.long_options = internal global [3 x %struct.option] [%struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8 -128, i32 0, i32 104 }, %struct.option { i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i32 0, i32 0), i8 -128, i32 0, i32 110 }, %struct.option zeroinitializer]
@.str = [5 x i8] c"help\00"
@.str.1 = [10 x i8] c"no-daemon\00"
@.str.2 = [3 x i8] c"hn\00"
@EXIT_FAILURE = global i32 0
@LOG_ERR = global i32 0
@.str.3 = [27 x i8] c"daemon() failed; error: %s\00"
@errno = global i32 0
@.str.4 = [9 x i8] c"HV_FCOPY\00"
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.5 = [20 x i8] c"starting; pid is:%d\00"
@.str.6 = [20 x i8] c"/dev/vmbus/hv_fcopy\00"
@O_RDWR = global i32 0
@.str.7 = [46 x i8] c"open /dev/vmbus/hv_fcopy failed; error: %d %s\00"
@.str.8 = [24 x i8] c"Registration failed: %s\00"
@.str.9 = [17 x i8] c"pread failed: %s\00"
@.str.10 = [28 x i8] c"invalid version negotiation\00"
@.str.11 = [26 x i8] c"kernel module version: %d\00"
@.str.12 = [22 x i8] c"Unknown operation: %d\00"
@.str.13 = [18 x i8] c"pwrite failed: %s\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 i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [8192 x i8*]
%14 = alloca %struct.hv_fcopy_hdr*
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %9
store i32 0, i32* %10
%17 = load i32, i32* @FCOPY_CURRENT_VERSION
store i32 %17, i32* %12
store i32 1, i32* %15
br label %18
18: ; preds = %32, %2
%19 = load i32, i32* %4
%20 = load i8**, i8*** %5
%21 = call i32 @getopt_long(i32 %19, i8** %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %struct.option* getelementptr inbounds ([3 x %struct.option], [3 x %struct.option]* @main.long_options, i64 0, i64 0), i32* %10)
store i32 %21, i32* %11
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %33
23: ; preds = %18
%24 = load i32, i32* %11
switch i32 %24, label %27 [
i32 110, label %25
i32 104, label %26
]
25: ; preds = %23
store i32 0, i32* %9
br label %32
26: ; preds = %23
br label %27
27: ; preds = %23, %26
%28 = load i8**, i8*** %5
%29 = call i32 @print_usage(i8** %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %25
br label %18
33: ; preds = %18
%34 = load i32, i32* %9
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = call i64 @daemon(i32 1, i32 0)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i32, i32* @LOG_ERR
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @syslog(i32 %40, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %36, %33
%47 = load i32, i32* @LOG_USER
%48 = call i32 @openlog(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %47)
%49 = load i32, i32* @LOG_INFO
%50 = call i32 (...) @getpid()
%51 = call i32 (i32, i8*, ...) @syslog(i32 %49, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = load i32, i32* @O_RDWR
%53 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %52)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %46
%57 = load i32, i32* @LOG_ERR
%58 = load i32, i32* @errno
%59 = load i32, i32* @errno
%60 = call i32 @strerror(i32 %59)
%61 = call i32 (i32, i8*, ...) @syslog(i32 %57, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.7, i64 0, i64 0), i32 %58, i32 %60)
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %46
%65 = load i32, i32* %6
%66 = call i32 @write(i32 %65, i32* %12, i32 4)
%67 = sext i32 %66 to i64
%68 = icmp ne i64 %67, 4
br i1 %68, label %69, label %76
69: ; preds = %64
%70 = load i32, i32* @LOG_ERR
%71 = load i32, i32* @errno
%72 = call i32 @strerror(i32 %71)
%73 = call i32 (i32, i8*, ...) @syslog(i32 %70, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i32 %72)
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %64
br label %77
77: ; preds = %76, %102, %145
%78 = load i32, i32* %6
%79 = getelementptr inbounds [8192 x i8*], [8192 x i8*]* %13, i64 0, i64 0
%80 = call i32 @pread(i32 %78, i8** %79, i32 8192, i32 0)
store i32 %80, i32* %7
%81 = load i32, i32* %7
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %90
83: ; preds = %77
%84 = load i32, i32* @LOG_ERR
%85 = load i32, i32* @errno
%86 = call i32 @strerror(i32 %85)
%87 = call i32 (i32, i8*, ...) @syslog(i32 %84, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i32 %86)
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %77
%91 = load i32, i32* %15
%92 = icmp ne i32 %91, 0
br i1 %92, label %93, label %109
93: ; preds = %90
%94 = load i32, i32* %7
%95 = sext i32 %94 to i64
%96 = icmp ne i64 %95, 4
br i1 %96, label %97, label %102
97: ; preds = %93
%98 = load i32, i32* @LOG_ERR
%99 = call i32 (i32, i8*, ...) @syslog(i32 %98, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
%100 = load i32, i32* @EXIT_FAILURE
%101 = call i32 @exit(i32 %100)
unreachable
102: ; preds = %93
%103 = getelementptr inbounds [8192 x i8*], [8192 x i8*]* %13, i64 0, i64 0
%104 = bitcast i8** %103 to i32*
%105 = load i32, i32* %104
store i32 %105, i32* %16
store i32 0, i32* %15
%106 = load i32, i32* @LOG_INFO
%107 = load i32, i32* %16
%108 = call i32 (i32, i8*, ...) @syslog(i32 %106, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.11, i64 0, i64 0), i32 %107)
br label %77
109: ; preds = %90
%110 = getelementptr inbounds [8192 x i8*], [8192 x i8*]* %13, i64 0, i64 0
%111 = bitcast i8** %110 to %struct.hv_fcopy_hdr*
store %struct.hv_fcopy_hdr* %111, %struct.hv_fcopy_hdr** %14
%112 = load %struct.hv_fcopy_hdr*, %struct.hv_fcopy_hdr** %14
%113 = getelementptr inbounds %struct.hv_fcopy_hdr, %struct.hv_fcopy_hdr* %112, i32 0, i32 0
%114 = load i32, i32* %113
switch i32 %114, label %127 [
i32 130, label %115
i32 129, label %119
i32 131, label %123
i32 132, label %125
]
115: ; preds = %109
%116 = load %struct.hv_fcopy_hdr*, %struct.hv_fcopy_hdr** %14
%117 = bitcast %struct.hv_fcopy_hdr* %116 to %struct.hv_start_fcopy*
%118 = call i32 @hv_start_fcopy(%struct.hv_start_fcopy* %117)
store i32 %118, i32* %8
br label %133
119: ; preds = %109
%120 = load %struct.hv_fcopy_hdr*, %struct.hv_fcopy_hdr** %14
%121 = bitcast %struct.hv_fcopy_hdr* %120 to %struct.hv_do_fcopy*
%122 = call i32 @hv_copy_data(%struct.hv_do_fcopy* %121)
store i32 %122, i32* %8
br label %133
123: ; preds = %109
%124 = call i32 (...) @hv_copy_finished()
store i32 %124, i32* %8
br label %133
125: ; preds = %109
%126 = call i32 (...) @hv_copy_cancel()
store i32 %126, i32* %8
br label %133
127: ; preds = %109
%128 = load i32, i32* @LOG_ERR
%129 = load %struct.hv_fcopy_hdr*, %struct.hv_fcopy_hdr** %14
%130 = getelementptr inbounds %struct.hv_fcopy_hdr, %struct.hv_fcopy_hdr* %129, i32 0, i32 0
%131 = load i32, i32* %130
%132 = call i32 (i32, i8*, ...) @syslog(i32 %128, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.12, i64 0, i64 0), i32 %131)
br label %133
133: ; preds = %127, %125, %123, %119, %115
%134 = load i32, i32* %6
%135 = call i32 @pwrite(i32 %134, i32* %8, i32 4, i32 0)
%136 = sext i32 %135 to i64
%137 = icmp ne i64 %136, 4
br i1 %137, label %138, label %145
138: ; preds = %133
%139 = load i32, i32* @LOG_ERR
%140 = load i32, i32* @errno
%141 = call i32 @strerror(i32 %140)
%142 = call i32 (i32, i8*, ...) @syslog(i32 %139, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0), i32 %141)
%143 = load i32, i32* @EXIT_FAILURE
%144 = call i32 @exit(i32 %143)
unreachable
145: ; preds = %133
br label %77
}

LLVM IR (-O2):
%struct.option = type { i8*, i8, i32, i32 }
%struct.hv_do_fcopy = type { i32 }
%struct.hv_start_fcopy = type { i32 }
@FCOPY_CURRENT_VERSION = global i32 0
@main.long_options = internal global [3 x %struct.option] [%struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8 -128, i32 0, i32 104 }, %struct.option { i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i32 0, i32 0), i8 -128, i32 0, i32 110 }, %struct.option zeroinitializer]
@.str = [5 x i8] c"help\00"
@.str.1 = [10 x i8] c"no-daemon\00"
@.str.2 = [3 x i8] c"hn\00"
@EXIT_FAILURE = global i32 0
@LOG_ERR = global i32 0
@.str.3 = [27 x i8] c"daemon() failed; error: %s\00"
@errno = global i32 0
@.str.4 = [9 x i8] c"HV_FCOPY\00"
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.5 = [20 x i8] c"starting; pid is:%d\00"
@.str.6 = [20 x i8] c"/dev/vmbus/hv_fcopy\00"
@O_RDWR = global i32 0
@.str.7 = [46 x i8] c"open /dev/vmbus/hv_fcopy failed; error: %d %s\00"
@.str.8 = [24 x i8] c"Registration failed: %s\00"
@.str.9 = [17 x i8] c"pread failed: %s\00"
@.str.10 = [28 x i8] c"invalid version negotiation\00"
@.str.11 = [26 x i8] c"kernel module version: %d\00"
@.str.12 = [22 x i8] c"Unknown operation: %d\00"
@.str.13 = [18 x i8] c"pwrite failed: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [8192 x i8*]
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = bitcast i32* %5 to i8*
%10 = load i32, i32* @FCOPY_CURRENT_VERSION
store i32 %10, i32* %5
%11 = bitcast [8192 x i8*]* %6 to i8*
br label %12
12: ; preds = %12, %2
%13 = phi i1 [ false, %2 ], [ true, %12 ]
%14 = call i32 @getopt_long(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %struct.option* getelementptr inbounds ([3 x %struct.option], [3 x %struct.option]* @main.long_options, i64 0, i64 0), i32* %4)
switch i32 %14, label %15 [
i32 -1, label %19
i32 110, label %12
]
15: ; preds = %12
%16 = call i32 @print_usage(i8** %1)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
br i1 %13, label %30, label %20
20: ; preds = %19
%21 = call i64 @daemon(i32 1, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %20
%24 = load i32, i32* @LOG_ERR
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 (i32, i8*, ...) @syslog(i32 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %20, %19
%31 = load i32, i32* @LOG_USER
%32 = call i32 @openlog(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %31)
%33 = load i32, i32* @LOG_INFO
%34 = call i32 (...) @getpid()
%35 = call i32 (i32, i8*, ...) @syslog(i32 %33, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %30
%40 = load i32, i32* @LOG_ERR
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @syslog(i32 %40, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.7, i64 0, i64 0), i32 %41, i32 %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %30
%47 = call i32 @write(i32 %37, i32* %5, i32 4)
%48 = icmp eq i32 %47, 4
br i1 %48, label %49, label %64
49: ; preds = %46
%50 = getelementptr inbounds [8192 x i8*], [8192 x i8*]* %6, i64 0, i64 0
%51 = call i32 @pread(i32 %37, i8** %50, i32 8192, i32 0)
%52 = icmp slt i32 %51, 0
br i1 %52, label %71, label %53
53: ; preds = %49
%54 = bitcast [8192 x i8*]* %6 to i32*
%55 = bitcast [8192 x i8*]* %6 to %struct.hv_do_fcopy*
%56 = bitcast [8192 x i8*]* %6 to %struct.hv_start_fcopy*
%57 = icmp eq i32 %51, 4
br i1 %57, label %58, label %78
58: ; preds = %53
%59 = load i32, i32* %54
%60 = load i32, i32* @LOG_INFO
%61 = call i32 (i32, i8*, ...) @syslog(i32 %60, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.11, i64 0, i64 0), i32 %59)
%62 = call i32 @pread(i32 %37, i8** %50, i32 8192, i32 0)
%63 = icmp slt i32 %62, 0
br i1 %63, label %71, label %86
64: ; preds = %46
%65 = load i32, i32* @LOG_ERR
%66 = load i32, i32* @errno
%67 = call i32 @strerror(i32 %66)
%68 = call i32 (i32, i8*, ...) @syslog(i32 %65, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i32 %67)
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %83, %58, %49
%72 = load i32, i32* @LOG_ERR
%73 = load i32, i32* @errno
%74 = call i32 @strerror(i32 %73)
%75 = call i32 (i32, i8*, ...) @syslog(i32 %72, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i32 %74)
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %53
%79 = load i32, i32* @LOG_ERR
%80 = call i32 (i32, i8*, ...) @syslog(i32 %79, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
%81 = load i32, i32* @EXIT_FAILURE
%82 = call i32 @exit(i32 %81)
unreachable
83: ; preds = %99
%84 = call i32 @pread(i32 %37, i8** %50, i32 8192, i32 0)
%85 = icmp slt i32 %84, 0
br i1 %85, label %71, label %86
86: ; preds = %58, %83
%87 = load i32, i32* %54
switch i32 %87, label %96 [
i32 130, label %88
i32 129, label %90
i32 131, label %92
i32 132, label %94
]
88: ; preds = %86
%89 = call i32 @hv_start_fcopy(%struct.hv_start_fcopy* %56)
store i32 %89, i32* %3
br label %99
90: ; preds = %86
%91 = call i32 @hv_copy_data(%struct.hv_do_fcopy* %55)
store i32 %91, i32* %3
br label %99
92: ; preds = %86
%93 = call i32 (...) @hv_copy_finished()
store i32 %93, i32* %3
br label %99
94: ; preds = %86
%95 = call i32 (...) @hv_copy_cancel()
store i32 %95, i32* %3
br label %99
96: ; preds = %86
%97 = load i32, i32* @LOG_ERR
%98 = call i32 (i32, i8*, ...) @syslog(i32 %97, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.12, i64 0, i64 0), i32 %87)
br label %99
99: ; preds = %96, %94, %92, %90, %88
%100 = call i32 @pwrite(i32 %37, i32* %3, i32 4, i32 0)
%101 = icmp eq i32 %100, 4
br i1 %101, label %83, label %102
102: ; preds = %99
%103 = load i32, i32* @LOG_ERR
%104 = load i32, i32* @errno
%105 = call i32 @strerror(i32 %104)
%106 = call i32 (i32, i8*, ...) @syslog(i32 %103, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0), i32 %105)
%107 = load i32, i32* @EXIT_FAILURE
%108 = call i32 @exit(i32 %107)
unreachable
}