#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*/ EXIT_FAILURE;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
char *alloc(int, float);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ err(int, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int open(char *, int, int);
int snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int open_db(char *path, char *file, int update) {
  int plen = strlen(path), flen = strlen(file);
  char *fn;
  int fd;
  assert(plen > 0);
  assert(flen > 0);
  plen += strlen(file) + 2;
  fn = alloc(plen, 'f');
  if (plen <= snprintf(fn, plen, "%s/%s", path, file)) {
    fprintf(stderr, "snprintf error\n");
    exit(EXIT_FAILURE);
  }
  fd = open(fn, O_RDWR | (update ? 0 : O_TRUNC) | O_CREAT, 0744);
  if (0)
    fprintf(stderr, "opening %s, got fd %d\n", fn, fd);
  if (fd == -1)
    err(1, "%s", fn);
  free(fn);
  return fd;
}


x86 assembly (clang, -O0):
<open_db>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x2,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
movss  0xe06(%rip),%xmm0
call   1090 <alloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xde5(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jg     1256 <open_db+0xc6>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     127e <open_db+0xee>
xor    %eax,%eax
mov    %eax,-0x44(%rbp)
jmp    128a <open_db+0xfa>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x44(%rbp),%eax
or     %eax,%esi
mov    0x2d5b(%rip),%rax
or     (%rax),%esi
mov    $0x1e4,%edx
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12cb <open_db+0x13b>
mov    -0x28(%rbp),%rdx
mov    $0x1,%edi
lea    0xd41(%rip),%rsi
call   1080 <err@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_db>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %ebp,%ebp
setg   %dil
call   1060 <assert@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
add    $0x2,%ebp
movss  0xe1e(%rip),%xmm0
mov    %ebp,%edi
call   1090 <alloc@plt>
mov    %rax,%rbx
lea    0xe11(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r15,%rcx
mov    %r12,%r8
call   1050 <snprintf@plt>
cmp    %eax,%ebp
jle    1264 <open_db+0xd4>
mov    0x2dca(%rip),%rax
xor    %esi,%esi
test   %r14d,%r14d
jne    121e <open_db+0x8e>
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd1(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1e4,%edx
call   10a0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1251 <open_db+0xc1>
lea    0xdc3(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1080 <err@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"snprintf error\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @open_db(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %8
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* %8
%20 = icmp sgt i32 %19, 0
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i8*, i8** %5
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %24, 2
%26 = load i32, i32* %7
%27 = add nsw i32 %26, %25
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = call i8* @alloc(i32 %28, float 1.020000e+02)
store i8* %29, i8** %9
%30 = load i32, i32* %7
%31 = load i8*, i8** %9
%32 = load i32, i32* %7
%33 = load i8*, i8** %4
%34 = load i8*, i8** %5
%35 = call i32 @snprintf(i8* %31, i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %33, i8* %34)
%36 = icmp sle i32 %30, %35
br i1 %36, label %37, label %42
37: ; preds = %3
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %3
%43 = load i8*, i8** %9
%44 = load i32, i32* @O_RDWR
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
br label %50
48: ; preds = %42
%49 = load i32, i32* @O_TRUNC
br label %50
50: ; preds = %48, %47
%51 = phi i32 [ 0, %47 ], [ %49, %48 ]
%52 = or i32 %44, %51
%53 = load i32, i32* @O_CREAT
%54 = or i32 %52, %53
%55 = call i32 @open(i8* %43, i32 %54, i32 484)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %50
%59 = load i8*, i8** %9
%60 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %58, %50
%62 = load i8*, i8** %9
%63 = call i32 @free(i8* %62)
%64 = load i32, i32* %10
ret i32 %64
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"snprintf error\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @open_db(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = icmp sgt i32 %4, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = icmp sgt i32 %5, 0
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32 @strlen(i8* %1)
%13 = add i32 %4, 2
%14 = add i32 %13, %12
%15 = tail call i8* @alloc(i32 %14, float 1.020000e+02)
%16 = tail call i32 @snprintf(i8* %15, i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%17 = icmp sgt i32 %14, %16
br i1 %17, label %23, label %18
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %3
%24 = load i32, i32* @O_RDWR
%25 = icmp eq i32 %2, 0
%26 = load i32, i32* @O_TRUNC
%27 = select i1 %25, i32 %26, i32 0
%28 = or i32 %27, %24
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = tail call i32 @open(i8* %15, i32 %30, i32 484)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %35
35: ; preds = %33, %23
%36 = tail call i32 @free(i8* %15)
ret i32 %31
}