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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int strlen(char *);
int get_uuid_by_partition(char *uuid, int uuid_size) {
  FILE *fd;
  int ret = -1;
  char *cmd = "ls -X /dev/disk/by-uuid/";
  if ((fd = popen(cmd, "r")) == NULL) {
    goto out;
  }
  while (1) {
    if (fgets(uuid, uuid_size, fd) == NULL) {
      goto out;
    }
    if (strlen(uuid) == 37) {
      uuid[strlen(uuid) - 1] = '\0';
      ret = 0;
      break;
    }
  }
out:
  pclose(fd);
  return ret;
}


x86 assembly (clang, -O0):
<get_uuid_by_partition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    0xea3(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xead(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1184 <get_uuid_by_partition+0x44>
jmp    11f1 <get_uuid_by_partition+0xb1>
jmp    1189 <get_uuid_by_partition+0x49>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11a8 <get_uuid_by_partition+0x68>
jmp    11f1 <get_uuid_by_partition+0xb1>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x25,%eax
jne    11e7 <get_uuid_by_partition+0xa7>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x1c(%rbp)
jmp    11ec <get_uuid_by_partition+0xac>
jmp    1189 <get_uuid_by_partition+0x49>
jmp    11f1 <get_uuid_by_partition+0xb1>
mov    -0x18(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_uuid_by_partition>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xead(%rip),%rdi
lea    0xebf(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r14
mov    $0xffffffff,%r15d
test   %rax,%rax
je     11a1 <get_uuid_by_partition+0x61>
nopl   (%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11a1 <get_uuid_by_partition+0x61>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x25,%eax
jne    1170 <get_uuid_by_partition+0x30>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %r15d,%r15d
mov    %r14,%rdi
call   1040 <pclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"ls -X /dev/disk/by-uuid/\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_uuid_by_partition(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 -1, i32* %6
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = load i8*, i8** %7
%9 = call i32* @popen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
br label %33
12: ; preds = %2
br label %13
13: ; preds = %12, %31
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = load i32*, i32** %5
%17 = call i32* @fgets(i8* %14, i32 %15, i32* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %13
br label %33
20: ; preds = %13
%21 = load i8*, i8** %3
%22 = call i32 @strlen(i8* %21)
%23 = icmp eq i32 %22, 37
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = load i8*, i8** %3
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %25, i64 %29
store i8 0, i8* %30
store i32 0, i32* %6
br label %32
31: ; preds = %20
br label %13
32: ; preds = %24
br label %33
33: ; preds = %32, %19, %11
%34 = load i32*, i32** %5
%35 = call i32 @pclose(i32* %34)
%36 = load i32, i32* %6
ret i32 %36
}

LLVM IR (-O2):
@.str = [25 x i8] c"ls -X /dev/disk/by-uuid/\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_uuid_by_partition(i8* %0, i32 %1) {
%3 = tail call i32* @popen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %16, label %5
5: ; preds = %2, %8
%6 = tail call i32* @fgets(i8* %0, i32 %1, i32* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %16, label %8
8: ; preds = %5
%9 = tail call i32 @strlen(i8* %0)
%10 = icmp eq i32 %9, 37
br i1 %10, label %11, label %5
11: ; preds = %8
%12 = tail call i32 @strlen(i8* %0)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %5, %2, %11
%17 = phi i32 [ -1, %2 ], [ 0, %11 ], [ -1, %5 ]
%18 = tail call i32 @pclose(i32* %3)
ret i32 %17
}