#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 scalar_t__ uid_t;

/* Variables and functions */
int chown(char *, scalar_t__, scalar_t__);
scalar_t__ getgid();
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ usleep(int);
int setup_device(char *vendor, char *product) {
  uid_t orig_uid, orig_gid;
  orig_uid = getuid();
  orig_gid = getgid();
  setuid(0);
  if (getuid() != 0) {
    printf("Couldn't setuid to 0\n");
    return 1;
  } else {
    system("echo 1965 0017 2 076d 0006 > /sys/bus/usb/drivers/cdc_acm/new_id");
    usleep(250 * 1000);
    if (chown("/dev/ttyACM0", orig_uid, orig_gid) == -1)
      perror("chown failed");
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getgid@plt>
mov    %rax,-0x28(%rbp)
xor    %edi,%edi
call   1090 <setuid@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
je     11d6 <setup_device+0x56>
lea    0xe3b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1221 <setup_device+0xa1>
lea    0xe39(%rip),%rdi
call   1040 <system@plt>
mov    $0x3d090,%edi
call   10a0 <usleep@plt>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe5c(%rip),%rdi
call   1070 <chown@plt>
cmp    $0xffffffff,%eax
jne    1215 <setup_device+0x95>
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
jmp    121a <setup_device+0x9a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_device>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1060 <getgid@plt>
mov    %rax,%r15
xor    %edi,%edi
call   1090 <setuid@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     11c1 <setup_device+0x41>
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    11ee <setup_device+0x6e>
lea    0xe38(%rip),%rdi
call   1050 <system@plt>
mov    $0x3d090,%edi
call   10a0 <usleep@plt>
lea    0xe63(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <chown@plt>
cmp    $0xffffffff,%eax
je     11f6 <setup_device+0x76>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
jmp    11ee <setup_device+0x6e>

LLVM IR (-O0):
@.str = [22 x i8] c"Couldn't setuid to 0\0A\00"
@.str.1 = [65 x i8] c"echo 1965 0017 2 076d 0006 > /sys/bus/usb/drivers/cdc_acm/new_id\00"
@.str.2 = [13 x i8] c"/dev/ttyACM0\00"
@.str.3 = [13 x i8] c"chown failed\00"
define i32 @setup_device(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = call i64 (...) @getuid()
store i64 %8, i64* %6
%9 = call i64 (...) @getgid()
store i64 %9, i64* %7
%10 = call i32 @setuid(i32 0)
%11 = call i64 (...) @getuid()
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %26
15: ; preds = %2
%16 = call i32 @system(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @usleep(i32 250000)
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = call i32 @chown(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 %18, i64 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
br label %25
25: ; preds = %24
store i32 0, i32* %3
br label %26
26: ; preds = %25, %13
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str.1 = [65 x i8] c"echo 1965 0017 2 076d 0006 > /sys/bus/usb/drivers/cdc_acm/new_id\00"
@.str.2 = [13 x i8] c"/dev/ttyACM0\00"
@.str.3 = [13 x i8] c"chown failed\00"
@str = [21 x i8] c"Couldn't setuid to 0\00"
define i32 @setup_device(i8* nocapture readnone %0, i8* nocapture readnone %1) {
%3 = tail call i64 (...) @getuid()
%4 = tail call i64 (...) @getgid()
%5 = tail call i32 @setuid(i32 0)
%6 = tail call i64 (...) @getuid()
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %17
10: ; preds = %2
%11 = tail call i32 @system(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @usleep(i32 250000)
%13 = tail call i32 @chown(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 %3, i64 %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %10, %8
%18 = phi i32 [ 1, %8 ], [ 0, %10 ], [ 0, %15 ]
ret i32 %18
}