#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 usb_device {
  int dummy;
};
struct usb_dev_handle {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ Sleep(int);
int /*<<< orphan*/ printf(char *, char *);
int usb_claim_interface(struct usb_dev_handle *, int /*<<< orphan*/);
int usb_clear_halt(struct usb_dev_handle *, int);
struct usb_dev_handle *usb_open(struct usb_device *);
int usb_set_altinterface(struct usb_dev_handle *, int /*<<< orphan*/);
int usb_set_configuration(struct usb_dev_handle *, int);
struct usb_dev_handle *hpsdr_open_interface(struct usb_device *dev,
                                            int interface, int altinterface) {
  int rc;
  struct usb_dev_handle *udh;
  for (; (udh = usb_open(dev)) == NULL; Sleep(50)) {
    printf("%s\n", "Can't open USB");
  }
  if (udh == 0)
    return NULL;
  rc = usb_set_configuration(udh, 1);
  rc = usb_claim_interface(udh, 0);
  rc = usb_set_altinterface(udh, 0);
  rc = usb_clear_halt(udh, 0x86);
  rc = usb_clear_halt(udh, 2);
  return udh;
}


x86 assembly (clang, -O0):
<hpsdr_open_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <usb_open@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11bb <hpsdr_open_interface+0x4b>
lea    0xe60(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1030 <printf@plt>
mov    $0x32,%edi
call   1090 <Sleep@plt>
jmp    1182 <hpsdr_open_interface+0x12>
cmpq   $0x0,-0x28(%rbp)
jne    11d3 <hpsdr_open_interface+0x63>
movq   $0x0,-0x8(%rbp)
jmp    122a <hpsdr_open_interface+0xba>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1050 <usb_set_configuration@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1080 <usb_claim_interface@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1060 <usb_set_altinterface@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x86,%esi
call   1040 <usb_clear_halt@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2,%esi
call   1040 <usb_clear_halt@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hpsdr_open_interface>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1070 <usb_open@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c5 <hpsdr_open_interface+0x55>
lea    0xe71(%rip),%r15
lea    0xe6e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
mov    $0x32,%edi
call   1090 <Sleep@plt>
mov    %r14,%rdi
call   1070 <usb_open@plt>
test   %rax,%rax
je     11a0 <hpsdr_open_interface+0x30>
mov    %rax,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <usb_set_configuration@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <usb_claim_interface@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <usb_set_altinterface@plt>
mov    %rbx,%rdi
mov    $0x86,%esi
call   1040 <usb_clear_halt@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <usb_clear_halt@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.usb_dev_handle = type { i32 }
%struct.usb_device = type { i32 }
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [15 x i8] c"Can't open USB\00"
define %struct.usb_dev_handle* @hpsdr_open_interface(%struct.usb_device* %0, i32 %1, i32 %2) {
%4 = alloca %struct.usb_dev_handle*
%5 = alloca %struct.usb_device*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.usb_dev_handle*
store %struct.usb_device* %0, %struct.usb_device** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
br label %10
10: ; preds = %16, %3
%11 = load %struct.usb_device*, %struct.usb_device** %5
%12 = call %struct.usb_dev_handle* @usb_open(%struct.usb_device* %11)
store %struct.usb_dev_handle* %12, %struct.usb_dev_handle** %9
%13 = icmp eq %struct.usb_dev_handle* %12, null
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14
%17 = call i32 @Sleep(i32 50)
br label %10
18: ; preds = %10
%19 = load %struct.usb_dev_handle*, %struct.usb_dev_handle** %9
%20 = icmp eq %struct.usb_dev_handle* %19, null
br i1 %20, label %21, label %22
21: ; preds = %18
store %struct.usb_dev_handle* null, %struct.usb_dev_handle** %4
br label %34
22: ; preds = %18
%23 = load %struct.usb_dev_handle*, %struct.usb_dev_handle** %9
%24 = call i32 @usb_set_configuration(%struct.usb_dev_handle* %23, i32 1)
store i32 %24, i32* %8
%25 = load %struct.usb_dev_handle*, %struct.usb_dev_handle** %9
%26 = call i32 @usb_claim_interface(%struct.usb_dev_handle* %25, i32 0)
store i32 %26, i32* %8
%27 = load %struct.usb_dev_handle*, %struct.usb_dev_handle** %9
%28 = call i32 @usb_set_altinterface(%struct.usb_dev_handle* %27, i32 0)
store i32 %28, i32* %8
%29 = load %struct.usb_dev_handle*, %struct.usb_dev_handle** %9
%30 = call i32 @usb_clear_halt(%struct.usb_dev_handle* %29, i32 134)
store i32 %30, i32* %8
%31 = load %struct.usb_dev_handle*, %struct.usb_dev_handle** %9
%32 = call i32 @usb_clear_halt(%struct.usb_dev_handle* %31, i32 2)
store i32 %32, i32* %8
%33 = load %struct.usb_dev_handle*, %struct.usb_dev_handle** %9
store %struct.usb_dev_handle* %33, %struct.usb_dev_handle** %4
br label %34
34: ; preds = %22, %21
%35 = load %struct.usb_dev_handle*, %struct.usb_dev_handle** %4
ret %struct.usb_dev_handle* %35
}

LLVM IR (-O2):
%struct.usb_dev_handle = type { i32 }
%struct.usb_device = type { i32 }
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [15 x i8] c"Can't open USB\00"
define %struct.usb_dev_handle* @hpsdr_open_interface(%struct.usb_device* %0, i32 %1, i32 %2) {
%4 = tail call %struct.usb_dev_handle* @usb_open(%struct.usb_device* %0)
%5 = icmp eq %struct.usb_dev_handle* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3, %6
%7 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @Sleep(i32 50)
%9 = tail call %struct.usb_dev_handle* @usb_open(%struct.usb_device* %0)
%10 = icmp eq %struct.usb_dev_handle* %9, null
br i1 %10, label %6, label %11
11: ; preds = %6, %3
%12 = phi %struct.usb_dev_handle* [ %4, %3 ], [ %9, %6 ]
%13 = tail call i32 @usb_set_configuration(%struct.usb_dev_handle* %12, i32 1)
%14 = tail call i32 @usb_claim_interface(%struct.usb_dev_handle* %12, i32 0)
%15 = tail call i32 @usb_set_altinterface(%struct.usb_dev_handle* %12, i32 0)
%16 = tail call i32 @usb_clear_halt(%struct.usb_dev_handle* %12, i32 134)
%17 = tail call i32 @usb_clear_halt(%struct.usb_dev_handle* %12, i32 2)
ret %struct.usb_dev_handle* %12
}