#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 hd_driveid {
  char *serial_no;
};

/* Variables and functions */
int /*<<< orphan*/ HDIO_GET_IDENTITY;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct hd_driveid *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int getdiskid(char *hardc) {
  int fd;
  struct hd_driveid hid;
  fd = open("/dev/sda", O_RDONLY);
  if (fd < 0) {
    perror("error");
    return -1;
  }
  if (ioctl(fd, HDIO_GET_IDENTITY, &hid) < 0) {
    return -1;
  }
  close(fd);
  sprintf(hardc, "%s", hid.serial_no);
  return 0;
}


x86 assembly (clang, -O0):
<getdiskid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1196 <getdiskid+0x46>
lea    0xe84(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <getdiskid+0x94>
mov    -0x14(%rbp),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11c1 <getdiskid+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <getdiskid+0x94>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1070 <sprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdiskid>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a0 <getdiskid+0x50>
mov    %eax,%ebp
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11ac <getdiskid+0x5c>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rsp),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
jmp    11b1 <getdiskid+0x61>
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.hd_driveid = type { i8* }
@.str = [9 x i8] c"/dev/sda\00"
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"error\00"
@HDIO_GET_IDENTITY = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @getdiskid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.hd_driveid
store i8* %0, i8** %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %25
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @HDIO_GET_IDENTITY
%15 = call i64 @ioctl(i32 %13, i32 %14, %struct.hd_driveid* %5)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %2
br label %25
18: ; preds = %12
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = load i8*, i8** %3
%22 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %5, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %23)
store i32 0, i32* %2
br label %25
25: ; preds = %18, %17, %10
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.hd_driveid = type { i8* }
@.str = [9 x i8] c"/dev/sda\00"
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"error\00"
@HDIO_GET_IDENTITY = global i32 0
define i32 @getdiskid(i8* %0) {
%2 = alloca %struct.hd_driveid
%3 = bitcast %struct.hd_driveid* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %18
9: ; preds = %1
%10 = load i32, i32* @HDIO_GET_IDENTITY
%11 = call i64 @ioctl(i32 %5, i32 %10, %struct.hd_driveid* %2)
%12 = icmp slt i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = call i32 @close(i32 %5)
%15 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %2, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i8* @strcpy(i8* dereferenceable(1) %0, i8* dereferenceable(1) %16)
br label %18
18: ; preds = %9, %13, %7
%19 = phi i32 [ -1, %7 ], [ 0, %13 ], [ -1, %9 ]
ret i32 %19
}