#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 dm_target_versions {
  int next;
  char *name;
  int *version;
};
struct dm_ioctl {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ DM_LIST_VERSIONS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct dm_ioctl *);
int /*<<< orphan*/ ioctl_init(struct dm_ioctl *, int, int /*<<< orphan*/ *,
                              int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int, int, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char buffer[4096];
  struct dm_ioctl *io;
  struct dm_target_versions *v;
  int i;
  int fd;
  fd = open("/dev/device-mapper", O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Cannot open /dev/device-mapper\n");
    exit(1);
  }
  io = (struct dm_ioctl *)buffer;
  ioctl_init(io, 4096, NULL, 0);
  if (ioctl(fd, DM_LIST_VERSIONS, io)) {
    fprintf(stderr, "ioctl(DM_LIST_VERSIONS) returned an error\n");
    exit(1);
  }
  v = (struct dm_target_versions *)&buffer[sizeof(struct dm_ioctl)];
  while (v->next) {
    printf("%s: %d.%d.%d\n", v->name, v->version[0], v->version[1],
           v->version[2]);
    v = (struct dm_target_versions *)(((char *)v) + v->next);
  }
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1028(%rbp)
cmpl   $0x0,-0x1028(%rbp)
jge    11d0 <main+0x60>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x1018(%rbp),%rdi
mov    $0x1000,%esi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <ioctl_init@plt>
mov    -0x1028(%rbp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    -0x1018(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1239 <main+0xc9>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x1010(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
cmpl   $0x0,(%rax)
je     12bc <main+0x14c>
mov    -0x1020(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x1020(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%edx
mov    -0x1020(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x4(%rax),%ecx
mov    -0x1020(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x8(%rax),%r8d
lea    0xdc7(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1020(%rbp),%rax
mov    -0x1020(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x1020(%rbp)
jmp    124b <main+0xdb>
mov    -0x1028(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
jns    11ab <main+0x3b>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
jmp    1249 <main+0xd9>
mov    %eax,%r14d
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ioctl_init@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
jne    1239 <main+0xc9>
cmpl   $0x0,0x4(%rsp)
je     122a <main+0xba>
lea    0x4(%rsp),%rbx
lea    0xe70(%rip),%r15
mov    %rbx,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rbp),%rsi
mov    0x10(%rbp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%ecx
mov    0x8(%rax),%r8d
mov    %r15,%rdi
call   1030 <printf@plt>
movslq (%rbx),%rax
lea    (%rax,%rbp,1),%rbx
cmpl   $0x0,0x0(%rbp,%rax,1)
mov    %rbx,%rbp
jne    1200 <main+0x90>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.dm_ioctl = type { i32 }
%struct.dm_target_versions = type { i32, i8*, i32* }
@.str = [19 x i8] c"/dev/device-mapper\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Cannot open /dev/device-mapper\0A\00"
@DM_LIST_VERSIONS = global i32 0
@.str.2 = [43 x i8] c"ioctl(DM_LIST_VERSIONS) returned an error\0A\00"
@.str.3 = [14 x i8] c"%s: %d.%d.%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4096 x i8]
%7 = alloca %struct.dm_ioctl*
%8 = alloca %struct.dm_target_versions*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%21 = bitcast i8* %20 to %struct.dm_ioctl*
store %struct.dm_ioctl* %21, %struct.dm_ioctl** %7
%22 = load %struct.dm_ioctl*, %struct.dm_ioctl** %7
%23 = call i32 @ioctl_init(%struct.dm_ioctl* %22, i32 4096, i32* null, i32 0)
%24 = load i32, i32* %10
%25 = load i32, i32* @DM_LIST_VERSIONS
%26 = load %struct.dm_ioctl*, %struct.dm_ioctl** %7
%27 = call i64 @ioctl(i32 %24, i32 %25, %struct.dm_ioctl* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %19
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 4
%35 = bitcast i8* %34 to %struct.dm_target_versions*
store %struct.dm_target_versions* %35, %struct.dm_target_versions** %8
br label %36
36: ; preds = %41, %33
%37 = load %struct.dm_target_versions*, %struct.dm_target_versions** %8
%38 = getelementptr inbounds %struct.dm_target_versions, %struct.dm_target_versions* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %69
41: ; preds = %36
%42 = load %struct.dm_target_versions*, %struct.dm_target_versions** %8
%43 = getelementptr inbounds %struct.dm_target_versions, %struct.dm_target_versions* %42, i32 0, i32 1
%44 = load i8*, i8** %43
%45 = load %struct.dm_target_versions*, %struct.dm_target_versions** %8
%46 = getelementptr inbounds %struct.dm_target_versions, %struct.dm_target_versions* %45, i32 0, i32 2
%47 = load i32*, i32** %46
%48 = getelementptr inbounds i32, i32* %47, i64 0
%49 = load i32, i32* %48
%50 = load %struct.dm_target_versions*, %struct.dm_target_versions** %8
%51 = getelementptr inbounds %struct.dm_target_versions, %struct.dm_target_versions* %50, i32 0, i32 2
%52 = load i32*, i32** %51
%53 = getelementptr inbounds i32, i32* %52, i64 1
%54 = load i32, i32* %53
%55 = load %struct.dm_target_versions*, %struct.dm_target_versions** %8
%56 = getelementptr inbounds %struct.dm_target_versions, %struct.dm_target_versions* %55, i32 0, i32 2
%57 = load i32*, i32** %56
%58 = getelementptr inbounds i32, i32* %57, i64 2
%59 = load i32, i32* %58
%60 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %49, i32 %54, i32 %59)
%61 = load %struct.dm_target_versions*, %struct.dm_target_versions** %8
%62 = bitcast %struct.dm_target_versions* %61 to i8*
%63 = load %struct.dm_target_versions*, %struct.dm_target_versions** %8
%64 = getelementptr inbounds %struct.dm_target_versions, %struct.dm_target_versions* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %62, i64 %66
%68 = bitcast i8* %67 to %struct.dm_target_versions*
store %struct.dm_target_versions* %68, %struct.dm_target_versions** %8
br label %36
69: ; preds = %36
%70 = load i32, i32* %10
%71 = call i32 @close(i32 %70)
%72 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.dm_ioctl = type { i32 }
@.str = [19 x i8] c"/dev/device-mapper\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Cannot open /dev/device-mapper\0A\00"
@DM_LIST_VERSIONS = global i32 0
@.str.2 = [43 x i8] c"ioctl(DM_LIST_VERSIONS) returned an error\0A\00"
@.str.3 = [14 x i8] c"%s: %d.%d.%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = bitcast [4096 x i8]* %3 to %struct.dm_ioctl*
%14 = call i32 @ioctl_init(%struct.dm_ioctl* %13, i32 4096, i32* null, i32 0)
%15 = load i32, i32* @DM_LIST_VERSIONS
%16 = call i64 @ioctl(i32 %6, i32 %15, %struct.dm_ioctl* %13)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 4
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, 0
br i1 %26, label %48, label %27
27: ; preds = %22, %27
%28 = phi i32* [ %45, %27 ], [ %24, %22 ]
%29 = phi i8* [ %44, %27 ], [ %23, %22 ]
%30 = getelementptr inbounds i8, i8* %29, i64 8
%31 = bitcast i8* %30 to i8**
%32 = load i8*, i8** %31
%33 = getelementptr inbounds i8, i8* %29, i64 16
%34 = bitcast i8* %33 to i32**
%35 = load i32*, i32** %34
%36 = load i32, i32* %35
%37 = getelementptr inbounds i32, i32* %35, i64 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds i32, i32* %35, i64 2
%40 = load i32, i32* %39
%41 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %32, i32 %36, i32 %38, i32 %40)
%42 = load i32, i32* %28
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %29, i64 %43
%45 = bitcast i8* %44 to i32*
%46 = load i32, i32* %45
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %27
48: ; preds = %27, %22
%49 = call i32 @close(i32 %6)
%50 = call i32 @exit(i32 0)
unreachable
}