#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*/ path;
typedef scalar_t__ dev_t;

/* Variables and functions */
char *bdevname(scalar_t__);
int /*<<< orphan*/ close(int);
scalar_t__ makedev(unsigned int, unsigned int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int, char *);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
void *strtoul(char *, char **, int);
dev_t try_name(char *name, int part) {
  char path[65536];
  char buf[65536];
  int range;
  unsigned int major, minor;
  dev_t res;
  char *s;
  int len;
  int fd;
  snprintf(path, sizeof(path), "/sys/block/%s/dev", name);
  fd = open(path, 0, 0);
  if (fd < 0)
    goto fail;
  len = read(fd, buf, 65536);
  close(fd);
  if (len <= 0 || len == 65536 || buf[len - 1] != '\n')
    goto fail;
  buf[len - 1] = '\0';
  major = strtoul(buf, &s, 10);
  if (*s != ':')
    goto fail;
  minor = strtoul(s + 1, &s, 10);
  if (*s)
    goto fail;
  res = makedev(major, minor);
  if (!part)
    return res;
  snprintf(path, sizeof(path), "/sys/block/%s/range", name);
  fd = open(path, 0, 0);
  if (fd < 0)
    goto fail;
  len = read(fd, buf, 32);
  close(fd);
  if (len <= 0 || len == 32 || buf[len - 1] != '\n')
    goto fail;
  buf[len - 1] = '\0';
  range = strtoul(buf, &s, 10);
  if (*s)
    goto fail;
  if (part < range) {
    printf("kinit: try_name %s,%d = %s\n", name, part, bdevname(res + part));
    return res + part;
  }
fail:
  return (dev_t)0;
}


