#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 {
  int dummy;
};
struct mtd_info_user {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ MEMGETINFO;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                         char *);
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct mtd_info_user *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int get_flash_info(char *device, struct mtd_info_user *iu, struct stat *fs) {
  int fd;
  if ((fd = open(device, O_RDONLY)) < 0) {
    printf("Can't open %s\n", device);
    return -1;
  }
  fstat(fd, fs);
  if (ioctl(fd, MEMGETINFO, iu) < 0) {
    error(0, errno, "ioclt MEMGETINFO on device %s", device);
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<get_flash_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ae <get_flash_info+0x4e>
mov    -0x10(%rbp),%rsi
lea    0xe63(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <get_flash_info+0xb7>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x24(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1208 <get_flash_info+0xa8>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
xor    %edi,%edi
lea    0xe20(%rip),%rdx
call   1060 <error@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <get_flash_info+0xb7>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_flash_info>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11b1 <get_flash_info+0x51>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1080 <fstat@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     11c2 <get_flash_info+0x62>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11e8 <get_flash_info+0x88>
lea    0xe48(%rip),%rdi
mov    %r15,%rsi
call   1030 <printf@plt>
jmp    11e3 <get_flash_info+0x83>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0xe3d(%rip),%rdx
xor    %edi,%edi
mov    %r15,%rcx
call   1060 <error@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mtd_info_user = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@MEMGETINFO = global i32 0
@errno = global i32 0
@.str.1 = [30 x i8] c"ioclt MEMGETINFO on device %s\00"
define i32 @get_flash_info(i8* %0, %struct.mtd_info_user* %1, %struct.stat* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.mtd_info_user*
%7 = alloca %struct.stat*
%8 = alloca i32
store i8* %0, i8** %5
store %struct.mtd_info_user* %1, %struct.mtd_info_user** %6
store %struct.stat* %2, %struct.stat** %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 -1, i32* %4
br label %34
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load %struct.stat*, %struct.stat** %7
%19 = call i32 @fstat(i32 %17, %struct.stat* %18)
%20 = load i32, i32* %8
%21 = load i32, i32* @MEMGETINFO
%22 = load %struct.mtd_info_user*, %struct.mtd_info_user** %6
%23 = call i64 @ioctl(i32 %20, i32 %21, %struct.mtd_info_user* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %16
%26 = load i32, i32* @errno
%27 = load i8*, i8** %5
%28 = call i32 @error(i32 0, i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %4
br label %34
31: ; preds = %16
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
store i32 0, i32* %4
br label %34
34: ; preds = %31, %25, %13
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.mtd_info_user = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@MEMGETINFO = global i32 0
@errno = global i32 0
@.str.1 = [30 x i8] c"ioclt MEMGETINFO on device %s\00"
define i32 @get_flash_info(i8* %0, %struct.mtd_info_user* %1, %struct.stat* nocapture %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
9: ; preds = %3
%10 = tail call i32 @fstat(i32 %5, %struct.stat* %2)
%11 = load i32, i32* @MEMGETINFO
%12 = tail call i64 @ioctl(i32 %5, i32 %11, %struct.mtd_info_user* %1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i32 @error(i32 0, i32 %15, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%17 = tail call i32 @close(i32 %5)
br label %20
18: ; preds = %9
%19 = tail call i32 @close(i32 %5)
br label %20
20: ; preds = %18, %14, %7
%21 = phi i32 [ -1, %7 ], [ -1, %14 ], [ 0, %18 ]
ret i32 %21
}