#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;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int sys_read_string(char *, char *, char *, int);
int sys_read_uint(char *, char *, unsigned int *);
int umad_id_to_dev(int umad_id, char *dev, unsigned *port) {
  char path[256];
  int r;
  snprintf(path, sizeof(path),
           "/sys/class/infiniband_mad"
           "/umad%d/",
           umad_id);
  if ((r = sys_read_string(path, "ibdev", dev, 20)) < 0)
    return r;
  if ((r = sys_read_uint(path, "port", port)) < 0)
    return r;
  return 0;
}


x86 assembly (clang, -O0):
<umad_id_to_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xea4(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xeaf(%rip),%rsi
mov    $0x14,%ecx
call   1040 <sys_read_string@plt>
mov    %eax,-0x124(%rbp)
cmp    $0x0,%eax
jge    119a <umad_id_to_dev+0x6a>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d5 <umad_id_to_dev+0xa5>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe7c(%rip),%rsi
call   1050 <sys_read_uint@plt>
mov    %eax,-0x124(%rbp)
cmp    $0x0,%eax
jge    11ce <umad_id_to_dev+0x9e>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d5 <umad_id_to_dev+0xa5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<umad_id_to_dev>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ecx
lea    0xeb5(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x100,%esi
call   1030 <snprintf@plt>
lea    0xec0(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
mov    $0x14,%ecx
call   1040 <sys_read_string@plt>
test   %eax,%eax
js     118f <umad_id_to_dev+0x5f>
lea    0xeab(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
call   1050 <sys_read_uint@plt>
mov    %eax,%ecx
sar    $0x1f,%eax
and    %ecx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [34 x i8] c"/sys/class/infiniband_mad/umad%d/\00"
@.str.1 = [6 x i8] c"ibdev\00"
@.str.2 = [5 x i8] c"port\00"
define i32 @umad_id_to_dev(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [256 x i8]
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%14 = load i8*, i8** %6
%15 = call i32 @sys_read_string(i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 20)
store i32 %15, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* %9
store i32 %18, i32* %4
br label %27
19: ; preds = %3
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%21 = load i32*, i32** %7
%22 = call i32 @sys_read_uint(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %21)
store i32 %22, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i32, i32* %9
store i32 %25, i32* %4
br label %27
26: ; preds = %19
store i32 0, i32* %4
br label %27
27: ; preds = %26, %24, %17
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@.str = [34 x i8] c"/sys/class/infiniband_mad/umad%d/\00"
@.str.1 = [6 x i8] c"ibdev\00"
@.str.2 = [5 x i8] c"port\00"
define i32 @umad_id_to_dev(i32 %0, i8* %1, i32* %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32 @sys_read_string(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 20)
%8 = icmp slt i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = call i32 @sys_read_uint(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%11 = icmp slt i32 %10, 0
%12 = select i1 %11, i32 %10, i32 0
br label %13
13: ; preds = %9, %3
%14 = phi i32 [ %7, %3 ], [ %12, %9 ]
ret i32 %14
}