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

/* Variables and functions */
int ENOMEM;
int errno;
int evemu_create_managed(struct evemu_device *);
int /*<<< orphan*/ evemu_destroy(struct evemu_device *);
int /*<<< orphan*/ evemu_get_name(struct evemu_device *);
struct evemu_device *evemu_new(int /*<<< orphan*/ *);
int evemu_read(struct evemu_device *, int /*<<< orphan*/ *);
int /*<<< orphan*/ evemu_set_name(struct evemu_device *, char *);
int getpid();
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strlen(int /*<<< orphan*/);
struct evemu_device *create_device(FILE *fp) {
  struct evemu_device *dev;
  int ret = -ENOMEM;
  int saved_errno;
  dev = evemu_new(NULL);
  if (!dev)
    goto out;
  ret = evemu_read(dev, fp);
  if (ret <= 0)
    goto out;
  if (strlen(evemu_get_name(dev)) == 0) {
    char name[64];
    sprintf(name, "evemu-%d", getpid());
    evemu_set_name(dev, name);
  }
  ret = evemu_create_managed(dev);
  if (ret < 0)
    goto out;
out:
  if (ret && dev) {
    saved_errno = errno;
    evemu_destroy(dev);
    dev = NULL;
    errno = saved_errno;
  }
  return dev;
}


x86 assembly (clang, -O0):
<create_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
mov    0x2e3b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <evemu_new@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c7 <create_device+0x37>
jmp    124e <create_device+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <evemu_read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11e6 <create_device+0x56>
jmp    124e <create_device+0xbe>
mov    -0x10(%rbp),%rdi
call   10a0 <evemu_get_name@plt>
mov    %eax,%edi
call   1080 <strlen@plt>
cmp    $0x0,%rax
jne    122e <create_device+0x9e>
lea    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%edx
lea    0xde4(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1070 <evemu_set_name@plt>
mov    -0x10(%rbp),%rdi
call   1090 <evemu_create_managed@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1249 <create_device+0xb9>
jmp    124e <create_device+0xbe>
jmp    124e <create_device+0xbe>
cmpl   $0x0,-0x14(%rbp)
je     128c <create_device+0xfc>
cmpq   $0x0,-0x10(%rbp)
je     128c <create_device+0xfc>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <evemu_destroy@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    0x2d4e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_device>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <evemu_new@plt>
test   %rax,%rax
je     121d <create_device+0x8d>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <evemu_read@plt>
test   %eax,%eax
jle    11ff <create_device+0x6f>
mov    %r14,%rdi
call   10a0 <evemu_get_name@plt>
mov    %eax,%edi
call   1080 <strlen@plt>
test   %rax,%rax
jne    11f7 <create_device+0x67>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe21(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <evemu_set_name@plt>
mov    %r14,%rdi
call   1090 <evemu_create_managed@plt>
test   %eax,%eax
je     121a <create_device+0x8a>
mov    0x2dd6(%rip),%rbx
mov    (%rbx),%ebp
mov    %r14,%rdi
call   1050 <evemu_destroy@plt>
mov    %ebp,(%rbx)
xor    %ebx,%ebx
jmp    121d <create_device+0x8d>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.evemu_device = type { i32 }
@ENOMEM = global i32 0
@.str = [9 x i8] c"evemu-%d\00"
@errno = global i32 0
define %struct.evemu_device* @create_device(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.evemu_device*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
store i32* %0, i32** %2
%7 = load i32, i32* @ENOMEM
%8 = sub nsw i32 0, %7
store i32 %8, i32* %4
%9 = call %struct.evemu_device* @evemu_new(i32* null)
store %struct.evemu_device* %9, %struct.evemu_device** %3
%10 = load %struct.evemu_device*, %struct.evemu_device** %3
%11 = icmp ne %struct.evemu_device* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
br label %39
13: ; preds = %1
%14 = load %struct.evemu_device*, %struct.evemu_device** %3
%15 = load i32*, i32** %2
%16 = call i32 @evemu_read(%struct.evemu_device* %14, i32* %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %13
br label %39
20: ; preds = %13
%21 = load %struct.evemu_device*, %struct.evemu_device** %3
%22 = call i32 @evemu_get_name(%struct.evemu_device* %21)
%23 = call i64 @strlen(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %20
%26 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%27 = call i32 (...) @getpid()
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = load %struct.evemu_device*, %struct.evemu_device** %3
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%31 = call i32 @evemu_set_name(%struct.evemu_device* %29, i8* %30)
br label %32
32: ; preds = %25, %20
%33 = load %struct.evemu_device*, %struct.evemu_device** %3
%34 = call i32 @evemu_create_managed(%struct.evemu_device* %33)
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %32
br label %39
38: ; preds = %32
br label %39
39: ; preds = %38, %37, %19, %12
%40 = load i32, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load %struct.evemu_device*, %struct.evemu_device** %3
%44 = icmp ne %struct.evemu_device* %43, null
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i32, i32* @errno
store i32 %46, i32* %5
%47 = load %struct.evemu_device*, %struct.evemu_device** %3
%48 = call i32 @evemu_destroy(%struct.evemu_device* %47)
store %struct.evemu_device* null, %struct.evemu_device** %3
%49 = load i32, i32* %5
store i32 %49, i32* @errno
br label %50
50: ; preds = %45, %42, %39
%51 = load %struct.evemu_device*, %struct.evemu_device** %3
ret %struct.evemu_device* %51
}

LLVM IR (-O2):
%struct.evemu_device = type { i32 }
@ENOMEM = global i32 0
@.str = [9 x i8] c"evemu-%d\00"
@errno = global i32 0
define %struct.evemu_device* @create_device(i32* %0) {
%2 = alloca [64 x i8]
%3 = tail call %struct.evemu_device* @evemu_new(i32* null)
%4 = icmp eq %struct.evemu_device* %3, null
br i1 %4, label %25, label %5
5: ; preds = %1
%6 = tail call i32 @evemu_read(%struct.evemu_device* %3, i32* %0)
%7 = icmp slt i32 %6, 1
br i1 %7, label %19, label %8
8: ; preds = %5
%9 = tail call i32 @evemu_get_name(%struct.evemu_device* %3)
%10 = tail call i64 @strlen(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%14 = tail call i32 (...) @getpid()
%15 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @evemu_set_name(%struct.evemu_device* %3, i8* %13)
br label %17
17: ; preds = %12, %8
%18 = call i32 @evemu_create_managed(%struct.evemu_device* %3)
br label %19
19: ; preds = %17, %5
%20 = phi i32 [ %6, %5 ], [ %18, %17 ]
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = call i32 @evemu_destroy(%struct.evemu_device* %3)
store i32 %23, i32* @errno
br label %25
25: ; preds = %1, %22, %19
%26 = phi %struct.evemu_device* [ null, %22 ], [ %3, %19 ], [ null, %1 ]
ret %struct.evemu_device* %26
}