#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int newDbRestoreTable(char *tablename, char *data, int len) {
  ;
  char filename[40];
  char backup[40];
  if (tablename && data && len > 0) {
    sprintf(filename, "%siot_%s.db", "/usr/share/iot/", tablename);
    sprintf(backup, "%siot_%s.bck", "/usr/share/iot/", tablename);
    int fd = open(filename, O_RDONLY);
    if (fd >= 0) {
      ;
    } else {
      ;
      fd = open(backup, O_RDONLY);
    }
    if (fd >= 0) {
      int bytestoread = len;
      int bytesread;
      char *pbuf = data;
      while ((bytestoread > 0) &&
             (bytesread = read(fd, pbuf, bytestoread)) > 0) {
        bytestoread -= bytesread;
        pbuf += bytesread;
      }
      close(fd);
      if (bytestoread == 0) {
        ;
      }
      return (bytestoread == 0);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<newDbRestoreTable>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12cd <newDbRestoreTable+0x18d>
cmpq   $0x0,-0x18(%rbp)
je     12cd <newDbRestoreTable+0x18d>
cmpl   $0x0,-0x1c(%rbp)
jle    12cd <newDbRestoreTable+0x18d>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe7b(%rip),%rsi
lea    0xe80(%rip),%rdx
call   1060 <sprintf@plt>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe7c(%rip),%rsi
lea    0xe65(%rip),%rdx
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jl     11d6 <newDbRestoreTable+0x96>
jmp    11ee <newDbRestoreTable+0xae>
lea    -0x80(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jl     12c8 <newDbRestoreTable+0x188>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x88(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x98(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x88(%rbp)
mov    %al,-0x99(%rbp)
jle    124e <newDbRestoreTable+0x10e>
mov    -0x84(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    -0x88(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x8c(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x99(%rbp)
mov    -0x99(%rbp),%al
test   $0x1,%al
jne    1261 <newDbRestoreTable+0x121>
jmp    1294 <newDbRestoreTable+0x154>
mov    -0x8c(%rbp),%ecx
mov    -0x88(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x88(%rbp)
mov    -0x8c(%rbp),%ecx
mov    -0x98(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x98(%rbp)
jmp    120f <newDbRestoreTable+0xcf>
mov    -0x84(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x88(%rbp)
jne    12b1 <newDbRestoreTable+0x171>
jmp    12b1 <newDbRestoreTable+0x171>
cmpl   $0x0,-0x88(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    12d4 <newDbRestoreTable+0x194>
jmp    12cd <newDbRestoreTable+0x18d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newDbRestoreTable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
xor    %r14d,%r14d
test   %rdi,%rdi
je     120b <newDbRestoreTable+0xcb>
mov    %rsi,%rbx
test   %rsi,%rsi
je     120b <newDbRestoreTable+0xcb>
mov    %edx,%ebp
test   %edx,%edx
jle    120b <newDbRestoreTable+0xcb>
mov    %rdi,%r15
lea    0xe86(%rip),%rsi
lea    0xe8b(%rip),%r12
lea    0x30(%rsp),%r13
mov    %r13,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
call   1060 <sprintf@plt>
lea    0xe81(%rip),%rsi
mov    %rsp,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
call   1060 <sprintf@plt>
mov    0x2e38(%rip),%r12
mov    (%r12),%esi
mov    %r13,%rdi
call   1050 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11e0 <newDbRestoreTable+0xa0>
mov    (%r12),%esi
mov    %rsp,%rdi
call   1050 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     120b <newDbRestoreTable+0xcb>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11fa <newDbRestoreTable+0xba>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     11e0 <newDbRestoreTable+0xa0>
mov    %r15d,%edi
call   1030 <close@plt>
xor    %r14d,%r14d
test   %ebp,%ebp
sete   %r14b
mov    %r14d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%siot_%s.db\00"
@.str.1 = [16 x i8] c"/usr/share/iot/\00"
@.str.2 = [13 x i8] c"%siot_%s.bck\00"
@O_RDONLY = global i32 0
define i32 @newDbRestoreTable(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [40 x i8]
%9 = alloca [40 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %75
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %75
19: ; preds = %16
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %75
22: ; preds = %19
%23 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%24 = load i8*, i8** %5
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%27 = load i8*, i8** %5
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp sge i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %22
br label %39
35: ; preds = %22
%36 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%37 = load i32, i32* @O_RDONLY
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %10
br label %39
39: ; preds = %35, %34
%40 = load i32, i32* %10
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %74
42: ; preds = %39
%43 = load i32, i32* %7
store i32 %43, i32* %11
%44 = load i8*, i8** %6
store i8* %44, i8** %13
br label %45
45: ; preds = %56, %42
%46 = load i32, i32* %11
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* %10
%50 = load i8*, i8** %13
%51 = load i32, i32* %11
%52 = call i32 @read(i32 %49, i8* %50, i32 %51)
store i32 %52, i32* %12
%53 = icmp sgt i32 %52, 0
br label %54
54: ; preds = %48, %45
%55 = phi i1 [ false, %45 ], [ %53, %48 ]
br i1 %55, label %56, label %64
56: ; preds = %54
%57 = load i32, i32* %12
%58 = load i32, i32* %11
%59 = sub nsw i32 %58, %57
store i32 %59, i32* %11
%60 = load i32, i32* %12
%61 = load i8*, i8** %13
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8, i8* %61, i64 %62
store i8* %63, i8** %13
br label %45
64: ; preds = %54
%65 = load i32, i32* %10
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %11
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %64
br label %70
70: ; preds = %69, %64
%71 = load i32, i32* %11
%72 = icmp eq i32 %71, 0
%73 = zext i1 %72 to i32
store i32 %73, i32* %4
br label %76
74: ; preds = %39
br label %75
75: ; preds = %74, %19, %16, %3
store i32 0, i32* %4
br label %76
76: ; preds = %75, %70
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
@.str = [12 x i8] c"%siot_%s.db\00"
@.str.1 = [16 x i8] c"/usr/share/iot/\00"
@.str.2 = [13 x i8] c"%siot_%s.bck\00"
@O_RDONLY = global i32 0
define i32 @newDbRestoreTable(i8* %0, i8* %1, i32 %2) {
%4 = alloca [40 x i8]
%5 = alloca [40 x i8]
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%8 = icmp ne i8* %0, null
%9 = icmp ne i8* %1, null
%10 = and i1 %8, %9
%11 = icmp sgt i32 %2, 0
%12 = and i1 %10, %11
br i1 %12, label %13, label %40
13: ; preds = %3
%14 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%15 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %6, i32 %16)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %7, i32 %20)
%22 = icmp sgt i32 %21, -1
br i1 %22, label %23, label %40
23: ; preds = %13, %19
%24 = phi i32 [ %21, %19 ], [ %17, %13 ]
br label %25
25: ; preds = %23, %30
%26 = phi i8* [ %33, %30 ], [ %1, %23 ]
%27 = phi i32 [ %31, %30 ], [ %2, %23 ]
%28 = tail call i32 @read(i32 %24, i8* %26, i32 %27)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = sub nsw i32 %27, %28
%32 = zext i32 %28 to i64
%33 = getelementptr inbounds i8, i8* %26, i64 %32
%34 = icmp sgt i32 %31, 0
br i1 %34, label %25, label %35
35: ; preds = %30, %25
%36 = phi i32 [ %31, %30 ], [ %27, %25 ]
%37 = tail call i32 @close(i32 %24)
%38 = icmp eq i32 %36, 0
%39 = zext i1 %38 to i32
br label %40
40: ; preds = %3, %19, %35
%41 = phi i32 [ %39, %35 ], [ 0, %19 ], [ 0, %3 ]
ret i32 %41
}