x86 assembly (clang, -O0):
<try_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20060,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x10020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x10000,%esi
lea    0xe57(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x10020(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <open@plt>
mov    %eax,-0x20048(%rbp)
cmpl   $0x0,-0x20048(%rbp)
jge    11d6 <try_name+0x56>
jmp    1449 <try_name+0x2c9>
mov    -0x20048(%rbp),%edi
lea    -0x20020(%rbp),%rsi
mov    $0x10000,%edx
call   1070 <read@plt>
mov    %eax,-0x20044(%rbp)
mov    -0x20048(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x20044(%rbp)
jle    1237 <try_name+0xb7>
cmpl   $0x10000,-0x20044(%rbp)
je     1237 <try_name+0xb7>
mov    -0x20044(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x20020(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     123c <try_name+0xbc>
jmp    1449 <try_name+0x2c9>
mov    -0x20044(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x20020(%rbp,%rax,1)
lea    -0x20020(%rbp),%rdi
lea    -0x20040(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    %eax,-0x20028(%rbp)
mov    -0x20040(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
je     1285 <try_name+0x105>
jmp    1449 <try_name+0x2c9>
mov    -0x20040(%rbp),%rdi
add    $0x1,%rdi
lea    -0x20040(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    %eax,-0x2002c(%rbp)
mov    -0x20040(%rbp),%rax
cmpb   $0x0,(%rax)
je     12bc <try_name+0x13c>
jmp    1449 <try_name+0x2c9>
mov    -0x20028(%rbp),%edi
mov    -0x2002c(%rbp),%esi
call   1090 <makedev@plt>
mov    %rax,-0x20038(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    12ee <try_name+0x16e>
mov    -0x20038(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1451 <try_name+0x2d1>
lea    -0x10020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x10000,%esi
lea    0xd0d(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x10020(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <open@plt>
mov    %eax,-0x20048(%rbp)
cmpl   $0x0,-0x20048(%rbp)
jge    1332 <try_name+0x1b2>
jmp    1449 <try_name+0x2c9>
mov    -0x20048(%rbp),%edi
lea    -0x20020(%rbp),%rsi
mov    $0x20,%edx
call   1070 <read@plt>
mov    %eax,-0x20044(%rbp)
mov    -0x20048(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x20044(%rbp)
jle    1390 <try_name+0x210>
cmpl   $0x20,-0x20044(%rbp)
je     1390 <try_name+0x210>
mov    -0x20044(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x20020(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     1395 <try_name+0x215>
jmp    1449 <try_name+0x2c9>
mov    -0x20044(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x20020(%rbp,%rax,1)
lea    -0x20020(%rbp),%rdi
lea    -0x20040(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    %eax,-0x20024(%rbp)
mov    -0x20040(%rbp),%rax
cmpb   $0x0,(%rax)
je     13db <try_name+0x25b>
jmp    1449 <try_name+0x2c9>
mov    -0x14(%rbp),%eax
cmp    -0x20024(%rbp),%eax
jge    1444 <try_name+0x2c4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20058(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2004c(%rbp)
mov    -0x20038(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
call   1030 <bdevname@plt>
mov    -0x20058(%rbp),%rsi
mov    -0x2004c(%rbp),%edx
mov    %rax,%rcx
lea    0xbfe(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20038(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1451 <try_name+0x2d1>
jmp    1449 <try_name+0x2c9>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20018,%rsp
mov    %esi,%r15d
mov    %rdi,%r12
lea    0xe62(%rip),%rdx
lea    0x10010(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x10000,%esi
mov    %r12,%rcx
call   1050 <snprintf@plt>
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1244 <try_name+0xc4>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x10000,%edx
call   1070 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
xor    %r13d,%r13d
test   %ebx,%ebx
jle    1244 <try_name+0xc4>
cmp    $0x10000,%ebx
je     1244 <try_name+0xc4>
add    $0xffffffff,%ebx
cmpb   $0xa,0x10(%rsp,%rbx,1)
jne    1241 <try_name+0xc1>
movb   $0x0,0x10(%rsp,%rbx,1)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    0x8(%rsp),%rdi
cmpb   $0x3a,(%rdi)
jne    1241 <try_name+0xc1>
mov    %rax,%rbp
add    $0x1,%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1259 <try_name+0xd9>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x20018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
mov    %eax,%esi
call   1090 <makedev@plt>
mov    %rax,%r14
test   %r15d,%r15d
je     1330 <try_name+0x1b0>
lea    0xd9d(%rip),%rdx
lea    0x10010(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x10000,%esi
mov    %r12,%rcx
call   1050 <snprintf@plt>
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1244 <try_name+0xc4>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1070 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
xor    %r13d,%r13d
test   %ebx,%ebx
jle    1244 <try_name+0xc4>
cmp    $0x20,%ebx
je     1244 <try_name+0xc4>
add    $0xffffffff,%ebx
cmpb   $0xa,0x10(%rsp,%rbx,1)
jne    1241 <try_name+0xc1>
movb   $0x0,0x10(%rsp,%rbx,1)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    0x8(%rsp),%rcx
xor    %r13d,%r13d
cmpb   $0x0,(%rcx)
jne    1244 <try_name+0xc4>
cmp    %r15d,%eax
jle    1244 <try_name+0xc4>
movslq %r15d,%rax
add    %rax,%r14
mov    %r14,%rdi
call   1030 <bdevname@plt>
lea    0xd04(%rip),%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %rax,%rcx
call   1040 <printf@plt>
mov    %r14,%r13
jmp    1244 <try_name+0xc4>

LLVM IR (-O0):
@.str = [18 x i8] c"/sys/block/%s/dev\00"
@.str.1 = [20 x i8] c"/sys/block/%s/range\00"
@.str.2 = [28 x i8] c"kinit: try_name %s,%d = %s\0A\00"
define i64 @try_name(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [65536 x i8]
%7 = alloca [65536 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%16 = load i8*, i8** %4
%17 = call i32 @snprintf(i8* %15, i32 65536, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%19 = call i32 @open(i8* %18, i32 0, i32 0)
store i32 %19, i32* %14
%20 = load i32, i32* %14
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %2
br label %132
23: ; preds = %2
%24 = load i32, i32* %14
%25 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 65536)
store i32 %26, i32* %13
%27 = load i32, i32* %14
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %13
%30 = icmp sle i32 %29, 0
br i1 %30, label %42, label %31
31: ; preds = %23
%32 = load i32, i32* %13
%33 = icmp eq i32 %32, 65536
br i1 %33, label %42, label %34
34: ; preds = %31
%35 = load i32, i32* %13
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 10
br i1 %41, label %42, label %43
42: ; preds = %34, %31, %23
br label %132
43: ; preds = %34
%44 = load i32, i32* %13
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 %46
store i8 0, i8* %47
%48 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 0
%49 = call i8* @strtoul(i8* %48, i8** %12, i32 10)
%50 = ptrtoint i8* %49 to i32
store i32 %50, i32* %9
%51 = load i8*, i8** %12
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp ne i32 %53, 58
br i1 %54, label %55, label %56
55: ; preds = %43
br label %132
56: ; preds = %43
%57 = load i8*, i8** %12
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = call i8* @strtoul(i8* %58, i8** %12, i32 10)
%60 = ptrtoint i8* %59 to i32
store i32 %60, i32* %10
%61 = load i8*, i8** %12
%62 = load i8, i8* %61
%63 = icmp ne i8 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %56
br label %132
65: ; preds = %56
%66 = load i32, i32* %9
%67 = load i32, i32* %10
%68 = call i64 @makedev(i32 %66, i32 %67)
store i64 %68, i64* %11
%69 = load i32, i32* %5
%70 = icmp ne i32 %69, 0
br i1 %70, label %73, label %71
71: ; preds = %65
%72 = load i64, i64* %11
store i64 %72, i64* %3
br label %133
73: ; preds = %65
%74 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%75 = load i8*, i8** %4
%76 = call i32 @snprintf(i8* %74, i32 65536, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %75)
%77 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%78 = call i32 @open(i8* %77, i32 0, i32 0)
store i32 %78, i32* %14
%79 = load i32, i32* %14
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %73
br label %132
82: ; preds = %73
%83 = load i32, i32* %14
%84 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 0
%85 = call i32 @read(i32 %83, i8* %84, i32 32)
store i32 %85, i32* %13
%86 = load i32, i32* %14
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* %13
%89 = icmp sle i32 %88, 0
br i1 %89, label %101, label %90
90: ; preds = %82
%91 = load i32, i32* %13
%92 = icmp eq i32 %91, 32
br i1 %92, label %101, label %93
93: ; preds = %90
%94 = load i32, i32* %13
%95 = sub nsw i32 %94, 1
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 %96
%98 = load i8, i8* %97
%99 = sext i8 %98 to i32
%100 = icmp ne i32 %99, 10
br i1 %100, label %101, label %102
101: ; preds = %93, %90, %82
br label %132
102: ; preds = %93
%103 = load i32, i32* %13
%104 = sub nsw i32 %103, 1
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 %105
store i8 0, i8* %106
%107 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 0
%108 = call i8* @strtoul(i8* %107, i8** %12, i32 10)
%109 = ptrtoint i8* %108 to i32
store i32 %109, i32* %8
%110 = load i8*, i8** %12
%111 = load i8, i8* %110
%112 = icmp ne i8 %111, 0
br i1 %112, label %113, label %114
113: ; preds = %102
br label %132
114: ; preds = %102
%115 = load i32, i32* %5
%116 = load i32, i32* %8
%117 = icmp slt i32 %115, %116
br i1 %117, label %118, label %131
118: ; preds = %114
%119 = load i8*, i8** %4
%120 = load i32, i32* %5
%121 = load i64, i64* %11
%122 = load i32, i32* %5
%123 = sext i32 %122 to i64
%124 = add nsw i64 %121, %123
%125 = call i8* @bdevname(i64 %124)
%126 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %119, i32 %120, i8* %125)
%127 = load i64, i64* %11
%128 = load i32, i32* %5
%129 = sext i32 %128 to i64
%130 = add nsw i64 %127, %129
store i64 %130, i64* %3
br label %133
131: ; preds = %114
br label %132
132: ; preds = %131, %113, %101, %81, %64, %55, %42, %22
store i64 0, i64* %3
br label %133
133: ; preds = %132, %118, %71
%134 = load i64, i64* %3
ret i64 %134
}

LLVM IR (-O2):
@.str = [18 x i8] c"/sys/block/%s/dev\00"
@.str.1 = [20 x i8] c"/sys/block/%s/range\00"
@.str.2 = [28 x i8] c"kinit: try_name %s,%d = %s\0A\00"
define i64 @try_name(i8* %0, i32 %1) {
%3 = alloca [65536 x i8]
%4 = alloca [65536 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 0
%8 = bitcast i8** %5 to i8*
%9 = call i32 @snprintf(i8* %6, i32 65536, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i32 @open(i8* %6, i32 0, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %72, label %12
12: ; preds = %2
%13 = call i32 @read(i32 %10, i8* %7, i32 65536)
%14 = call i32 @close(i32 %10)
%15 = icmp slt i32 %13, 1
%16 = icmp eq i32 %13, 65536
%17 = or i1 %15, %16
br i1 %17, label %72, label %18
18: ; preds = %12
%19 = add nsw i32 %13, -1
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %72
24: ; preds = %18
store i8 0, i8* %21
%25 = call i8* @strtoul(i8* %7, i8** %5, i32 10)
%26 = ptrtoint i8* %25 to i64
%27 = trunc i64 %26 to i32
%28 = load i8*, i8** %5
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 58
br i1 %30, label %31, label %72
31: ; preds = %24
%32 = getelementptr inbounds i8, i8* %28, i64 1
%33 = call i8* @strtoul(i8* %32, i8** %5, i32 10)
%34 = load i8*, i8** %5
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 0
br i1 %36, label %37, label %72
37: ; preds = %31
%38 = ptrtoint i8* %33 to i64
%39 = trunc i64 %38 to i32
%40 = call i64 @makedev(i32 %27, i32 %39)
%41 = icmp eq i32 %1, 0
br i1 %41, label %72, label %42
42: ; preds = %37
%43 = call i32 @snprintf(i8* %6, i32 65536, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%44 = call i32 @open(i8* %6, i32 0, i32 0)
%45 = icmp slt i32 %44, 0
br i1 %45, label %72, label %46
46: ; preds = %42
%47 = call i32 @read(i32 %44, i8* %7, i32 32)
%48 = call i32 @close(i32 %44)
%49 = icmp slt i32 %47, 1
%50 = icmp eq i32 %47, 32
%51 = or i1 %49, %50
br i1 %51, label %72, label %52
52: ; preds = %46
%53 = add nsw i32 %47, -1
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = icmp eq i8 %56, 10
br i1 %57, label %58, label %72
58: ; preds = %52
store i8 0, i8* %55
%59 = call i8* @strtoul(i8* %7, i8** %5, i32 10)
%60 = load i8*, i8** %5
%61 = load i8, i8* %60
%62 = icmp eq i8 %61, 0
%63 = ptrtoint i8* %59 to i64
%64 = trunc i64 %63 to i32
%65 = icmp sgt i32 %64, %1
%66 = select i1 %62, i1 %65, i1 false
br i1 %66, label %67, label %72
67: ; preds = %58
%68 = sext i32 %1 to i64
%69 = add nsw i64 %40, %68
%70 = call i8* @bdevname(i64 %69)
%71 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1, i8* %70)
br label %72
72: ; preds = %2, %18, %12, %24, %31, %42, %52, %46, %58, %37, %67
%73 = phi i64 [ %69, %67 ], [ %40, %37 ], [ 0, %58 ], [ 0, %46 ], [ 0, %52 ], [ 0, %42 ], [ 0, %31 ], [ 0, %24 ], [ 0, %12 ], [ 0, %18 ], [ 0, %2 ]
ret i64 %73
}