#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*/ uuid;
typedef int /*<<< orphan*/ cmd;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *get_usb_serial_num(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ system(char *);
char *get_sd_card_serial_num(char *part_name) {
  static char uuid[128];
  char cmd[64], buf[128];
  int i = 0;
  FILE *fp;
  buf[0] = '\0';
  snprintf(cmd, sizeof(cmd),
           "/usr/sbin/vol_id -u /dev/%s > "
           "/tmp/sd_card_uuid",
           part_name);
  system(cmd);
  if (!(fp = fopen("/tmp/sd_card_uuid", "r")))
    return get_usb_serial_num(part_name);
  fgets(buf, sizeof(buf), fp);
  fclose(fp);
  while (buf[i] != '\0' && buf[i] != '\r' && buf[i] != '\n')
    i++;
  buf[i] = '\0';
  if (i == 0)
    return get_usb_serial_num(part_name);
  strncpy(uuid, buf, sizeof(uuid));
  return uuid;
}


x86 assembly (clang, -O0):
<get_sd_card_serial_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0xd4(%rbp)
movb   $0x0,-0xd0(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x40,%esi
lea    0xe5c(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <system@plt>
lea    0xe65(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xe0(%rbp)
cmp    $0x0,%rax
jne    11e8 <get_sd_card_serial_num+0x78>
mov    -0x10(%rbp),%rdi
call   1090 <get_usb_serial_num@plt>
mov    %rax,-0x8(%rbp)
jmp    12df <get_sd_card_serial_num+0x16f>
lea    -0xd0(%rbp),%rdi
mov    -0xe0(%rbp),%rdx
mov    $0x80,%esi
call   1070 <fgets@plt>
mov    -0xe0(%rbp),%rdi
call   1040 <fclose@plt>
movslq -0xd4(%rbp),%rax
movsbl -0xd0(%rbp,%rax,1),%ecx
ff
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xe1(%rbp)
je     1267 <get_sd_card_serial_num+0xf7>
movslq -0xd4(%rbp),%rax
movsbl -0xd0(%rbp,%rax,1),%ecx
ff
xor    %eax,%eax
cmp    $0xd,%ecx
mov    %al,-0xe1(%rbp)
je     1267 <get_sd_card_serial_num+0xf7>
movslq -0xd4(%rbp),%rax
movsbl -0xd0(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
setne  %al
mov    %al,-0xe1(%rbp)
mov    -0xe1(%rbp),%al
test   $0x1,%al
jne    127a <get_sd_card_serial_num+0x10a>
jmp    128e <get_sd_card_serial_num+0x11e>
mov    -0xd4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd4(%rbp)
jmp    120c <get_sd_card_serial_num+0x9c>
movslq -0xd4(%rbp),%rax
movb   $0x0,-0xd0(%rbp,%rax,1)
cmpl   $0x0,-0xd4(%rbp)
jne    12bc <get_sd_card_serial_num+0x14c>
mov    -0x10(%rbp),%rdi
call   1090 <get_usb_serial_num@plt>
mov    %rax,-0x8(%rbp)
jmp    12df <get_sd_card_serial_num+0x16f>
lea    -0xd0(%rbp),%rsi
lea    0x2da6(%rip),%rdi
mov    $0x80,%edx
call   1030 <strncpy@plt>
lea    0x2d95(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sd_card_serial_num>:
push   %r14
push   %rbx
sub    $0xc8,%rsp
mov    %rdi,%r14
movb   $0x0,(%rsp)
lea    0xe78(%rip),%rdx
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
lea    0xe6f(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1227 <get_sd_card_serial_num+0xb7>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
mov    $0x2401,%ecx
jmp    11f4 <get_sd_card_serial_num+0x84>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rax
movzbl (%rsp,%rax,1),%edx
cmp    $0xd,%rdx
ja     11f0 <get_sd_card_serial_num+0x80>
bt     %rdx,%rcx
jae    11f0 <get_sd_card_serial_num+0x80>
mov    %eax,%ecx
movb   $0x0,(%rsp,%rcx,1)
test   %eax,%eax
je     1227 <get_sd_card_serial_num+0xb7>
lea    0x2e5b(%rip),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x80,%edx
call   1030 <strncpy@plt>
jmp    1232 <get_sd_card_serial_num+0xc2>
mov    %r14,%rdi
call   1090 <get_usb_serial_num@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0xc8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@get_sd_card_serial_num.uuid = internal global [128 x i8] zeroinitializer
@.str = [48 x i8] c"/usr/sbin/vol_id -u /dev/%s > /tmp/sd_card_uuid\00"
@.str.1 = [18 x i8] c"/tmp/sd_card_uuid\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @get_sd_card_serial_num(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [64 x i8]
%5 = alloca [128 x i8]
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %6
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
store i8 0, i8* %8
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%13 = call i32 @system(i8* %12)
%14 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %14, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i8* @get_usb_serial_num(i8* %17)
store i8* %18, i8** %2
br label %63
19: ; preds = %1
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%21 = load i32*, i32** %7
%22 = call i32 @fgets(i8* %20, i32 128, i32* %21)
%23 = load i32*, i32** %7
%24 = call i32 @fclose(i32* %23)
br label %25
25: ; preds = %48, %19
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %46
32: ; preds = %25
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 13
br i1 %38, label %39, label %46
39: ; preds = %32
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 10
br label %46
46: ; preds = %39, %32, %25
%47 = phi i1 [ false, %32 ], [ false, %25 ], [ %45, %39 ]
br i1 %47, label %48, label %51
48: ; preds = %46
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %25
51: ; preds = %46
%52 = load i32, i32* %6
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %6
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = load i8*, i8** %3
%59 = call i8* @get_usb_serial_num(i8* %58)
store i8* %59, i8** %2
br label %63
60: ; preds = %51
%61 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%62 = call i32 @strncpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_sd_card_serial_num.uuid, i64 0, i64 0), i8* %61, i32 128)
store i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_sd_card_serial_num.uuid, i64 0, i64 0), i8** %2
br label %63
63: ; preds = %60, %57, %16
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
@get_sd_card_serial_num.uuid = internal global [128 x i8] zeroinitializer
@.str = [48 x i8] c"/usr/sbin/vol_id -u /dev/%s > /tmp/sd_card_uuid\00"
@.str.1 = [18 x i8] c"/tmp/sd_card_uuid\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @get_sd_card_serial_num(i8* %0) {
%2 = alloca [64 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i8 0, i8* %5
%6 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i32 @system(i8* %4)
%8 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i8* @get_usb_serial_num(i8* %0)
br label %30
12: ; preds = %1
%13 = call i32 @fgets(i8* %5, i32 128, i32* %8)
%14 = call i32 @fclose(i32* %8)
br label %15
15: ; preds = %19, %12
%16 = phi i64 [ %20, %19 ], [ 0, %12 ]
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %16
%18 = load i8, i8* %17
switch i8 %18, label %19 [
i8 0, label %21
i8 13, label %21
i8 10, label %21
]
19: ; preds = %15
%20 = add nuw i64 %16, 1
br label %15
21: ; preds = %15, %15, %15
%22 = trunc i64 %16 to i32
%23 = and i64 %16, 4294967295
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = icmp eq i32 %22, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i8* @get_usb_serial_num(i8* %0)
br label %30
28: ; preds = %21
%29 = call i32 @strncpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_sd_card_serial_num.uuid, i64 0, i64 0), i8* %5, i32 128)
br label %30
30: ; preds = %28, %26, %10
%31 = phi i8* [ %27, %26 ], [ getelementptr inbounds ([128 x i8], [128 x i8]* @get_sd_card_serial_num.uuid, i64 0, i64 0), %28 ], [ %11, %10 ]
ret i8* %31
}