#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 stat {
  scalar_t__ st_rdev;
};

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ S_IFBLK;
int /*<<< orphan*/ __ploop_err(int /*<<< orphan*/, char *, char const *);
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ chmod(char *, int);
int /*<<< orphan*/ errno;
int has_partition(char const *, int *);
scalar_t__ mknod(char *, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
scalar_t__ stat(char const *, struct stat *);
scalar_t__ strncmp(char const *, char *, int);
int get_partition_device_name(const char *device, char *out, int size) {
  int ret, part;
  const char *p;
  struct stat st;
  ret = has_partition(device, &part);
  if (ret)
    return ret;
  if (part) {
    p = device;
    if (strncmp(device, "/dev/", 5) == 0)
      p += 5;
    snprintf(out, size, "/dev/%sp1", p);
    if (access(out, F_OK) == 0)
      return 0;
    if (stat(device, &st)) {
      __ploop_err(errno, "failed stat %s", device);
      return -1;
    }
    if (mknod(out, S_IFBLK, st.st_rdev + 1) != 0) {
      __ploop_err(errno, "failed mknod %s", out);
      return -1;
    }
    if (chmod(out, 0600)) {
      __ploop_err(errno, "failed chmod %s", out);
      return -1;
    }
  } else
    snprintf(out, size, "%s", device);
  return 0;
}


x86 assembly (clang, -O0):
<get_partition_device_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1090 <has_partition@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11b8 <get_partition_device_name+0x38>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1319 <get_partition_device_name+0x199>
cmpl   $0x0,-0x24(%rbp)
je     12fb <get_partition_device_name+0x17b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2b(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11f5 <get_partition_device_name+0x75>
mov    -0x30(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
lea    0xdff(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    1234 <get_partition_device_name+0xb4>
movl   $0x0,-0x4(%rbp)
jmp    1319 <get_partition_device_name+0x199>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1270 <get_partition_device_name+0xf0>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb1(%rip),%rsi
call   10a0 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1319 <get_partition_device_name+0x199>
mov    -0x18(%rbp),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rdx
add    $0x1,%rdx
call   1060 <mknod@plt>
cmp    $0x0,%rax
je     12b9 <get_partition_device_name+0x139>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd77(%rip),%rsi
call   10a0 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1319 <get_partition_device_name+0x199>
mov    -0x18(%rbp),%rdi
mov    $0x180,%esi
call   1070 <chmod@plt>
cmp    $0x0,%rax
je     12f6 <get_partition_device_name+0x176>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd4a(%rip),%rsi
call   10a0 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1319 <get_partition_device_name+0x199>
jmp    1312 <get_partition_device_name+0x192>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xd2f(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_partition_device_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
call   1090 <has_partition@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11b0 <get_partition_device_name+0x30>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmpl   $0x0,0xc(%rsp)
je     122b <get_partition_device_name+0xab>
lea    0xe42(%rip),%rsi
mov    %rbx,%rdi
mov    $0x5,%edx
call   1030 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,4),%rcx
add    %rbx,%rcx
lea    0xe25(%rip),%rdx
mov    %r14,%rdi
mov    %r15d,%esi
call   1040 <snprintf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <access@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11a3 <get_partition_device_name+0x23>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     1247 <get_partition_device_name+0xc7>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
mov    %rbx,%rdx
jmp    12a1 <get_partition_device_name+0x121>
lea    0xe0a(%rip),%rdx
mov    %r14,%rdi
mov    %r15d,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
xor    %ebp,%ebp
jmp    11a3 <get_partition_device_name+0x23>
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x10(%rsp),%rdx
add    $0x1,%rdx
mov    %r14,%rdi
call   1060 <mknod@plt>
test   %rax,%rax
je     1278 <get_partition_device_name+0xf8>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
jmp    129e <get_partition_device_name+0x11e>
mov    %r14,%rdi
mov    $0x180,%esi
call   1070 <chmod@plt>
test   %rax,%rax
je     11a3 <get_partition_device_name+0x23>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
mov    %r14,%rdx
call   10a0 <__ploop_err@plt>
mov    $0xffffffff,%ebp
jmp    11a3 <get_partition_device_name+0x23>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [6 x i8] c"/dev/\00"
@.str.1 = [10 x i8] c"/dev/%sp1\00"
@F_OK = global i32 0
@errno = global i32 0
@.str.2 = [15 x i8] c"failed stat %s\00"
@S_IFBLK = global i32 0
@.str.3 = [16 x i8] c"failed mknod %s\00"
@.str.4 = [16 x i8] c"failed chmod %s\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @get_partition_device_name(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = call i32 @has_partition(i8* %12, i32* %9)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* %8
store i32 %17, i32* %4
br label %74
18: ; preds = %3
%19 = load i32, i32* %9
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %68
21: ; preds = %18
%22 = load i8*, i8** %5
store i8* %22, i8** %10
%23 = load i8*, i8** %5
%24 = call i64 @strncmp(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i8*, i8** %10
%28 = getelementptr inbounds i8, i8* %27, i64 5
store i8* %28, i8** %10
br label %29
29: ; preds = %26, %21
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = load i8*, i8** %10
%33 = call i32 @snprintf(i8* %30, i32 %31, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = load i8*, i8** %6
%35 = load i32, i32* @F_OK
%36 = call i64 @access(i8* %34, i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %29
store i32 0, i32* %4
br label %74
39: ; preds = %29
%40 = load i8*, i8** %5
%41 = call i64 @stat(i8* %40, %struct.stat* %11)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* @errno
%45 = load i8*, i8** %5
%46 = call i32 @__ploop_err(i32 %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %45)
store i32 -1, i32* %4
br label %74
47: ; preds = %39
%48 = load i8*, i8** %6
%49 = load i32, i32* @S_IFBLK
%50 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%51 = load i64, i64* %50
%52 = add nsw i64 %51, 1
%53 = call i64 @mknod(i8* %48, i32 %49, i64 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %47
%56 = load i32, i32* @errno
%57 = load i8*, i8** %6
%58 = call i32 @__ploop_err(i32 %56, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %57)
store i32 -1, i32* %4
br label %74
59: ; preds = %47
%60 = load i8*, i8** %6
%61 = call i64 @chmod(i8* %60, i32 384)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = load i32, i32* @errno
%65 = load i8*, i8** %6
%66 = call i32 @__ploop_err(i32 %64, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %65)
store i32 -1, i32* %4
br label %74
67: ; preds = %59
br label %73
68: ; preds = %18
%69 = load i8*, i8** %6
%70 = load i32, i32* %7
%71 = load i8*, i8** %5
%72 = call i32 @snprintf(i8* %69, i32 %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %71)
br label %73
73: ; preds = %68, %67
store i32 0, i32* %4
br label %74
74: ; preds = %73, %63, %55, %43, %38, %16
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [6 x i8] c"/dev/\00"
@.str.1 = [10 x i8] c"/dev/%sp1\00"
@F_OK = global i32 0
@errno = global i32 0
@.str.2 = [15 x i8] c"failed stat %s\00"
@S_IFBLK = global i32 0
@.str.3 = [16 x i8] c"failed mknod %s\00"
@.str.4 = [16 x i8] c"failed chmod %s\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @get_partition_device_name(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.stat
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.stat* %5 to i8*
%8 = call i32 @has_partition(i8* %0, i32* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %46
10: ; preds = %3
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 0
br i1 %12, label %44, label %13
13: ; preds = %10
%14 = call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i64 5, i64 0
%17 = getelementptr i8, i8* %0, i64 %16
%18 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @F_OK
%20 = call i64 @access(i8* %1, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %46, label %22
22: ; preds = %13
%23 = call i64 @stat(i8* %0, %struct.stat* %5)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = call i32 @__ploop_err(i32 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %46
28: ; preds = %22
%29 = load i32, i32* @S_IFBLK
%30 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%31 = load i64, i64* %30
%32 = add nsw i64 %31, 1
%33 = call i64 @mknod(i8* %1, i32 %29, i64 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %28
%36 = load i32, i32* @errno
%37 = call i32 @__ploop_err(i32 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %46
38: ; preds = %28
%39 = call i64 @chmod(i8* %1, i32 384)
%40 = icmp eq i64 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %38
%42 = load i32, i32* @errno
%43 = call i32 @__ploop_err(i32 %42, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %46
44: ; preds = %10
%45 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %46
46: ; preds = %44, %38, %13, %3, %41, %35, %25
%47 = phi i32 [ -1, %25 ], [ -1, %35 ], [ -1, %41 ], [ %8, %3 ], [ 0, %13 ], [ 0, %38 ], [ 0, %44 ]
ret i32 %47
}