#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_TRUNC;
int O_WRONLY;
int S_IREAD;
int S_IWRITE;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int log_seqno(const char *dirname, const char *type, char *filename) {
  char seqfilename[80];
  int seq_fd;
  int ret = 0;
  sprintf(seqfilename, "%s/%s.seqno", dirname, type);
  seq_fd = open(seqfilename, O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  if (seq_fd > 0) {
    if (write(seq_fd, filename, strlen(filename)) != strlen(filename))
      ret = -2;
    close(seq_fd);
  } else
    ret = -1;
  return ret;
}


x86 assembly (clang, -O0):
<log_seqno>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe7f(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    0x2e23(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jle    122c <log_seqno+0xdc>
mov    -0x74(%rbp),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x80(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x80(%rbp),%rax
cmp    %rcx,%rax
je     121f <log_seqno+0xcf>
movl   $0xfffffffe,-0x78(%rbp)
mov    -0x74(%rbp),%edi
call   1050 <close@plt>
jmp    1233 <log_seqno+0xe3>
movl   $0xffffffff,-0x78(%rbp)
mov    -0x78(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_seqno>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rdx,%r14
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0xe98(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1070 <sprintf@plt>
mov    0x2e3e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e64(%rip),%rax
or     (%rax),%esi
mov    0x2e33(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   1060 <open@plt>
test   %eax,%eax
jle    11e5 <log_seqno+0x95>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
xor    %ecx,%ecx
cmp    %rax,%rbx
sete   %cl
lea    (%rcx,%rcx,1),%ebx
add    $0xfffffffe,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11ea <log_seqno+0x9a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s/%s.seqno\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
define i32 @log_seqno(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [80 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %9
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IREAD
%21 = load i32, i32* @S_IWRITE
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %14, i32 %19, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %3
%27 = load i32, i32* %8
%28 = load i8*, i8** %6
%29 = load i8*, i8** %6
%30 = call i64 @strlen(i8* %29)
%31 = call i64 @write(i32 %27, i8* %28, i64 %30)
%32 = load i8*, i8** %6
%33 = call i64 @strlen(i8* %32)
%34 = icmp ne i64 %31, %33
br i1 %34, label %35, label %36
35: ; preds = %26
store i32 -2, i32* %9
br label %36
36: ; preds = %35, %26
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
br label %40
39: ; preds = %3
store i32 -1, i32* %9
br label %40
40: ; preds = %39, %36
%41 = load i32, i32* %9
ret i32 %41
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s/%s.seqno\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
define i32 @log_seqno(i8* %0, i8* %1, i8* nocapture %2) {
%4 = alloca [80 x i8]
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IREAD
%13 = load i32, i32* @S_IWRITE
%14 = or i32 %13, %12
%15 = call i32 @open(i8* %5, i32 %11, i32 %14)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %3
%18 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%19 = tail call i64 @write(i32 %15, i8* %2, i64 %18)
%20 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%21 = icmp eq i64 %19, %20
%22 = select i1 %21, i32 0, i32 -2
%23 = tail call i32 @close(i32 %15)
br label %24
24: ; preds = %3, %17
%25 = phi i32 [ %22, %17 ], [ -1, %3 ]
ret i32 %25
}