#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 */
struct evemu_device {
  int dummy;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
struct evemu_device *create_device(int /*<<< orphan*/ *);
int /*<<< orphan*/ evemu_delete(struct evemu_device *);
int evemu_play(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int open_evemu_device(struct evemu_device *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int play_from_file(int recording_fd) {
  FILE *fp;
  struct evemu_device *dev = NULL;
  int fd;
  fp = fdopen(recording_fd, "r");
  if (!fp) {
    fprintf(stderr, "error: could not open file (%m)\n");
    return -1;
  }
  dev = create_device(fp);
  if (!dev) {
    fprintf(stderr, "error: could not create device: %m\n");
    fclose(fp);
    return -1;
  }
  fd = open_evemu_device(dev);
  if (fd < 0)
    goto out;
  while (1) {
    int ret;
    char line[32];
    printf("Hit enter to start replaying");
    fflush(stdout);
    fgets(line, sizeof(line), stdin);
    fseek(fp, 0, SEEK_SET);
    ret = evemu_play(fp, fd);
    if (ret != 0) {
      fprintf(stderr, "error: could not replay device\n");
      break;
    }
  }
out:
  evemu_delete(dev);
  fclose(fp);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<play_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe23(%rip),%rsi
call   10d0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1212 <play_from_file+0x52>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <play_from_file+0x157>
mov    -0x10(%rbp),%rdi
call   1030 <create_device@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1254 <play_from_file+0x94>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <play_from_file+0x157>
mov    -0x18(%rbp),%rdi
call   10e0 <open_evemu_device@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    126f <play_from_file+0xaf>
jmp    12f6 <play_from_file+0x136>
jmp    1274 <play_from_file+0xb4>
lea    0xdcc(%rip),%rdi
call   1070 <printf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
lea    -0x40(%rbp),%rdi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
mov    $0x20,%esi
call   1090 <fgets@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <evemu_play@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12ec <play_from_file+0x12c>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
lea    0xd82(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    12f1 <play_from_file+0x131>
jmp    1274 <play_from_file+0xb4>
jmp    12f6 <play_from_file+0x136>
mov    -0x18(%rbp),%rdi
call   1050 <evemu_delete@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<play_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xe2b(%rip),%rsi
call   10d0 <fdopen@plt>
test   %rax,%rax
je     12a5 <play_from_file+0xe5>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <create_device@plt>
test   %rax,%rax
je     12bc <play_from_file+0xfc>
mov    %rax,%r14
mov    %rax,%rdi
call   10e0 <open_evemu_device@plt>
mov    %eax,%ebp
test   %eax,%eax
js     128a <play_from_file+0xca>
mov    %r14,0x8(%rsp)
mov    0x2db0(%rip),%r13
mov    0x2dc9(%rip),%r14
lea    0x10(%rsp),%r12
mov    0x2da5(%rip),%r15
nopl   0x0(%rax,%rax,1)
lea    0xe10(%rip),%rdi
call   1070 <printf@plt>
mov    0x0(%r13),%edi
call   10b0 <fflush@plt>
mov    (%r14),%edx
mov    %r12,%rdi
mov    $0x20,%esi
call   1090 <fgets@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <evemu_play@plt>
test   %eax,%eax
je     1230 <play_from_file+0x70>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x8(%rsp),%r14
mov    %r14,%rdi
call   1050 <evemu_delete@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    12de <play_from_file+0x11e>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    12d9 <play_from_file+0x119>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
call   10a0 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.evemu_device = type { i32 }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"error: could not open file (%m)\0A\00"
@.str.2 = [36 x i8] c"error: could not create device: %m\0A\00"
@.str.3 = [29 x i8] c"Hit enter to start replaying\00"
@stdout = global i32 0
@stdin = global i32 0
@SEEK_SET = global i32 0
@.str.4 = [32 x i8] c"error: could not replay device\0A\00"
define i32 @play_from_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.evemu_device*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [32 x i8]
store i32 %0, i32* %3
store %struct.evemu_device* null, %struct.evemu_device** %5
%9 = load i32, i32* %3
%10 = call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
16: ; preds = %1
%17 = load i32*, i32** %4
%18 = call %struct.evemu_device* @create_device(i32* %17)
store %struct.evemu_device* %18, %struct.evemu_device** %5
%19 = load %struct.evemu_device*, %struct.evemu_device** %5
%20 = icmp ne %struct.evemu_device* %19, null
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
store i32 -1, i32* %2
br label %60
26: ; preds = %16
%27 = load %struct.evemu_device*, %struct.evemu_device** %5
%28 = call i32 @open_evemu_device(%struct.evemu_device* %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
br label %53
32: ; preds = %26
br label %33
33: ; preds = %32, %51
%34 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @stdout
%36 = call i32 @fflush(i32 %35)
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%38 = load i32, i32* @stdin
%39 = call i32 @fgets(i8* %37, i32 32, i32 %38)
%40 = load i32*, i32** %4
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @fseek(i32* %40, i32 0, i32 %41)
%43 = load i32*, i32** %4
%44 = load i32, i32* %6
%45 = call i32 @evemu_play(i32* %43, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %33
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %52
51: ; preds = %33
br label %33
52: ; preds = %48
br label %53
53: ; preds = %52, %31
%54 = load %struct.evemu_device*, %struct.evemu_device** %5
%55 = call i32 @evemu_delete(%struct.evemu_device* %54)
%56 = load i32*, i32** %4
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
store i32 0, i32* %2
br label %60
60: ; preds = %53, %21, %13
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
%struct.evemu_device = type { i32 }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"error: could not open file (%m)\0A\00"
@.str.2 = [36 x i8] c"error: could not create device: %m\0A\00"
@.str.3 = [29 x i8] c"Hit enter to start replaying\00"
@stdout = global i32 0
@stdin = global i32 0
@SEEK_SET = global i32 0
@.str.4 = [32 x i8] c"error: could not replay device\0A\00"
define i32 @play_from_file(i32 %0) {
%2 = alloca [32 x i8]
%3 = tail call i32* @fdopen(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %38
8: ; preds = %1
%9 = tail call %struct.evemu_device* @create_device(i32* %3)
%10 = icmp eq %struct.evemu_device* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @fclose(i32* %3)
br label %38
15: ; preds = %8
%16 = tail call i32 @open_evemu_device(%struct.evemu_device* %9)
%17 = icmp slt i32 %16, 0
br i1 %17, label %34, label %18
18: ; preds = %15
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
br label %20
20: ; preds = %18, %33
%21 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %19, i32 32, i32 %24)
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %3, i32 0, i32 %26)
%28 = call i32 @evemu_play(i32* %3, i32 %16)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %20
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %34
33: ; preds = %20
br label %20
34: ; preds = %30, %15
%35 = call i32 @evemu_delete(%struct.evemu_device* %9)
%36 = call i32 @fclose(i32* %3)
%37 = call i32 @close(i32 %16)
br label %38
38: ; preds = %34, %11, %5
%39 = phi i32 [ 0, %34 ], [ -1, %11 ], [ -1, %5 ]
ret i32 %39
}