#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ die(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ *tmpfile();
void removeevent(int rmid, char *database, int status, int errno) {
  FILE *fd, *fd_temp;
  int nread;
  char *buffer = NULL;
  int pos = 1;
  size_t len = 0;
  assert(database != NULL);
  fd = fopen(database, "r");
  fd_temp = tmpfile();
  while ((nread = getline(&buffer, &len, fd)) != -1) {
    if (rmid != pos && buffer != NULL) {
      fprintf(fd_temp, "%s", buffer);
    }
    pos++;
  }
  free(buffer);
  fclose(fd);
  fseek(fd_temp, 0, SEEK_SET);
  fd = fopen(database, "w+");
  buffer = (char *)malloc(1024);
  pos = ftell(fd_temp);
  while ((nread = getline(&buffer, &len, fd_temp)) != -1) {
    fprintf(fd, "%s", buffer);
  }
  free(buffer);
  fclose(fd_temp);
  fclose(fd);
  die(status, errno);
}


x86 assembly (clang, -O0):
<removeevent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <tmpfile@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdx
lea    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   10d0 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     1263 <removeevent+0xb3>
mov    -0x4(%rbp),%eax
cmp    -0x3c(%rbp),%eax
je     1255 <removeevent+0xa5>
cmpq   $0x0,-0x38(%rbp)
je     1255 <removeevent+0xa5>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xdb2(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    120d <removeevent+0x5d>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x10(%rbp),%rdi
lea    0xd71(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    $0x400,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdx
lea    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   10d0 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     12ed <removeevent+0x13d>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xd1f(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12b7 <removeevent+0x107>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10c0 <die@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<removeevent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x14(%rsp)
mov    %edx,0x10(%rsp)
mov    %rsi,%r12
mov    %edi,%ebp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1070 <assert@plt>
lea    0xe0f(%rip),%rsi
mov    %r12,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <tmpfile@plt>
mov    %rax,%r13
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   10d0 <getline@plt>
cmp    $0xffffffff,%eax
je     125b <removeevent+0xab>
add    $0xffffffff,%ebp
mov    %rsp,%r14
lea    0x8(%rsp),%r15
jmp    1243 <removeevent+0x93>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10d0 <getline@plt>
cmp    $0xffffffff,%eax
je     125b <removeevent+0xab>
add    $0xffffffff,%ebp
jae    1230 <removeevent+0x80>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1230 <removeevent+0x80>
mov    %r13,%rsi
call   1050 <fputs@plt>
jmp    1230 <removeevent+0x80>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0xd7c(%rip),%rsi
mov    %r12,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    $0x400,%edi
call   1090 <malloc@plt>
mov    %rax,(%rsp)
mov    %r13,%rdi
call   1080 <ftell@plt>
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %r13,%rdx
call   10d0 <getline@plt>
cmp    $0xffffffff,%eax
je     12ef <removeevent+0x13f>
mov    %rsp,%r14
lea    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rsp),%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   10d0 <getline@plt>
cmp    $0xffffffff,%eax
jne    12d0 <removeevent+0x120>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%edi
mov    0x14(%rsp),%esi
call   10c0 <die@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%s\00"
@SEEK_SET = global i32 0
@.str.2 = [3 x i8] c"w+\00"
define void @removeevent(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i8* null, i8** %12
store i32 1, i32* %13
store i64 0, i64* %14
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %6
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %9
%21 = call i32* (...) @tmpfile()
store i32* %21, i32** %10
br label %22
22: ; preds = %37, %4
%23 = load i32*, i32** %9
%24 = call i32 @getline(i8** %12, i64* %14, i32* %23)
store i32 %24, i32* %11
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %40
26: ; preds = %22
%27 = load i32, i32* %5
%28 = load i32, i32* %13
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %37
30: ; preds = %26
%31 = load i8*, i8** %12
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i32*, i32** %10
%35 = load i8*, i8** %12
%36 = call i32 @fprintf(i32* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %33, %30, %26
%38 = load i32, i32* %13
%39 = add nsw i32 %38, 1
store i32 %39, i32* %13
br label %22
40: ; preds = %22
%41 = load i8*, i8** %12
%42 = call i32 @free(i8* %41)
%43 = load i32*, i32** %9
%44 = call i32 @fclose(i32* %43)
%45 = load i32*, i32** %10
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @fseek(i32* %45, i32 0, i32 %46)
%48 = load i8*, i8** %6
%49 = call i32* @fopen(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %49, i32** %9
%50 = call i64 @malloc(i32 1024)
%51 = inttoptr i64 %50 to i8*
store i8* %51, i8** %12
%52 = load i32*, i32** %10
%53 = call i32 @ftell(i32* %52)
store i32 %53, i32* %13
br label %54
54: ; preds = %58, %40
%55 = load i32*, i32** %10
%56 = call i32 @getline(i8** %12, i64* %14, i32* %55)
store i32 %56, i32* %11
%57 = icmp ne i32 %56, -1
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = load i32*, i32** %9
%60 = load i8*, i8** %12
%61 = call i32 @fprintf(i32* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %60)
br label %54
62: ; preds = %54
%63 = load i8*, i8** %12
%64 = call i32 @free(i8* %63)
%65 = load i32*, i32** %10
%66 = call i32 @fclose(i32* %65)
%67 = load i32*, i32** %9
%68 = call i32 @fclose(i32* %67)
%69 = load i32, i32* %7
%70 = load i32, i32* %8
%71 = call i32 @die(i32 %69, i32 %70)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.2 = [3 x i8] c"w+\00"
define void @removeevent(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%8 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%9 = icmp ne i8* %1, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32* (...) @tmpfile()
%14 = call i32 @getline(i8** %5, i64* %6, i32* %12)
%15 = icmp eq i32 %14, -1
br i1 %15, label %28, label %16
16: ; preds = %4, %24
%17 = phi i32 [ %25, %24 ], [ 1, %4 ]
%18 = icmp ne i32 %17, %0
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
%21 = select i1 %18, i1 %20, i1 false
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @fputs(i8* %19, i32* %13)
br label %24
24: ; preds = %22, %16
%25 = add nuw nsw i32 %17, 1
%26 = call i32 @getline(i8** %5, i64* %6, i32* %12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %16
28: ; preds = %24, %4
%29 = load i8*, i8** %5
%30 = call i32 @free(i8* %29)
%31 = call i32 @fclose(i32* %12)
%32 = load i32, i32* @SEEK_SET
%33 = call i32 @fseek(i32* %13, i32 0, i32 %32)
%34 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%35 = call i64 @malloc(i32 1024)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %5
%37 = call i32 @ftell(i32* %13)
%38 = call i32 @getline(i8** %5, i64* %6, i32* %13)
%39 = icmp eq i32 %38, -1
br i1 %39, label %45, label %40
40: ; preds = %28, %40
%41 = load i8*, i8** %5
%42 = call i32 @fputs(i8* %41, i32* %34)
%43 = call i32 @getline(i8** %5, i64* %6, i32* %13)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %40
45: ; preds = %40, %28
%46 = load i8*, i8** %5
%47 = call i32 @free(i8* %46)
%48 = call i32 @fclose(i32* %13)
%49 = call i32 @fclose(i32* %34)
%50 = call i32 @die(i32 %2, i32 %3)
ret void
}