#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 */
scalar_t__ EEXIST;
int S_IFBLK;
scalar_t__ asprintf(char **, char *, char const *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int /*<<< orphan*/ logMessage(int, char *, char const *, ...);
int /*<<< orphan*/ makedev(int, int);
scalar_t__ mknod(char *, int, int /*<<< orphan*/);
int createDevNode(const char *dname, const char *devname) {
  char *dnode;
  FILE *f;
  int major, minor;
  f = fopen(dname, "r");
  logMessage(10, "Trying to get device info %s from %s.", devname, dname);
  if (!f) {
    logMessage(40, "Could not open %s.", dname);
    return -1;
  }
  if (fscanf(f, "%i:%i", &major, &minor) < 2) {
    logMessage(40, "Could not get major/minor from %s.", devname);
    fclose(f);
    return -2;
  }
  fclose(f);
  if (asprintf(&dnode, "/dev/%s", devname) <= 0) {
    logMessage(40, "Cannot allocate memory for device node (%s).", devname);
    return -3;
  }
  if (mknod(dnode, 0600 | S_IFBLK, makedev(major, minor)) < 0) {
    if (errno != EEXIST) {
      logMessage(40, "Failed to create device node (%s %i:%i).", dnode, major,
                 minor);
    }
    free(dnode);
    return -4;
  } else {
    logMessage(10, "Device node (%s %i:%i) created.", dnode, major, minor);
    free(dnode);
    return 0;
  }
}


x86 assembly (clang, -O0):
<createDevNode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0xa,%edi
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1030 <logMessage@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11ed <createDevNode+0x6d>
mov    -0x10(%rbp),%rdx
mov    $0x28,%edi
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1030 <logMessage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <createDevNode+0x1bb>
mov    -0x28(%rbp),%rdi
lea    0xe43(%rip),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <fscanf@plt>
cmp    $0x2,%eax
jge    123a <createDevNode+0xba>
mov    -0x18(%rbp),%rdx
mov    $0x28,%edi
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1030 <logMessage@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    133b <createDevNode+0x1bb>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1080 <asprintf@plt>
cmp    $0x0,%rax
jg     1284 <createDevNode+0x104>
mov    -0x18(%rbp),%rdx
mov    $0x28,%edi
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1030 <logMessage@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    133b <createDevNode+0x1bb>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
or     $0x180,%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
call   10a0 <makedev@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   1070 <mknod@plt>
cmp    $0x0,%rax
jge    130d <createDevNode+0x18d>
mov    0x2d09(%rip),%rax
mov    (%rax),%rax
mov    0x2d07(%rip),%rcx
cmp    (%rcx),%rax
je     12f8 <createDevNode+0x178>
mov    -0x20(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
mov    $0x28,%edi
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1030 <logMessage@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    133b <createDevNode+0x1bb>
mov    -0x20(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
mov    $0xa,%edi
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1030 <logMessage@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createDevNode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
lea    0xe5c(%rip),%rsi
mov    $0xa,%edi
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <logMessage@plt>
test   %r15,%r15
je     1203 <createDevNode+0x83>
lea    0xe77(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jg     1223 <createDevNode+0xa3>
lea    0xe5f(%rip),%rsi
mov    $0x28,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <logMessage@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    $0xfffffffe,%ebx
jmp    1307 <createDevNode+0x187>
lea    0xe1e(%rip),%rsi
mov    $0x28,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <logMessage@plt>
mov    $0xffffffff,%ebx
jmp    1307 <createDevNode+0x187>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xe32(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r14,%rdx
call   1080 <asprintf@plt>
test   %rax,%rax
jle    12a4 <createDevNode+0x124>
mov    0x10(%rsp),%rbx
mov    0x2d90(%rip),%rax
mov    $0x180,%ebp
or     (%rax),%ebp
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
call   10a0 <makedev@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <mknod@plt>
test   %rax,%rax
js     12c1 <createDevNode+0x141>
mov    0x10(%rsp),%rdx
mov    0xc(%rsp),%ecx
mov    0x8(%rsp),%r8d
lea    0xe38(%rip),%rsi
xor    %ebx,%ebx
mov    $0xa,%edi
xor    %eax,%eax
call   1030 <logMessage@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
jmp    1307 <createDevNode+0x187>
lea    0xdc1(%rip),%rsi
mov    $0x28,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <logMessage@plt>
mov    $0xfffffffd,%ebx
jmp    1307 <createDevNode+0x187>
mov    0x2d08(%rip),%rax
mov    (%rax),%rax
mov    0x2d06(%rip),%rcx
cmp    (%rcx),%rax
je     12f8 <createDevNode+0x178>
mov    0x10(%rsp),%rdx
mov    0xc(%rsp),%ecx
mov    0x8(%rsp),%r8d
lea    0xdad(%rip),%rsi
mov    $0x28,%edi
xor    %eax,%eax
call   1030 <logMessage@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    $0xfffffffc,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [38 x i8] c"Trying to get device info %s from %s.\00"
@.str.2 = [19 x i8] c"Could not open %s.\00"
@.str.3 = [6 x i8] c"%i:%i\00"
@.str.4 = [35 x i8] c"Could not get major/minor from %s.\00"
@.str.5 = [8 x i8] c"/dev/%s\00"
@.str.6 = [45 x i8] c"Cannot allocate memory for device node (%s).\00"
@S_IFBLK = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.7 = [41 x i8] c"Failed to create device node (%s %i:%i).\00"
@.str.8 = [32 x i8] c"Device node (%s %i:%i) created.\00"
define i32 @createDevNode(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i8*, i8** %5
%13 = load i8*, i8** %4
%14 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %13)
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 40, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %66
20: ; preds = %2
%21 = load i32*, i32** %7
%22 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %8, i32* %9)
%23 = icmp slt i32 %22, 2
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 40, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %7
%28 = call i32 @fclose(i32* %27)
store i32 -2, i32* %3
br label %66
29: ; preds = %20
%30 = load i32*, i32** %7
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %5
%33 = call i64 @asprintf(i8** %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %32)
%34 = icmp sle i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i8*, i8** %5
%37 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 40, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %36)
store i32 -3, i32* %3
br label %66
38: ; preds = %29
%39 = load i8*, i8** %6
%40 = load i32, i32* @S_IFBLK
%41 = or i32 384, %40
%42 = load i32, i32* %8
%43 = load i32, i32* %9
%44 = call i32 @makedev(i32 %42, i32 %43)
%45 = call i64 @mknod(i8* %39, i32 %41, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %59
47: ; preds = %38
%48 = load i64, i64* @errno
%49 = load i64, i64* @EEXIST
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i8*, i8** %6
%53 = load i32, i32* %8
%54 = load i32, i32* %9
%55 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 40, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i8* %52, i32 %53, i32 %54)
br label %56
56: ; preds = %51, %47
%57 = load i8*, i8** %6
%58 = call i32 @free(i8* %57)
store i32 -4, i32* %3
br label %66
59: ; preds = %38
%60 = load i8*, i8** %6
%61 = load i32, i32* %8
%62 = load i32, i32* %9
%63 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i8* %60, i32 %61, i32 %62)
%64 = load i8*, i8** %6
%65 = call i32 @free(i8* %64)
store i32 0, i32* %3
br label %66
66: ; preds = %59, %56, %35, %24, %17
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [38 x i8] c"Trying to get device info %s from %s.\00"
@.str.2 = [19 x i8] c"Could not open %s.\00"
@.str.3 = [6 x i8] c"%i:%i\00"
@.str.4 = [35 x i8] c"Could not get major/minor from %s.\00"
@.str.5 = [8 x i8] c"/dev/%s\00"
@.str.6 = [45 x i8] c"Cannot allocate memory for device node (%s).\00"
@S_IFBLK = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.7 = [41 x i8] c"Failed to create device node (%s %i:%i).\00"
@.str.8 = [32 x i8] c"Device node (%s %i:%i) created.\00"
define i32 @createDevNode(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i8** %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 (i32, i8*, i8*, ...) @logMessage(i32 10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
%11 = icmp eq i32* %9, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 (i32, i8*, i8*, ...) @logMessage(i32 40, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %54
14: ; preds = %2
%15 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %4, i32* %5)
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 40, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%19 = call i32 @fclose(i32* %9)
br label %54
20: ; preds = %14
%21 = call i32 @fclose(i32* %9)
%22 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %1)
%23 = icmp slt i64 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 40, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %1)
br label %54
26: ; preds = %20
%27 = load i8*, i8** %3
%28 = load i32, i32* @S_IFBLK
%29 = or i32 %28, 384
%30 = load i32, i32* %4
%31 = load i32, i32* %5
%32 = call i32 @makedev(i32 %30, i32 %31)
%33 = call i64 @mknod(i8* %27, i32 %29, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %26
%36 = load i64, i64* @errno
%37 = load i64, i64* @EEXIST
%38 = icmp eq i64 %36, %37
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = load i8*, i8** %3
%41 = load i32, i32* %4
%42 = load i32, i32* %5
%43 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 40, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i8* %40, i32 %41, i32 %42)
br label %44
44: ; preds = %39, %35
%45 = load i8*, i8** %3
%46 = call i32 @free(i8* %45)
br label %54
47: ; preds = %26
%48 = load i8*, i8** %3
%49 = load i32, i32* %4
%50 = load i32, i32* %5
%51 = call i32 (i32, i8*, i8*, ...) @logMessage(i32 10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i8* %48, i32 %49, i32 %50)
%52 = load i8*, i8** %3
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %47, %44, %24, %17, %12
%55 = phi i32 [ -2, %17 ], [ -3, %24 ], [ -4, %44 ], [ 0, %47 ], [ -1, %12 ]
ret i32 %55
}