#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 snd_pcm_hw_params {
  int dummy;
};
struct pcm_params {
  int dummy;
};
typedef int /*<<< orphan*/ fn;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SNDRV_PCM_IOCTL_HW_REFINE;
struct snd_pcm_hw_params *calloc(int, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(struct snd_pcm_hw_params *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct snd_pcm_hw_params *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ param_init(struct snd_pcm_hw_params *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, unsigned int,
                            char);
int /*<<< orphan*/ stderr;
struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
                                  unsigned int flags) {
  struct snd_pcm_hw_params *params;
  char fn[256];
  int fd;
  snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
           flags & 0x10000000 ? 'c' : 'p');
  fd = open(fn, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "cannot open device '%s'\n", fn);
    goto err_open;
  }
  params = calloc(1, sizeof(struct snd_pcm_hw_params));
  if (!params)
    goto err_calloc;
  param_init(params);
  if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
    fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
    goto err_hw_refine;
  }
  close(fd);
  return (struct pcm_params *)params;
err_hw_refine:
  free(params);
err_calloc:
  close(fd);
err_open:
  return NULL;
}


x86 assembly (clang, -O0):
<pcm_params_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
mov    -0x14(%rbp),%esi
and    $0x10000000,%esi
mov    $0x70,%eax
mov    $0x63,%edx
cmp    $0x0,%esi
cmovne %edx,%eax
mov    $0x100,%esi
lea    0xe39(%rip),%rdx
movsbl %al,%r9d
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    121b <pcm_params_get+0x9b>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    12ba <pcm_params_get+0x13a>
mov    $0x1,%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123e <pcm_params_get+0xbe>
jmp    12af <pcm_params_get+0x12f>
mov    -0x20(%rbp),%rdi
call   1040 <param_init@plt>
mov    -0x124(%rbp),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     128e <pcm_params_get+0x10e>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    12a6 <pcm_params_get+0x126>
mov    -0x124(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c2 <pcm_params_get+0x142>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x124(%rbp),%edi
call   1070 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pcm_params_get>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
mov    %esi,%r8d
mov    %edi,%ecx
test   $0x10000000,%edx
mov    $0x70,%eax
mov    $0x63,%r9d
cmove  %eax,%r9d
lea    0xe56(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     123b <pcm_params_get+0xbb>
mov    %eax,%ebp
mov    $0x1,%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     1230 <pcm_params_get+0xb0>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <param_init@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
je     1259 <pcm_params_get+0xd9>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
lea    0xe0d(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %ebx,%ebx
jmp    1260 <pcm_params_get+0xe0>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
xor    %ebx,%ebx
mov    %rsp,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1260 <pcm_params_get+0xe0>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %rbx,%rax
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.pcm_params = type { i32 }
%struct.snd_pcm_hw_params = type { i32 }
@.str = [21 x i8] c"/dev/snd/pcmC%uD%u%c\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"cannot open device '%s'\0A\00"
@SNDRV_PCM_IOCTL_HW_REFINE = global i32 0
@.str.2 = [38 x i8] c"SNDRV_PCM_IOCTL_HW_REFINE error (%d)\0A\00"
@errno = global i32 0
define %struct.pcm_params* @pcm_params_get(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.pcm_params*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.snd_pcm_hw_params*
%9 = alloca [256 x i8]
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = load i32, i32* %7
%15 = and i32 %14, 268435456
%16 = icmp ne i32 %15, 0
%17 = zext i1 %16 to i64
%18 = select i1 %16, i32 99, i32 112
%19 = trunc i32 %18 to i8
%20 = call i32 @snprintf(i8* %11, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13, i8 signext %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %3
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %58
30: ; preds = %3
%31 = call %struct.snd_pcm_hw_params* @calloc(i32 1, i32 4)
store %struct.snd_pcm_hw_params* %31, %struct.snd_pcm_hw_params** %8
%32 = load %struct.snd_pcm_hw_params*, %struct.snd_pcm_hw_params** %8
%33 = icmp ne %struct.snd_pcm_hw_params* %32, null
br i1 %33, label %35, label %34
34: ; preds = %30
br label %55
35: ; preds = %30
%36 = load %struct.snd_pcm_hw_params*, %struct.snd_pcm_hw_params** %8
%37 = call i32 @param_init(%struct.snd_pcm_hw_params* %36)
%38 = load i32, i32* %10
%39 = load i32, i32* @SNDRV_PCM_IOCTL_HW_REFINE
%40 = load %struct.snd_pcm_hw_params*, %struct.snd_pcm_hw_params** %8
%41 = call i64 @ioctl(i32 %38, i32 %39, %struct.snd_pcm_hw_params* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %52
47: ; preds = %35
%48 = load i32, i32* %10
%49 = call i32 @close(i32 %48)
%50 = load %struct.snd_pcm_hw_params*, %struct.snd_pcm_hw_params** %8
%51 = bitcast %struct.snd_pcm_hw_params* %50 to %struct.pcm_params*
store %struct.pcm_params* %51, %struct.pcm_params** %4
br label %59
52: ; preds = %43
%53 = load %struct.snd_pcm_hw_params*, %struct.snd_pcm_hw_params** %8
%54 = call i32 @free(%struct.snd_pcm_hw_params* %53)
br label %55
55: ; preds = %52, %34
%56 = load i32, i32* %10
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55, %26
store %struct.pcm_params* null, %struct.pcm_params** %4
br label %59
59: ; preds = %58, %47
%60 = load %struct.pcm_params*, %struct.pcm_params** %4
ret %struct.pcm_params* %60
}

LLVM IR (-O2):
%struct.pcm_params = type { i32 }
%struct.snd_pcm_hw_params = type { i32 }
@.str = [21 x i8] c"/dev/snd/pcmC%uD%u%c\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"cannot open device '%s'\0A\00"
@SNDRV_PCM_IOCTL_HW_REFINE = global i32 0
@.str.2 = [38 x i8] c"SNDRV_PCM_IOCTL_HW_REFINE error (%d)\0A\00"
@errno = global i32 0
define %struct.pcm_params* @pcm_params_get(i32 %0, i32 %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = and i32 %2, 268435456
%7 = icmp eq i32 %6, 0
%8 = select i1 %7, i8 112, i8 99
%9 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1, i8 signext %8)
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %5, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %34
16: ; preds = %3
%17 = call %struct.snd_pcm_hw_params* @calloc(i32 1, i32 4)
%18 = icmp eq %struct.snd_pcm_hw_params* %17, null
br i1 %18, label %32, label %19
19: ; preds = %16
%20 = call i32 @param_init(%struct.snd_pcm_hw_params* %17)
%21 = load i32, i32* @SNDRV_PCM_IOCTL_HW_REFINE
%22 = call i64 @ioctl(i32 %11, i32 %21, %struct.snd_pcm_hw_params* %17)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = call i32 @free(%struct.snd_pcm_hw_params* %17)
br label %32
29: ; preds = %19
%30 = call i32 @close(i32 %11)
%31 = bitcast %struct.snd_pcm_hw_params* %17 to %struct.pcm_params*
br label %34
32: ; preds = %16, %24
%33 = call i32 @close(i32 %11)
br label %34
34: ; preds = %13, %32, %29
%35 = phi %struct.pcm_params* [ %31, %29 ], [ null, %32 ], [ null, %13 ]
ret %struct.pcm_params* %35
}