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

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ ENOENT;
int PATH_MAX;
scalar_t__ errno;
int get_id_lun(char *);
int /*<<< orphan*/ *pathcpy(char *, char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, char, ...);
int get_sd_path(int idlun, char *path, size_t len, int host_id_only) {
  char tmp[PATH_MAX];
  int i;
  int eacces;
  int idlun2;
  eacces = 0;
  if (host_id_only) {
    idlun = (idlun & 0xff) << 24;
  }
  for (i = 0; i < 256; i++) {
    if (i < 26) {
      sprintf(tmp, "/dev/sd%c", 'a' + i);
    } else {
      sprintf(tmp, "/dev/sd%c%c", 'a' + (i / 26), 'a' + (i % 26));
    }
    if ((idlun2 = get_id_lun(tmp)) == -1) {
      if (errno == EACCES) {
        eacces = 1;
      }
    } else if (idlun == idlun2) {
      if (pathcpy(path, tmp, len) == NULL) {
        return -1;
      }
      return 0;
    }
  }
  errno = eacces ? EACCES : ENOENT;
  return -1;
}


x86 assembly (clang, -O0):
<get_sd_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1191 <get_sd_path+0x61>
mov    -0x8(%rbp),%eax
and    $0xff,%eax
shl    $0x18,%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x100,-0x34(%rbp)
jge    12a8 <get_sd_path+0x178>
cmpl   $0x1a,-0x34(%rbp)
jge    11cf <get_sd_path+0x9f>
mov    -0x48(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    $0x61,%eax
lea    0xe40(%rip),%rsi
movsbl %al,%edx
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    1208 <get_sd_path+0xd8>
mov    -0x48(%rbp),%rdi
mov    -0x34(%rbp),%eax
mov    $0x1a,%ecx
cltd
idiv   %ecx
add    $0x61,%eax
mov    %al,-0x49(%rbp)
mov    -0x34(%rbp),%eax
mov    $0x1a,%ecx
cltd
idiv   %ecx
mov    -0x49(%rbp),%al
mov    %edx,%ecx
add    $0x61,%ecx
lea    0xe0c(%rip),%rsi
movsbl %al,%edx
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <get_id_lun@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    1243 <get_sd_path+0x113>
mov    0x2dac(%rip),%rax
mov    (%rax),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
jne    123e <get_sd_path+0x10e>
movl   $0x1,-0x38(%rbp)
jmp    1295 <get_sd_path+0x165>
mov    -0x8(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jne    1290 <get_sd_path+0x160>
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1040 <pathcpy@plt>
cmp    $0x0,%rax
jne    127d <get_sd_path+0x14d>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    12ef <get_sd_path+0x1bf>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    12ef <get_sd_path+0x1bf>
jmp    1295 <get_sd_path+0x165>
jmp    129a <get_sd_path+0x16a>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1198 <get_sd_path+0x68>
cmpl   $0x0,-0x38(%rbp)
je     12c5 <get_sd_path+0x195>
mov    0x2d27(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    12d3 <get_sd_path+0x1a3>
mov    0x2cf4(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rcx
mov    0x2cf2(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sd_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x38(%rbp)
mov    %rsi,-0x30(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %edi,%r15d
shl    $0x18,%r15d
test   %ecx,%ecx
cmove  %edi,%r15d
mov    0x2e69(%rip),%r14
xor    %r12d,%r12d
xor    %r13d,%r13d
jmp    11a7 <get_sd_path+0x77>
nop
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
cmp    (%r14),%rax
mov    $0x1,%eax
cmove  %eax,%r12d
add    $0x1,%r13d
cmp    $0x100,%r13d
je     1245 <get_sd_path+0x115>
cmp    $0x19,%r13d
ja     11d0 <get_sd_path+0xa0>
lea    0x61(%r13),%eax
movsbl %al,%edx
mov    %rbx,%rdi
lea    0xe42(%rip),%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
jmp    1213 <get_sd_path+0xe3>
nopw   0x0(%rax,%rax,1)
movzbl %r13b,%eax
imul   $0x4f,%eax,%eax
shr    $0xb,%eax
lea    (%rax,%rax,4),%ecx
lea    (%rcx,%rcx,4),%ecx
add    %eax,%ecx
mov    $0x60,%al
sub    %cl,%al
movzbl %al,%eax
or     $0x1,%eax
movzbl %r13b,%ecx
imul   $0x4f,%ecx,%edx
shr    $0xb,%edx
add    $0x61,%dl
add    %r13d,%eax
movzbl %al,%ecx
movzbl %dl,%edx
mov    %rbx,%rdi
lea    0xdfe(%rip),%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <get_id_lun@plt>
cmp    $0xffffffff,%eax
je     1180 <get_sd_path+0x50>
cmp    %eax,%r15d
jne    1196 <get_sd_path+0x66>
mov    -0x30(%rbp),%rdi
mov    %rbx,%rsi
mov    -0x38(%rbp),%rdx
call   1040 <pathcpy@plt>
cmp    $0x1,%rax
sbb    %eax,%eax
jmp    1263 <get_sd_path+0x133>
test   %r12d,%r12d
jne    1251 <get_sd_path+0x121>
mov    0x2d6f(%rip),%r14
mov    (%r14),%rax
mov    0x2d75(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/dev/sd%c\00"
@.str.1 = [12 x i8] c"/dev/sd%c%c\00"
@errno = global i64 0
@EACCES = global i64 0
@ENOENT = global i64 0
define i32 @get_sd_path(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %10
%19 = alloca i8, i64 %17
store i64 %17, i64* %11
store i32 0, i32* %13
%20 = load i32, i32* %9
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %4
%23 = load i32, i32* %6
%24 = and i32 %23, 255
%25 = shl i32 %24, 24
store i32 %25, i32* %6
br label %26
26: ; preds = %22, %4
store i32 0, i32* %12
br label %27
27: ; preds = %69, %26
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 256
br i1 %29, label %30, label %72
30: ; preds = %27
%31 = load i32, i32* %12
%32 = icmp slt i32 %31, 26
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* %12
%35 = add nsw i32 97, %34
%36 = trunc i32 %35 to i8
%37 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8 signext %36)
br label %47
38: ; preds = %30
%39 = load i32, i32* %12
%40 = sdiv i32 %39, 26
%41 = add nsw i32 97, %40
%42 = trunc i32 %41 to i8
%43 = load i32, i32* %12
%44 = srem i32 %43, 26
%45 = add nsw i32 97, %44
%46 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 signext %42, i32 %45)
br label %47
47: ; preds = %38, %33
%48 = call i32 @get_id_lun(i8* %19)
store i32 %48, i32* %14
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %56
50: ; preds = %47
%51 = load i64, i64* @errno
%52 = load i64, i64* @EACCES
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
store i32 1, i32* %13
br label %55
55: ; preds = %54, %50
br label %68
56: ; preds = %47
%57 = load i32, i32* %6
%58 = load i32, i32* %14
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %67
60: ; preds = %56
%61 = load i8*, i8** %7
%62 = load i64, i64* %8
%63 = call i32* @pathcpy(i8* %61, i8* %19, i64 %62)
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %66
65: ; preds = %60
store i32 -1, i32* %5
store i32 1, i32* %15
br label %81
66: ; preds = %60
store i32 0, i32* %5
store i32 1, i32* %15
br label %81
67: ; preds = %56
br label %68
68: ; preds = %67, %55
br label %69
69: ; preds = %68
%70 = load i32, i32* %12
%71 = add nsw i32 %70, 1
store i32 %71, i32* %12
br label %27
72: ; preds = %27
%73 = load i32, i32* %13
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = load i64, i64* @EACCES
br label %79
77: ; preds = %72
%78 = load i64, i64* @ENOENT
br label %79
79: ; preds = %77, %75
%80 = phi i64 [ %76, %75 ], [ %78, %77 ]
store i64 %80, i64* @errno
store i32 -1, i32* %5
store i32 1, i32* %15
br label %81
81: ; preds = %79, %66, %65
%82 = load i8*, i8** %10
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/dev/sd%c\00"
@.str.1 = [12 x i8] c"/dev/sd%c%c\00"
@errno = global i64 0
@EACCES = global i64 0
@ENOENT = global i64 0
define i32 @get_sd_path(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = icmp eq i32 %3, 0
%9 = shl i32 %0, 24
%10 = select i1 %8, i32 %0, i32 %9
br label %11
11: ; preds = %4, %40
%12 = phi i32 [ 0, %4 ], [ %41, %40 ]
%13 = phi i32 [ 0, %4 ], [ %42, %40 ]
%14 = icmp ult i32 %13, 26
%15 = trunc i32 %13 to i8
br i1 %14, label %16, label %19
16: ; preds = %11
%17 = add nuw nsw i8 %15, 97
%18 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8 signext %17)
br label %26
19: ; preds = %11
%20 = udiv i8 %15, 26
%21 = add nuw nsw i8 %20, 97
%22 = urem i8 %15, 26
%23 = add nuw nsw i8 %22, 97
%24 = zext i8 %23 to i32
%25 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 signext %21, i32 %24)
br label %26
26: ; preds = %19, %16
%27 = call i32 @get_id_lun(i8* %7)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EACCES
%32 = icmp eq i64 %30, %31
%33 = select i1 %32, i32 1, i32 %12
br label %40
34: ; preds = %26
%35 = icmp eq i32 %10, %27
br i1 %35, label %36, label %40
36: ; preds = %34
%37 = call i32* @pathcpy(i8* %1, i8* %7, i64 %2)
%38 = icmp eq i32* %37, null
%39 = sext i1 %38 to i32
br label %49
40: ; preds = %29, %34
%41 = phi i32 [ %12, %34 ], [ %33, %29 ]
%42 = add nuw nsw i32 %13, 1
%43 = icmp eq i32 %42, 256
br i1 %43, label %44, label %11
44: ; preds = %40
%45 = icmp eq i32 %41, 0
%46 = load i64, i64* @EACCES
%47 = load i64, i64* @ENOENT
%48 = select i1 %45, i64 %47, i64 %46
store i64 %48, i64* @errno
br label %49
49: ; preds = %36, %44
%50 = phi i32 [ -1, %44 ], [ %39, %36 ]
ret i32 %50
}