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

/* Variables and functions */
char **xrealloc(char **, int);
char *xstrdup(char const *);
int add_device(const char *dev, char ***p_devices, int *p_max_devices,
               int n_devices) {
  if (n_devices + 1 >= *p_max_devices) {
    *p_max_devices += 16;
    *p_devices = xrealloc(*p_devices, *p_max_devices * sizeof(**p_devices));
    if (!*p_devices) {
      *p_max_devices = 0;
      return 0;
    }
  };
  (*p_devices)[n_devices] = xstrdup(dev);
  return n_devices + 1;
}


x86 assembly (clang, -O0):
<add_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jl     11a0 <add_device+0x80>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x10,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1030 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    119b <add_device+0x7b>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11c4 <add_device+0xa4>
jmp    11a0 <add_device+0x80>
mov    -0x10(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_device>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x1(%r14),%ebp
mov    (%rdx),%eax
cmp    %eax,%ebp
jl     115a <add_device+0x3a>
mov    %rdx,%rbx
lea    0x10(%rax),%ecx
mov    %ecx,(%rdx)
mov    (%r15),%rdi
lea    0x80(,%rax,8),%esi
call   1030 <xrealloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     116e <add_device+0x4e>
mov    %r12,%rdi
call   1040 <xstrdup@plt>
mov    (%r15),%rcx
movslq %r14d,%rdx
mov    %rax,(%rcx,%rdx,8)
jmp    1176 <add_device+0x56>
movl   $0x0,(%rbx)
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @add_device(i8* %0, i8*** %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8***
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %6
store i8*** %1, i8**** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%10 = load i32, i32* %9
%11 = add nsw i32 %10, 1
%12 = load i32*, i32** %8
%13 = load i32, i32* %12
%14 = icmp sge i32 %11, %13
br i1 %14, label %15, label %34
15: ; preds = %4
%16 = load i32*, i32** %8
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 16
store i32 %18, i32* %16
%19 = load i8***, i8**** %7
%20 = load i8**, i8*** %19
%21 = load i32*, i32** %8
%22 = load i32, i32* %21
%23 = sext i32 %22 to i64
%24 = mul i64 %23, 8
%25 = trunc i64 %24 to i32
%26 = call i8** @xrealloc(i8** %20, i32 %25)
%27 = load i8***, i8**** %7
store i8** %26, i8*** %27
%28 = load i8***, i8**** %7
%29 = load i8**, i8*** %28
%30 = icmp ne i8** %29, null
br i1 %30, label %33, label %31
31: ; preds = %15
%32 = load i32*, i32** %8
store i32 0, i32* %32
store i32 0, i32* %5
br label %44
33: ; preds = %15
br label %34
34: ; preds = %33, %4
%35 = load i8*, i8** %6
%36 = call i8* @xstrdup(i8* %35)
%37 = load i8***, i8**** %7
%38 = load i8**, i8*** %37
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
store i8* %36, i8** %41
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %44
44: ; preds = %34, %31
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
define i32 @add_device(i8* %0, i8*** nocapture %1, i32* nocapture %2, i32 %3) {
%5 = add nsw i32 %3, 1
%6 = load i32, i32* %2
%7 = icmp slt i32 %5, %6
br i1 %7, label %15, label %8
8: ; preds = %4
%9 = add nsw i32 %6, 16
store i32 %9, i32* %2
%10 = load i8**, i8*** %1
%11 = shl i32 %9, 3
%12 = tail call i8** @xrealloc(i8** %10, i32 %11)
store i8** %12, i8*** %1
%13 = icmp eq i8** %12, null
br i1 %13, label %14, label %15
14: ; preds = %8
store i32 0, i32* %2
br label %20
15: ; preds = %8, %4
%16 = tail call i8* @xstrdup(i8* %0)
%17 = load i8**, i8*** %1
%18 = sext i32 %3 to i64
%19 = getelementptr inbounds i8*, i8** %17, i64 %18
store i8* %16, i8** %19
br label %20
20: ; preds = %15, %14
%21 = phi i32 [ %5, %15 ], [ 0, %14 ]
ret i32 %21
}