#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 /*<<< orphan*/ F_OK;
int /*<<< orphan*/ O_CREAT;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ S_IRWXU;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ configure_tty(int);
scalar_t__ do_handshake(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ ramdump_recv(int);
int /*<<< orphan*/ remove(char *);
int main(int argc, char *argv[]) {
  int ret;
  int dev_fd;
  int dev_lock_fd;
  char *dev_file;
  ret = 1;
  if (argc < 2) {
    printf("Usage: ./ramdump <device>\n");
    printf("Ex   : ./ramdump /dev/ttyUSB1\n");
    return -1;
  }
  dev_file = argv[1];
  if (access("/var/lock/LCK..ttyUSB1", F_OK) == 0) {
    printf("Error : couldnt lock serial port device %s\n", "/dev/ttyUSB1");
    printf("Please close any other process using this port first\n");
    goto lock_check_err;
  }
  if ((dev_fd = open(dev_file, O_RDWR | O_NOCTTY | O_SYNC)) == -1) {
    printf("%s open failed\n", dev_file);
    goto dev_open_err;
  }
  if (configure_tty(dev_fd) != 0) {
    printf("tty configuration failed\n");
    goto tty_config_err;
  }
  if ((dev_lock_fd = open("/var/lock/LCK..ttyUSB1", O_CREAT, S_IRWXU)) == -1) {
    printf("tty lock  failed\n");
    goto tty_lock_err;
  }
  if (do_handshake(dev_fd) != 0) {
    printf("Target Handshake Failed\n");
    goto handshake_err;
  }
  printf("Target entered to ramdump mode\n");
  printf("Receiving ramdump......\n");
  if (ramdump_recv(dev_fd) != 0) {
    printf("Ramdump receive failed\n");
    goto ramdump_err;
  }
  printf("Ramdump received successfully\n");
  ret = 0;
ramdump_err:
handshake_err:
dl_mode_err:
dl_cmd_err:
  remove("/var/lock/LCK..ttyUSB1");
  close(dev_lock_fd);
tty_lock_err:
tty_config_err:
  close(dev_fd);
dev_open_err:
lock_check_err:
bin_type_err:
  return ret;
}


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)
movl   $0x1,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11cf <main+0x4f>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <main+0x20a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   10a0 <access@plt>
cmp    $0x0,%rax
jne    1222 <main+0xa2>
lea    0xe50(%rip),%rdi
lea    0xe75(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    137f <main+0x1ff>
mov    -0x28(%rbp),%rdi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
mov    0x2d91(%rip),%rax
or     (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    126b <main+0xeb>
mov    -0x28(%rbp),%rsi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    137a <main+0x1fa>
mov    -0x18(%rbp),%edi
call   1070 <configure_tty@plt>
cmp    $0x0,%rax
je     1290 <main+0x110>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1372 <main+0x1f2>
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    12cf <main+0x14f>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    136d <main+0x1ed>
mov    -0x18(%rbp),%edi
call   1080 <do_handshake@plt>
cmp    $0x0,%rax
je     12f4 <main+0x174>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    134f <main+0x1cf>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1040 <ramdump_recv@plt>
cmp    $0x0,%rax
je     1335 <main+0x1b5>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    134a <main+0x1ca>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
jmp    134f <main+0x1cf>
jmp    1354 <main+0x1d4>
jmp    1359 <main+0x1d9>
lea    0xcda(%rip),%rdi
call   1030 <remove@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
jmp    1372 <main+0x1f2>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    137f <main+0x1ff>
jmp    1384 <main+0x204>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     11bb <main+0x2b>
lea    0xf96(%rip),%rdi
call   1040 <puts@plt>
lea    0xfa4(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1315 <main+0x185>
mov    0x8(%rsi),%rbp
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   10b0 <access@plt>
test   %rax,%rax
je     1227 <main+0x97>
mov    0x2df8(%rip),%rax
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dde(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1252 <main+0xc2>
mov    %eax,%ebx
mov    %eax,%edi
call   1080 <configure_tty@plt>
test   %rax,%rax
je     126d <main+0xdd>
lea    0xed0(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
jmp    130e <main+0x17e>
lea    0xde9(%rip),%rdi
lea    0xf2c(%rip),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xebe(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
jmp    1315 <main+0x185>
lea    0xdea(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    1315 <main+0x185>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
lea    0xd7a(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12af <main+0x11f>
mov    %eax,%r14d
mov    %ebx,%edi
call   1090 <do_handshake@plt>
test   %rax,%rax
je     12bb <main+0x12b>
mov    $0x1,%ebp
lea    0xe12(%rip),%rdi
jmp    12f5 <main+0x165>
lea    0xe21(%rip),%rdi
jmp    1218 <main+0x88>
lea    0xd91(%rip),%rdi
call   1040 <puts@plt>
lea    0xda4(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1050 <ramdump_recv@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
lea    0xda0(%rip),%rax
lea    0xdb7(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
lea    0xcff(%rip),%rdi
call   1030 <remove@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Usage: ./ramdump <device>\0A\00"
@.str.1 = [31 x i8] c"Ex : ./ramdump /dev/ttyUSB1\0A\00"
@.str.2 = [23 x i8] c"/var/lock/LCK..ttyUSB1\00"
@F_OK = global i32 0
@.str.3 = [44 x i8] c"Error : couldnt lock serial port device %s\0A\00"
@.str.4 = [13 x i8] c"/dev/ttyUSB1\00"
@.str.5 = [54 x i8] c"Please close any other process using this port first\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.6 = [16 x i8] c"%s open failed\0A\00"
@.str.7 = [26 x i8] c"tty configuration failed\0A\00"
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str.8 = [18 x i8] c"tty lock failed\0A\00"
@.str.9 = [25 x i8] c"Target Handshake Failed\0A\00"
@.str.10 = [32 x i8] c"Target entered to ramdump mode\0A\00"
@.str.11 = [25 x i8] c"Receiving ramdump......\0A\00"
@.str.12 = [24 x i8] c"Ramdump receive failed\0A\00"
@.str.13 = [31 x i8] c"Ramdump received successfully\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 i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %81
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %9
%19 = load i32, i32* @F_OK
%20 = call i64 @access(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
br label %78
25: ; preds = %15
%26 = load i8*, i8** %9
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_NOCTTY
%29 = or i32 %27, %28
%30 = load i32, i32* @O_SYNC
%31 = or i32 %29, %30
%32 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31)
store i32 %32, i32* %7
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = load i8*, i8** %9
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %35)
br label %77
37: ; preds = %25
%38 = load i32, i32* %7
%39 = call i64 @configure_tty(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
br label %74
43: ; preds = %37
%44 = load i32, i32* @O_CREAT
%45 = load i32, i32* @S_IRWXU
%46 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45)
store i32 %46, i32* %8
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %73
50: ; preds = %43
%51 = load i32, i32* %7
%52 = call i64 @do_handshake(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
br label %67
56: ; preds = %50
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0))
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0))
%59 = load i32, i32* %7
%60 = call i64 @ramdump_recv(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0))
br label %66
64: ; preds = %56
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0))
store i32 0, i32* %6
br label %66
66: ; preds = %64, %62
br label %67
67: ; preds = %66, %54
br label %68
68: ; preds = %67
br label %69
69: ; preds = %68
%70 = call i32 @remove(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %69, %48
br label %74
74: ; preds = %73, %41
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %74, %34
br label %78
78: ; preds = %77, %22
br label %79
79: ; preds = %78
%80 = load i32, i32* %6
store i32 %80, i32* %3
br label %81
81: ; preds = %79, %12
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str.2 = [23 x i8] c"/var/lock/LCK..ttyUSB1\00"
@F_OK = global i32 0
@.str.3 = [44 x i8] c"Error : couldnt lock serial port device %s\0A\00"
@.str.4 = [13 x i8] c"/dev/ttyUSB1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.6 = [16 x i8] c"%s open failed\0A\00"
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@str = [31 x i8] c"Target entered to ramdump mode\00"
@str.14 = [24 x i8] c"Receiving ramdump......\00"
@str.15 = [30 x i8] c"Ramdump received successfully\00"
@str.16 = [23 x i8] c"Ramdump receive failed\00"
@str.17 = [24 x i8] c"Target Handshake Failed\00"
@str.18 = [17 x i8] c"tty lock failed\00"
@str.19 = [25 x i8] c"tty configuration failed\00"
@str.20 = [53 x i8] c"Please close any other process using this port first\00"
@str.21 = [26 x i8] c"Usage: ./ramdump <device>\00"
@str.22 = [30 x i8] c"Ex : ./ramdump /dev/ttyUSB1\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.21, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.22, i64 0, i64 0))
br label %58
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @F_OK
%11 = tail call i64 @access(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.20, i64 0, i64 0))
br label %58
16: ; preds = %7
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_NOCTTY
%19 = or i32 %18, %17
%20 = load i32, i32* @O_SYNC
%21 = or i32 %19, %20
%22 = tail call i32 (i8*, i32, ...) @open(i8* %9, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %9)
br label %58
26: ; preds = %16
%27 = tail call i64 @configure_tty(i32 %22)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.19, i64 0, i64 0))
br label %55
31: ; preds = %26
%32 = load i32, i32* @O_CREAT
%33 = load i32, i32* @S_IRWXU
%34 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.18, i64 0, i64 0))
br label %55
38: ; preds = %31
%39 = tail call i64 @do_handshake(i32 %22)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.14, i64 0, i64 0))
%44 = tail call i64 @ramdump_recv(i32 %22)
%45 = icmp eq i64 %44, 0
%46 = select i1 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @str.15, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str.16, i64 0, i64 0)
%47 = xor i1 %45, true
%48 = zext i1 %47 to i32
br label %49
49: ; preds = %41, %38
%50 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @str.17, i64 0, i64 0), %38 ], [ %46, %41 ]
%51 = phi i32 [ 1, %38 ], [ %48, %41 ]
%52 = tail call i32 @puts(i8* dereferenceable(1) %50)
%53 = tail call i32 @remove(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%54 = tail call i32 @close(i32 %34)
br label %55
55: ; preds = %36, %49, %29
%56 = phi i32 [ 1, %29 ], [ 1, %36 ], [ %51, %49 ]
%57 = tail call i32 @close(i32 %22)
br label %58
58: ; preds = %24, %55, %13, %4
%59 = phi i32 [ -1, %4 ], [ 1, %13 ], [ 1, %24 ], [ %56, %55 ]
ret i32 %59
}